16#include <initializer_list>
21#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
30#ifdef CPP_UTILITIES_BOOST_PROCESS
31#include <boost/asio/buffers_iterator.hpp>
32#include <boost/asio/io_context.hpp>
33#include <boost/asio/streambuf.hpp>
34#if BOOST_VERSION >= 108600
35#include <boost/process/v1/async.hpp>
36#include <boost/process/v1/child.hpp>
37#include <boost/process/v1/env.hpp>
38#include <boost/process/v1/environment.hpp>
39#include <boost/process/v1/group.hpp>
40#include <boost/process/v1/io.hpp>
41#include <boost/process/v1/search_path.hpp>
43#include <boost/process/async.hpp>
44#include <boost/process/child.hpp>
45#include <boost/process/env.hpp>
46#include <boost/process/environment.hpp>
47#include <boost/process/group.hpp>
48#include <boost/process/io.hpp>
49#include <boost/process/search_path.hpp>
50namespace boost::process {
51namespace v1 = boost::process;
56#ifdef PLATFORM_WINDOWS
69static bool fileSystemItemExists(
const string &path)
73 return stat(path.data(), &res) == 0;
75 const auto widePath(convertMultiByteToWide(path));
76 if (!widePath.first) {
79 const auto fileType(GetFileAttributesW(widePath.first.get()));
80 return fileType != INVALID_FILE_ATTRIBUTES;
84static bool fileExists(
const string &path)
88 return stat(path.data(), &res) == 0 && !S_ISDIR(res.st_mode);
90 const auto widePath(convertMultiByteToWide(path));
91 if (!widePath.first) {
94 const auto fileType(GetFileAttributesW(widePath.first.get()));
95 return (fileType != INVALID_FILE_ATTRIBUTES) && !(fileType & FILE_ATTRIBUTE_DIRECTORY) && !(fileType & FILE_ATTRIBUTE_DEVICE);
99static bool dirExists(
const string &path)
103 return stat(path.data(), &res) == 0 && S_ISDIR(res.st_mode);
105 const auto widePath(convertMultiByteToWide(path));
106 if (!widePath.first) {
109 const auto fileType(GetFileAttributesW(widePath.first.get()));
110 return (fileType != INVALID_FILE_ATTRIBUTES) && (fileType & FILE_ATTRIBUTE_DIRECTORY);
114static bool makeDir(
const string &path)
117 return mkdir(path.data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
119 const auto widePath(convertMultiByteToWide(path));
120 if (!widePath.first) {
123 return CreateDirectoryW(widePath.first.get(),
nullptr) || GetLastError() == ERROR_ALREADY_EXISTS;
152 : m_listArg(
"list",
'l',
"lists available test units")
153 , m_runArg(
"run",
'r',
"runs the tests")
154 , m_testFilesPathArg(
"test-files-path",
'p',
"specifies the path of the directory with test files", {
"path" })
155 , m_applicationPathArg(
"app-path",
'a',
"specifies the path of the application to be tested", {
"path" })
156 , m_workingDirArg(
"working-dir",
'w',
"specifies the directory to store working copies of test files", {
"path" })
157 , m_unitsArg(
"units",
'u',
"specifies the units to test; omit to test all units", {
"unit1",
"unit2",
"unit3" })
161 throw runtime_error(
"only one TestApplication instance allowed at a time");
170 m_runArg.setImplicit(
true);
171 m_runArg.setSubArguments({ &m_testFilesPathArg, &m_applicationPathArg, &m_workingDirArg, &m_unitsArg });
172 m_parser.setMainArguments({ &m_runArg, &m_listArg, &m_parser.noColorArg(), &m_parser.helpArg() });
184 if (m_parser.helpArg().isPresent()) {
191 if (m_testFilesPathArg.isPresent()) {
192 for (
const char *
const testFilesPath : m_testFilesPathArg.values()) {
193 if (*testFilesPath) {
194 m_testFilesPaths.emplace_back(
argsToString(testFilesPath,
'/'));
196 m_testFilesPaths.emplace_back(
"./");
201 bool hasTestFilePathFromEnv;
202 if (
auto testFilePathFromEnv = readTestfilePathFromEnv(); (hasTestFilePathFromEnv = !testFilePathFromEnv.empty())) {
203 m_testFilesPaths.emplace_back(std::move(testFilePathFromEnv));
206 if (
auto testFilePathFromSrcDirRef = readTestfilePathFromSrcRef(); !testFilePathFromSrcDirRef.empty()) {
207 m_testFilesPaths.insert(m_testFilesPaths.end(), std::make_move_iterator(testFilePathFromSrcDirRef.begin()),
208 std::make_move_iterator(testFilePathFromSrcDirRef.end()));
211 m_testFilesPaths.emplace_back(
"./testfiles/");
212 for (
const auto &testFilesPath : m_testFilesPaths) {
213 cerr << testFilesPath <<
'\n';
217 if (m_workingDirArg.isPresent()) {
218 if (*m_workingDirArg.values().front()) {
219 (m_workingDir = m_workingDirArg.values().front()) +=
'/';
223 }
else if (
const char *
const workingDirEnv = getenv(
"WORKING_DIR")) {
224 if (*workingDirEnv) {
228 if ((m_testFilesPathArg.isPresent() && !m_testFilesPathArg.values().empty()) || hasTestFilePathFromEnv) {
229 m_workingDir = m_testFilesPaths.front() +
"workingdir/";
231 m_workingDir =
"./testfiles/workingdir/";
234 cerr <<
"Directory used to store working copies:\n" << m_workingDir <<
'\n';
237 if (
const char *
const profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
238 ofstream(profrawListFile, ios_base::trunc);
249 s_instance =
nullptr;
267 for (
const auto &testFilesPath : m_testFilesPaths) {
268 if (fileExists(path = testFilesPath + relativeTestFilePath)) {
272 throw std::runtime_error(
"The test file \"" % relativeTestFilePath %
"\" can not be located. Was looking under:\n"
273 +
joinStrings(m_testFilesPaths,
"\n",
false,
" - ", relativeTestFilePath));
285 for (
const auto &testFilesPath : m_testFilesPaths) {
286 if (dirExists(path = testFilesPath + relativeTestDirPath)) {
290 throw std::runtime_error(
"The test directory \"" % relativeTestDirPath %
"\" can not be located. Was looking under:\n"
291 +
joinStrings(m_testFilesPaths,
"\n",
false,
" - ", relativeTestDirPath));
321 const std::string &relativeTestFilePath,
const std::string &relativeWorkingCopyPath,
WorkingCopyMode mode)
const
325 if (!dirExists(m_workingDir) && !makeDir(m_workingDir)) {
326 cerr <<
Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": can't create working directory \""
333 if (!parts.empty()) {
336 currentLevel.reserve(m_workingDir.size() + relativeWorkingCopyPath.size() + 1);
337 currentLevel.assign(m_workingDir);
338 for (
auto i = parts.cbegin(), end = parts.end() - 1;
i != end; ++
i) {
339 if (currentLevel.back() !=
'/') {
345 if (dirExists(currentLevel) || makeDir(currentLevel)) {
349 cerr <<
Phrases::Error <<
"Unable to create working copy for \"" << relativeWorkingCopyPath <<
"\": can't create directory \""
363 const auto error = std::strerror(errno);
372 const auto origFilePath =
testFilePath(relativeTestFilePath);
373 size_t workingCopyPathAttempt = 0;
375 origFile.open(origFilePath, ios_base::in | ios_base::binary);
376 if (origFile.fail()) {
377 cerr <<
Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
378 <<
"\": an IO error occurred when opening original file \"" << origFilePath <<
"\"." <<
Phrases::EndFlush;
379 cerr <<
"error: " << std::strerror(errno) << endl;
383 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
388 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
390 if (workingCopy.fail()) {
391 cerr <<
Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
393 cerr <<
"error: " << strerror(errno) << endl;
397 workingCopy << origFile.rdbuf();
399 if (!origFile.fail() && !workingCopy.fail()) {
403 cerr <<
Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": ";
404 if (origFile.fail()) {
405 cerr <<
"an IO error occurred when reading original file \"" << origFilePath <<
"\"";
409 if (workingCopy.fail()) {
410 if (origFile.fail()) {
413 cerr <<
" an IO error occurred when writing to target file \"" <<
workingCopyPath <<
"\".";
415 cerr <<
"error: " << strerror(errno) << endl;
420#ifdef CPP_UTILITIES_HAS_EXEC_APP
422#if defined(CPP_UTILITIES_BOOST_PROCESS)
423inline static std::string streambufToString(boost::asio::streambuf &buf)
425 const auto begin = boost::asio::buffers_begin(buf.data());
426 return std::string(begin, begin +
static_cast<std::ptrdiff_t
>(buf.size()));
434static int execAppInternal(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout,
435 const std::string &newProfilingPath,
bool enableSearchPath =
false)
438 if (!suppressLogging) {
440 cout <<
'-' <<
' ' << appPath;
442 for (
const char *
const *
i = args + 1; *
i; ++
i) {
449#if defined(CPP_UTILITIES_BOOST_PROCESS)
450 auto path = enableSearchPath ? boost::process::v1::search_path(appPath) : boost::process::v1::filesystem::path(appPath);
451 auto ctx = boost::asio::io_context();
452 auto group = boost::process::v1::group();
454#if defined(PLATFORM_WINDOWS)
455 std::vector<std::wstring>();
457 std::vector<std::string>();
460 for (
const char *
const *arg = args + 1; *arg; ++arg) {
461#if defined(PLATFORM_WINDOWS)
462 auto ec = std::error_code();
463 argsAsVector.emplace_back(convertMultiByteToWide(ec, std::string_view(*arg)));
465 throw std::runtime_error(
argsToString(
"unable to convert arg \"", *arg,
"\" to wide string"));
468 argsAsVector.emplace_back(*arg);
472 auto outputBuffer = boost::asio::streambuf(), errorBuffer = boost::asio::streambuf();
473 auto env = boost::process::v1::environment(boost::this_process::environment());
474 if (!newProfilingPath.empty()) {
475 env[
"LLVM_PROFILE_FILE"] = newProfilingPath;
477 auto child = boost::process::v1::child(
478 ctx, group, path, argsAsVector, env, boost::process::v1::std_out > outputBuffer, boost::process::v1::std_err > errorBuffer);
480 ctx.run_for(std::chrono::milliseconds(timeout));
484 output = streambufToString(outputBuffer);
485 errors = streambufToString(errorBuffer);
488 return child.exit_code();
490#elif defined(PLATFORM_UNIX)
492 int coutPipes[2], cerrPipes[2];
493 if (pipe(coutPipes) != 0 || pipe(cerrPipes) != 0) {
494 throw std::runtime_error(
argsToString(
"Unable to create pipe: ", std::strerror(errno)));
496 const auto readCoutPipe = coutPipes[0], writeCoutPipe = coutPipes[1];
497 const auto readCerrPipe = cerrPipes[0], writeCerrPipe = cerrPipes[1];
500 if (
const auto child = fork()) {
502 close(writeCoutPipe);
503 close(writeCerrPipe);
507 throw std::runtime_error(
argsToString(
"Unable to create fork: ", std::strerror(errno)));
511 struct pollfd fileDescriptorSet[2];
512 fileDescriptorSet[0].fd = readCoutPipe;
513 fileDescriptorSet[1].fd = readCerrPipe;
514 fileDescriptorSet[0].events = fileDescriptorSet[1].events = POLLIN;
523 const auto retpoll = poll(fileDescriptorSet, 2, timeout);
525 throw std::runtime_error(
"Poll timed out");
528 throw std::runtime_error(
argsToString(
"Poll failed: ", std::strerror(errno)));
530 if (fileDescriptorSet[0].revents & POLLIN) {
531 const auto count = read(readCoutPipe, buffer,
sizeof(buffer));
533 output.append(buffer,
static_cast<size_t>(count));
535 }
else if (fileDescriptorSet[0].revents & POLLHUP) {
537 fileDescriptorSet[0].fd = -1;
539 if (fileDescriptorSet[1].revents & POLLIN) {
540 const auto count = read(readCerrPipe, buffer,
sizeof(buffer));
542 errors.append(buffer,
static_cast<size_t>(count));
544 }
else if (fileDescriptorSet[1].revents & POLLHUP) {
546 fileDescriptorSet[1].fd = -1;
548 }
while (fileDescriptorSet[0].fd >= 0 || fileDescriptorSet[1].fd >= 0);
558 waitpid(child, &childReturnCode, 0);
559 waitpid(-child,
nullptr, 0);
560 return childReturnCode;
564 if (dup2(writeCoutPipe, STDOUT_FILENO) == -1 || dup2(writeCerrPipe, STDERR_FILENO) == -1) {
565 std::cerr << Phrases::Error <<
"Unable to duplicate file descriptor: " << std::strerror(errno) << Phrases::EndFlush;
566 std::exit(EXIT_FAILURE);
569 close(writeCoutPipe);
571 close(writeCerrPipe);
575 cerr << Phrases::Error <<
"Unable create process group: " << std::strerror(errno) << Phrases::EndFlush;
580 if (!newProfilingPath.empty()) {
581 setenv(
"LLVM_PROFILE_FILE", newProfilingPath.data(),
true);
585 if (enableSearchPath) {
586 execvp(appPath,
const_cast<char *
const *
>(args));
588 execv(appPath,
const_cast<char *
const *
>(args));
590 cerr << Phrases::Error <<
"Unable to execute \"" << appPath <<
"\": " << std::strerror(errno) << Phrases::EndFlush;
595 throw std::runtime_error(
"lauching test applications is not supported on this platform");
607int TestApplication::execApp(
const char *
const *args,
string &output,
string &errors,
bool suppressLogging,
int timeout)
const
610 static unsigned int invocationCount = 0;
614 const char *
appPath = m_applicationPathArg.firstValue();
615 auto fallbackAppPath = string();
620 const char *
const testAppPath = m_parser.executable();
621 const auto testAppPathLength = strlen(testAppPath);
622 if (testAppPathLength > 6 && !strcmp(testAppPath + testAppPathLength - 6,
"_tests")) {
623 fallbackAppPath.assign(testAppPath, testAppPathLength - 6);
624 appPath = fallbackAppPath.data();
627 throw runtime_error(
"Unable to execute application to be tested: no application path specified");
632 const auto newProfilingPath = [
appPath] {
633 auto path = string();
634 const char *
const llvmProfileFile = getenv(
"LLVM_PROFILE_FILE");
635 if (!llvmProfileFile) {
639 const char *
const llvmProfileFileEnd = strstr(llvmProfileFile,
".profraw");
640 if (!llvmProfileFileEnd) {
643 const auto llvmProfileFileWithoutExtension = string(llvmProfileFile, llvmProfileFileEnd);
645 const char *appName = strrchr(
appPath,
'/');
646 appName = appName ? appName + 1 :
appPath;
648 path =
argsToString(llvmProfileFileWithoutExtension,
'_', appName, invocationCount,
".profraw");
650 if (
const char *
const profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
651 ofstream(profrawListFile, ios_base::app) << path << endl;
656 return execAppInternal(
appPath, args, output, errors, suppressLogging, timeout, newProfilingPath);
665int execHelperApp(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout)
667 return execAppInternal(appPath, args, output, errors, suppressLogging, timeout,
string());
679int execHelperAppInSearchPath(
680 const char *appName,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout)
682 return execAppInternal(appName, args, output, errors, suppressLogging, timeout,
string(),
true);
689string TestApplication::readTestfilePathFromEnv()
691 const char *
const testFilesPathEnv = getenv(
"TEST_FILE_PATH");
692 if (!testFilesPathEnv || !*testFilesPathEnv) {
703std::vector<std::string> TestApplication::readTestfilePathFromSrcRef()
707 auto res = std::vector<std::string>();
708 auto binaryPath = std::string();
709#if defined(CPP_UTILITIES_USE_STANDARD_FILESYSTEM) && defined(PLATFORM_UNIX)
711 binaryPath = std::filesystem::read_symlink(
"/proc/self/exe").parent_path();
713 }
catch (
const std::filesystem::filesystem_error &e) {
714 cerr << Phrases::Warning <<
"Unable to detect binary path for finding \"srcdirref\": " << e.what() << Phrases::EndFlush;
717 const auto srcdirrefPath = binaryPath +
"srcdirref";
720 const auto srcDirContent =
readFile(srcdirrefPath, 2 * 1024);
721 if (srcDirContent.empty()) {
722 cerr << Phrases::Warning <<
"The file \"srcdirref\" is empty." << Phrases::EndFlush;
728 for (
const auto &srcPath : srcPaths) {
729 auto testfilesPath =
argsToString(srcPath,
"/testfiles/");
730 if (dirExists(testfilesPath)) {
731 res.emplace_back(std::move(testfilesPath));
733 cerr << Phrases::Warning
734 <<
"The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist."
735 << Phrases::End <<
"Referenced source directory: " << testfilesPath << endl;
740 }
catch (
const std::ios_base::failure &e) {
741 cerr << Phrases::Warning <<
"The file \"" << srcdirrefPath <<
"\" can not be opened: " << e.what() << Phrases::EndFlush;
static constexpr std::size_t varValueCount
Denotes a variable number of values.
The ParseError class is thrown by an ArgumentParser when a parsing error occurs.
The TestApplication class simplifies writing test applications that require opening test files.
std::string workingCopyPath(const std::string &relativeTestFilePath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy) const
Returns the full path to a working copy of the test file with the specified relativeTestFilePath.
std::string testFilePath(const std::string &relativeTestFilePath) const
Returns the full path of the test file with the specified relativeTestFilePath.
static const char * appPath()
Returns the application path or an empty string if no application path has been set.
TestApplication()
Constructs a TestApplication instance without further arguments.
std::string workingCopyPathAs(const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode=WorkingCopyMode::CreateCopy) const
Returns the full path to a working copy of the test file with the specified relativeTestFilePath.
std::string testDirPath(const std::string &relativeTestDirPath) const
Returns the full path of the test directory with the specified relativeTestDirPath.
~TestApplication()
Destroys the TestApplication.
Encapsulates functions for formatted terminal output using ANSI escape codes.
Contains all utilities provides by the c++utilities library.
CPP_UTILITIES_EXPORT std::string readFile(const std::string &path, std::string::size_type maxSize=std::string::npos)
Reads all contents of the specified file in a single call.
WorkingCopyMode
The WorkingCopyMode enum specifies additional options to influence behavior of TestApplication::worki...
ReturnType joinStrings(const Container &strings, Detail::StringParamForContainer< Container > delimiter=Detail::StringParamForContainer< Container >(), bool omitEmpty=false, Detail::StringParamForContainer< Container > leftClosure=Detail::StringParamForContainer< Container >(), Detail::StringParamForContainer< Container > rightClosure=Detail::StringParamForContainer< Container >())
Joins the given strings using the specified delimiter.
std::fstream NativeFileStream
Container splitStringSimple(Detail::StringParamForContainer< Container > string, Detail::StringParamForContainer< Container > delimiter, int maxParts=-1)
Splits the given string (which might also be a string view) at the specified delimiter.
StringType argsToString(Args &&...args)
Container splitString(Detail::StringParamForContainer< Container > string, Detail::StringParamForContainer< Container > delimiter, EmptyPartsTreat emptyPartsRole=EmptyPartsTreat::Keep, int maxParts=-1)
Splits the given string at the specified delimiter.