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