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