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