xref: /freebsd/contrib/atf/atf-c++/detail/test_helpers.cpp (revision 8f0ea33f2bbf3a6aa80235f0a02fa5f2780c2b17)
1c243e490SMarcel Moolenaar // Copyright (c) 2009 The NetBSD Foundation, Inc.
2c243e490SMarcel Moolenaar // All rights reserved.
3c243e490SMarcel Moolenaar //
4c243e490SMarcel Moolenaar // Redistribution and use in source and binary forms, with or without
5c243e490SMarcel Moolenaar // modification, are permitted provided that the following conditions
6c243e490SMarcel Moolenaar // are met:
7c243e490SMarcel Moolenaar // 1. Redistributions of source code must retain the above copyright
8c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer.
9c243e490SMarcel Moolenaar // 2. Redistributions in binary form must reproduce the above copyright
10c243e490SMarcel Moolenaar //    notice, this list of conditions and the following disclaimer in the
11c243e490SMarcel Moolenaar //    documentation and/or other materials provided with the distribution.
12c243e490SMarcel Moolenaar //
13c243e490SMarcel Moolenaar // THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14c243e490SMarcel Moolenaar // CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15c243e490SMarcel Moolenaar // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16c243e490SMarcel Moolenaar // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17c243e490SMarcel Moolenaar // IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18c243e490SMarcel Moolenaar // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c243e490SMarcel Moolenaar // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20c243e490SMarcel Moolenaar // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21c243e490SMarcel Moolenaar // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22c243e490SMarcel Moolenaar // IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23c243e490SMarcel Moolenaar // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24c243e490SMarcel Moolenaar // IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25*0677dfd1SJulio Merino 
26*0677dfd1SJulio Merino #include "atf-c++/detail/test_helpers.hpp"
27c243e490SMarcel Moolenaar 
28c243e490SMarcel Moolenaar #include <fstream>
29c243e490SMarcel Moolenaar #include <iostream>
30c243e490SMarcel Moolenaar #include <string>
31c243e490SMarcel Moolenaar #include <vector>
32c243e490SMarcel Moolenaar 
33*0677dfd1SJulio Merino #include <atf-c++.hpp>
34c243e490SMarcel Moolenaar 
35*0677dfd1SJulio Merino #include "atf-c++/check.hpp"
36*0677dfd1SJulio Merino #include "atf-c++/detail/env.hpp"
37*0677dfd1SJulio Merino #include "atf-c++/detail/fs.hpp"
38*0677dfd1SJulio Merino #include "atf-c++/detail/process.hpp"
39c243e490SMarcel Moolenaar 
40b17b15d8SJulio Merino // Path to the directory containing the libatf-c tests, used to locate the
41b17b15d8SJulio Merino // process_helpers program.  If NULL (the default), the code will use a
42b17b15d8SJulio Merino // relative path.  Otherwise, the provided path will be used; this is so
43b17b15d8SJulio Merino // that we can locate the helpers binary if the installation uses a
44b17b15d8SJulio Merino // different layout than the one we provide (as is the case in FreeBSD).
45b17b15d8SJulio Merino #if defined(ATF_C_TESTS_BASE)
46b17b15d8SJulio Merino static const char* atf_c_tests_base = ATF_C_TESTS_BASE;
47b17b15d8SJulio Merino #else
48b17b15d8SJulio Merino static const char* atf_c_tests_base = NULL;
49b17b15d8SJulio Merino #endif
50b17b15d8SJulio Merino #undef ATF_C_TESTS_BASE
51b17b15d8SJulio Merino 
521a61beb0SJulio Merino bool
build_check_cxx_o(const char * sfile)531a61beb0SJulio Merino build_check_cxx_o(const char* sfile)
54c243e490SMarcel Moolenaar {
55c243e490SMarcel Moolenaar     std::vector< std::string > optargs;
56*0677dfd1SJulio Merino     optargs.push_back("-I" + atf::env::get("ATF_INCLUDEDIR", ATF_INCLUDEDIR));
57c243e490SMarcel Moolenaar     optargs.push_back("-Wall");
58c243e490SMarcel Moolenaar     optargs.push_back("-Werror");
59c243e490SMarcel Moolenaar 
601a61beb0SJulio Merino     return atf::check::build_cxx_o(sfile, "test.o",
611a61beb0SJulio Merino                                    atf::process::argv_array(optargs));
62c243e490SMarcel Moolenaar }
63c243e490SMarcel Moolenaar 
641a61beb0SJulio Merino bool
build_check_cxx_o_srcdir(const atf::tests::tc & tc,const char * sfile)651a61beb0SJulio Merino build_check_cxx_o_srcdir(const atf::tests::tc& tc, const char* sfile)
66c243e490SMarcel Moolenaar {
67c243e490SMarcel Moolenaar     const atf::fs::path sfilepath =
68c243e490SMarcel Moolenaar         atf::fs::path(tc.get_config_var("srcdir")) / sfile;
691a61beb0SJulio Merino     return build_check_cxx_o(sfilepath.c_str());
70c243e490SMarcel Moolenaar }
71c243e490SMarcel Moolenaar 
72c243e490SMarcel Moolenaar void
header_check(const char * hdrname)73c243e490SMarcel Moolenaar header_check(const char *hdrname)
74c243e490SMarcel Moolenaar {
756d732c66SJulio Merino     std::ofstream srcfile("test.cpp");
76c243e490SMarcel Moolenaar     ATF_REQUIRE(srcfile);
77c243e490SMarcel Moolenaar     srcfile << "#include <" << hdrname << ">\n";
78c243e490SMarcel Moolenaar     srcfile.close();
79c243e490SMarcel Moolenaar 
80c243e490SMarcel Moolenaar     const std::string failmsg = std::string("Header check failed; ") +
81c243e490SMarcel Moolenaar         hdrname + " is not self-contained";
821a61beb0SJulio Merino     if (!build_check_cxx_o("test.cpp"))
831a61beb0SJulio Merino         ATF_FAIL(failmsg);
84c243e490SMarcel Moolenaar }
85c243e490SMarcel Moolenaar 
86c243e490SMarcel Moolenaar atf::fs::path
get_process_helpers_path(const atf::tests::tc & tc,bool is_detail)875686c6c3SMarcel Moolenaar get_process_helpers_path(const atf::tests::tc& tc, bool is_detail)
88c243e490SMarcel Moolenaar {
89b17b15d8SJulio Merino     const char* helper = "detail/process_helpers";
90b17b15d8SJulio Merino     if (atf_c_tests_base == NULL) {
915686c6c3SMarcel Moolenaar         if (is_detail)
925686c6c3SMarcel Moolenaar             return atf::fs::path(tc.get_config_var("srcdir")) /
93b17b15d8SJulio Merino                    ".." / ".." / "atf-c" / helper;
945686c6c3SMarcel Moolenaar         else
95c243e490SMarcel Moolenaar             return atf::fs::path(tc.get_config_var("srcdir")) /
96b17b15d8SJulio Merino                    ".." / "atf-c" / helper;
97b17b15d8SJulio Merino     } else {
98b17b15d8SJulio Merino         return atf::fs::path(atf_c_tests_base) / helper;
99b17b15d8SJulio Merino     }
100c243e490SMarcel Moolenaar }
101