Path: blob/main/contrib/atf/atf-c++/detail/test_helpers.cpp
39563 views
// Copyright (c) 2009 The NetBSD Foundation, Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions5// are met:6// 1. Redistributions of source code must retain the above copyright7// notice, this list of conditions and the following disclaimer.8// 2. Redistributions in binary form must reproduce the above copyright9// notice, this list of conditions and the following disclaimer in the10// documentation and/or other materials provided with the distribution.11//12// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND13// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,14// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF15// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE19// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS20// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER21// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR22// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN23// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425#include "atf-c++/detail/test_helpers.hpp"2627#include <fstream>28#include <iostream>29#include <string>30#include <vector>3132#include <atf-c++.hpp>3334#include "atf-c++/check.hpp"35#include "atf-c++/detail/env.hpp"36#include "atf-c++/detail/fs.hpp"37#include "atf-c++/detail/process.hpp"3839// Path to the directory containing the libatf-c tests, used to locate the40// process_helpers program. If NULL (the default), the code will use a41// relative path. Otherwise, the provided path will be used; this is so42// that we can locate the helpers binary if the installation uses a43// different layout than the one we provide (as is the case in FreeBSD).44#if defined(ATF_C_TESTS_BASE)45static const char* atf_c_tests_base = ATF_C_TESTS_BASE;46#else47static const char* atf_c_tests_base = NULL;48#endif49#undef ATF_C_TESTS_BASE5051bool52build_check_cxx_o(const char* sfile)53{54std::vector< std::string > optargs;55optargs.push_back("-I" + atf::env::get("ATF_INCLUDEDIR", ATF_INCLUDEDIR));56optargs.push_back("-Wall");57optargs.push_back("-Werror");5859return atf::check::build_cxx_o(sfile, "test.o",60atf::process::argv_array(optargs));61}6263bool64build_check_cxx_o_srcdir(const atf::tests::tc& tc, const char* sfile)65{66const atf::fs::path sfilepath =67atf::fs::path(tc.get_config_var("srcdir")) / sfile;68return build_check_cxx_o(sfilepath.c_str());69}7071void72header_check(const char *hdrname)73{74std::ofstream srcfile("test.cpp");75ATF_REQUIRE(srcfile);76srcfile << "#include <" << hdrname << ">\n";77srcfile.close();7879const std::string failmsg = std::string("Header check failed; ") +80hdrname + " is not self-contained";81if (!build_check_cxx_o("test.cpp"))82ATF_FAIL(failmsg);83}8485atf::fs::path86get_process_helpers_path(const atf::tests::tc& tc, bool is_detail)87{88const char* helper = "detail/process_helpers";89if (atf_c_tests_base == NULL) {90if (is_detail)91return atf::fs::path(tc.get_config_var("srcdir")) /92".." / ".." / "atf-c" / helper;93else94return atf::fs::path(tc.get_config_var("srcdir")) /95".." / "atf-c" / helper;96} else {97return atf::fs::path(atf_c_tests_base) / helper;98}99}100101102