test_helpers.cpp revision 302408
1106266Sjulian// Copyright (c) 2009 The NetBSD Foundation, Inc.
2106266Sjulian// All rights reserved.
3106266Sjulian//
4106266Sjulian// Redistribution and use in source and binary forms, with or without
5106266Sjulian// modification, are permitted provided that the following conditions
6106266Sjulian// are met:
7106266Sjulian// 1. Redistributions of source code must retain the above copyright
8106266Sjulian//    notice, this list of conditions and the following disclaimer.
9106319Sjulian// 2. Redistributions in binary form must reproduce the above copyright
10106266Sjulian//    notice, this list of conditions and the following disclaimer in the
11106319Sjulian//    documentation and/or other materials provided with the distribution.
12106319Sjulian//
13106266Sjulian// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14106319Sjulian// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15106319Sjulian// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16106266Sjulian// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17106266Sjulian// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18106266Sjulian// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19106319Sjulian// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20106319Sjulian// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21106319Sjulian// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22106266Sjulian// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23106266Sjulian// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24106266Sjulian// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25106266Sjulian
26106266Sjulian#include "atf-c++/detail/test_helpers.hpp"
27106319Sjulian
28106266Sjulian#include <fstream>
29106266Sjulian#include <iostream>
30106266Sjulian#include <string>
31106266Sjulian#include <vector>
32106266Sjulian
33106266Sjulian#include <atf-c++.hpp>
34106266Sjulian
35106266Sjulian#include "atf-c++/check.hpp"
36106266Sjulian#include "atf-c++/detail/env.hpp"
37106266Sjulian#include "atf-c++/detail/fs.hpp"
38106266Sjulian#include "atf-c++/detail/process.hpp"
39106266Sjulian
40106266Sjulian// Path to the directory containing the libatf-c tests, used to locate the
41106266Sjulian// process_helpers program.  If NULL (the default), the code will use a
42106266Sjulian// relative path.  Otherwise, the provided path will be used; this is so
43106266Sjulian// that we can locate the helpers binary if the installation uses a
44106266Sjulian// different layout than the one we provide (as is the case in FreeBSD).
45106266Sjulian#if defined(ATF_C_TESTS_BASE)
46106266Sjulianstatic const char* atf_c_tests_base = ATF_C_TESTS_BASE;
47106266Sjulian#else
48106266Sjulianstatic const char* atf_c_tests_base = NULL;
49106266Sjulian#endif
50106266Sjulian#undef ATF_C_TESTS_BASE
51106266Sjulian
52106266Sjulianbool
53106266Sjulianbuild_check_cxx_o(const char* sfile)
54106266Sjulian{
55106266Sjulian    std::vector< std::string > optargs;
56106266Sjulian    optargs.push_back("-I" + atf::env::get("ATF_INCLUDEDIR", ATF_INCLUDEDIR));
57106266Sjulian    optargs.push_back("-Wall");
58106266Sjulian    optargs.push_back("-Werror");
59106266Sjulian
60106266Sjulian    return atf::check::build_cxx_o(sfile, "test.o",
61106266Sjulian                                   atf::process::argv_array(optargs));
62106266Sjulian}
63106266Sjulian
64106266Sjulianbool
65106266Sjulianbuild_check_cxx_o_srcdir(const atf::tests::tc& tc, const char* sfile)
66106266Sjulian{
67106266Sjulian    const atf::fs::path sfilepath =
68106266Sjulian        atf::fs::path(tc.get_config_var("srcdir")) / sfile;
69106266Sjulian    return build_check_cxx_o(sfilepath.c_str());
70106266Sjulian}
71106266Sjulian
72106266Sjulianvoid
73106266Sjulianheader_check(const char *hdrname)
74106266Sjulian{
75106266Sjulian    std::ofstream srcfile("test.cpp");
76106266Sjulian    ATF_REQUIRE(srcfile);
77106266Sjulian    srcfile << "#include <" << hdrname << ">\n";
78106266Sjulian    srcfile.close();
79106266Sjulian
80106266Sjulian    const std::string failmsg = std::string("Header check failed; ") +
81106266Sjulian        hdrname + " is not self-contained";
82106266Sjulian    if (!build_check_cxx_o("test.cpp"))
83106266Sjulian        ATF_FAIL(failmsg);
84106266Sjulian}
85106266Sjulian
86106266Sjulianatf::fs::path
87106319Sjulianget_process_helpers_path(const atf::tests::tc& tc, bool is_detail)
88106266Sjulian{
89106266Sjulian    const char* helper = "detail/process_helpers";
90106319Sjulian    if (atf_c_tests_base == NULL) {
91106266Sjulian        if (is_detail)
92106266Sjulian            return atf::fs::path(tc.get_config_var("srcdir")) /
93106266Sjulian                   ".." / ".." / "atf-c" / helper;
94106266Sjulian        else
95106266Sjulian            return atf::fs::path(tc.get_config_var("srcdir")) /
96106266Sjulian                   ".." / "atf-c" / helper;
97106266Sjulian    } else {
98106266Sjulian        return atf::fs::path(atf_c_tests_base) / helper;
99106266Sjulian    }
100106266Sjulian}
101106266Sjulian