3#include "../conversion/stringbuilder.h"
4#include "../conversion/stringconversion.h"
5#include "../io/ansiescapecodes.h"
7#include "../io/nativefilestream.h"
9#include "../misc/parseerror.h"
16#include <initializer_list>
21#ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
30#ifdef PLATFORM_WINDOWS
43static bool fileSystemItemExists(
const string &path)
47 return stat(path.data(), &res) == 0;
49 const auto widePath(convertMultiByteToWide(path));
50 if (!widePath.first) {
53 const auto fileType(GetFileAttributesW(widePath.first.get()));
54 return fileType != INVALID_FILE_ATTRIBUTES;
58static bool fileExists(
const string &path)
62 return stat(path.data(), &res) == 0 && !S_ISDIR(res.st_mode);
64 const auto widePath(convertMultiByteToWide(path));
65 if (!widePath.first) {
68 const auto fileType(GetFileAttributesW(widePath.first.get()));
69 return (fileType != INVALID_FILE_ATTRIBUTES) && !(fileType & FILE_ATTRIBUTE_DIRECTORY) && !(fileType & FILE_ATTRIBUTE_DEVICE);
73static bool dirExists(
const string &path)
77 return stat(path.data(), &res) == 0 && S_ISDIR(res.st_mode);
79 const auto widePath(convertMultiByteToWide(path));
80 if (!widePath.first) {
83 const auto fileType(GetFileAttributesW(widePath.first.get()));
84 return (fileType != INVALID_FILE_ATTRIBUTES) && (fileType & FILE_ATTRIBUTE_DIRECTORY);
88static bool makeDir(
const string &path)
91 return mkdir(path.data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
93 const auto widePath(convertMultiByteToWide(path));
94 if (!widePath.first) {
97 return CreateDirectoryW(widePath.first.get(),
nullptr) || GetLastError() == ERROR_ALREADY_EXISTS;
126 : m_listArg(
"list",
'l',
"lists available test units")
127 , m_runArg(
"run",
'r',
"runs the tests")
128 , m_testFilesPathArg(
"test-files-path",
'p',
"specifies the path of the directory with test files", {
"path" })
129 , m_applicationPathArg(
"app-path",
'a',
"specifies the path of the application to be tested", {
"path" })
130 , m_workingDirArg(
"working-dir",
'w',
"specifies the directory to store working copies of test files", {
"path" })
131 , m_unitsArg(
"units",
'u',
"specifies the units to test; omit to test all units", {
"unit1",
"unit2",
"unit3" })
135 throw runtime_error(
"only one TestApplication instance allowed at a time");
144 m_runArg.setImplicit(
true);
145 m_runArg.setSubArguments({ &m_testFilesPathArg, &m_applicationPathArg, &m_workingDirArg, &m_unitsArg });
146 m_parser.setMainArguments({ &m_runArg, &m_listArg, &m_parser.noColorArg(), &m_parser.helpArg() });
158 if (m_parser.helpArg().isPresent()) {
165 if (m_testFilesPathArg.isPresent()) {
166 for (
const char *
const testFilesPath : m_testFilesPathArg.values()) {
167 if (*testFilesPath) {
168 m_testFilesPaths.emplace_back(
argsToString(testFilesPath,
'/'));
170 m_testFilesPaths.emplace_back(
"./");
175 bool hasTestFilePathFromEnv;
176 if (
auto testFilePathFromEnv = readTestfilePathFromEnv(); (hasTestFilePathFromEnv = !testFilePathFromEnv.empty())) {
177 m_testFilesPaths.emplace_back(std::move(testFilePathFromEnv));
180 if (
auto testFilePathFromSrcDirRef = readTestfilePathFromSrcRef(); !testFilePathFromSrcDirRef.empty()) {
181 m_testFilesPaths.insert(m_testFilesPaths.end(), std::make_move_iterator(testFilePathFromSrcDirRef.begin()),
182 std::make_move_iterator(testFilePathFromSrcDirRef.end()));
185 m_testFilesPaths.emplace_back(
"./testfiles/");
186 for (
const auto &testFilesPath : m_testFilesPaths) {
187 cerr << testFilesPath <<
'\n';
191 if (m_workingDirArg.isPresent()) {
192 if (*m_workingDirArg.values().front()) {
193 (m_workingDir = m_workingDirArg.values().front()) +=
'/';
197 }
else if (
const char *
const workingDirEnv = getenv(
"WORKING_DIR")) {
198 if (*workingDirEnv) {
202 if ((m_testFilesPathArg.isPresent() && !m_testFilesPathArg.values().empty()) || hasTestFilePathFromEnv) {
203 m_workingDir = m_testFilesPaths.front() +
"workingdir/";
205 m_workingDir =
"./testfiles/workingdir/";
208 cerr <<
"Directory used to store working copies:\n" << m_workingDir <<
'\n';
211 if (
const char *
const profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
212 ofstream(profrawListFile, ios_base::trunc);
223 s_instance =
nullptr;
241 for (
const auto &testFilesPath : m_testFilesPaths) {
242 if (fileExists(path = testFilesPath + relativeTestFilePath)) {
246 throw std::runtime_error(
"The test file \"" % relativeTestFilePath %
"\" can not be located. Was looking under:\n"
247 +
joinStrings(m_testFilesPaths,
"\n",
false,
" - ", relativeTestFilePath));
259 for (
const auto &testFilesPath : m_testFilesPaths) {
260 if (dirExists(path = testFilesPath + relativeTestDirPath)) {
264 throw std::runtime_error(
"The test directory \"" % relativeTestDirPath %
"\" can not be located. Was looking under:\n"
265 +
joinStrings(m_testFilesPaths,
"\n",
false,
" - ", relativeTestDirPath));
295 const std::string &relativeTestFilePath,
const std::string &relativeWorkingCopyPath,
WorkingCopyMode mode)
const
299 if (!dirExists(m_workingDir) && !makeDir(m_workingDir)) {
300 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": can't create working directory \""
301 << m_workingDir <<
"\"." << Phrases::EndFlush;
307 if (!parts.empty()) {
310 currentLevel.reserve(m_workingDir.size() + relativeWorkingCopyPath.size() + 1);
311 currentLevel.assign(m_workingDir);
312 for (
auto i = parts.cbegin(), end = parts.end() - 1;
i != end; ++
i) {
313 if (currentLevel.back() !=
'/') {
319 if (dirExists(currentLevel) || makeDir(currentLevel)) {
323 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeWorkingCopyPath <<
"\": can't create directory \""
324 << currentLevel <<
"\" (inside working directory)." << Phrases::EndFlush;
337 const auto error = std::strerror(errno);
338 cerr << Phrases::Error <<
"Unable to delete \"" <<
workingCopyPath <<
"\": " << error << Phrases::EndFlush;
346 const auto origFilePath =
testFilePath(relativeTestFilePath);
347 size_t workingCopyPathAttempt = 0;
349 origFile.open(origFilePath, ios_base::in | ios_base::binary);
350 if (origFile.fail()) {
351 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
352 <<
"\": an IO error occurred when opening original file \"" << origFilePath <<
"\"." << Phrases::EndFlush;
353 cerr <<
"error: " << std::strerror(errno) << endl;
357 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
362 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
364 if (workingCopy.fail()) {
365 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
366 <<
"\": an IO error occurred when opening target file \"" <<
workingCopyPath <<
"\"." << Phrases::EndFlush;
367 cerr <<
"error: " << strerror(errno) << endl;
371 workingCopy << origFile.rdbuf();
373 if (!origFile.fail() && !workingCopy.fail()) {
377 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": ";
378 if (origFile.fail()) {
379 cerr <<
"an IO error occurred when reading original file \"" << origFilePath <<
"\"";
383 if (workingCopy.fail()) {
384 if (origFile.fail()) {
387 cerr <<
" an IO error occurred when writing to target file \"" <<
workingCopyPath <<
"\".";
389 cerr <<
"error: " << strerror(errno) << endl;
399static int execAppInternal(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout,
400 const std::string &newProfilingPath,
bool enableSearchPath =
false)
403 if (!suppressLogging) {
405 cout <<
'-' <<
' ' << appPath;
407 for (
const char *
const *
i = args + 1; *
i; ++
i) {
415 int coutPipes[2], cerrPipes[2];
418 const auto readCoutPipe = coutPipes[0], writeCoutPipe = coutPipes[1];
419 const auto readCerrPipe = cerrPipes[0], writeCerrPipe = cerrPipes[1];
422 if (
const auto child = fork()) {
424 close(writeCoutPipe);
425 close(writeCerrPipe);
429 throw runtime_error(
"Unable to create fork");
433 struct pollfd fileDescriptorSet[2];
434 fileDescriptorSet[0].fd = readCoutPipe;
435 fileDescriptorSet[1].fd = readCerrPipe;
436 fileDescriptorSet[0].events = fileDescriptorSet[1].events = POLLIN;
445 const auto retpoll = poll(fileDescriptorSet, 2, timeout);
447 throw runtime_error(
"Poll time-out");
450 throw runtime_error(
"Poll failed");
452 if (fileDescriptorSet[0].revents & POLLIN) {
453 const auto count = read(readCoutPipe, buffer,
sizeof(buffer));
455 output.append(buffer,
static_cast<size_t>(count));
457 }
else if (fileDescriptorSet[0].revents & POLLHUP) {
459 fileDescriptorSet[0].fd = -1;
461 if (fileDescriptorSet[1].revents & POLLIN) {
462 const auto count = read(readCerrPipe, buffer,
sizeof(buffer));
464 errors.append(buffer,
static_cast<size_t>(count));
466 }
else if (fileDescriptorSet[1].revents & POLLHUP) {
468 fileDescriptorSet[1].fd = -1;
470 }
while (fileDescriptorSet[0].fd >= 0 || fileDescriptorSet[1].fd >= 0);
480 waitpid(child, &childReturnCode, 0);
481 return childReturnCode;
485 dup2(writeCoutPipe, STDOUT_FILENO);
486 dup2(writeCerrPipe, STDERR_FILENO);
488 close(writeCoutPipe);
490 close(writeCerrPipe);
493 if (!newProfilingPath.empty()) {
494 setenv(
"LLVM_PROFILE_FILE", newProfilingPath.data(),
true);
498 if (enableSearchPath) {
499 execvp(appPath,
const_cast<char *
const *
>(args));
502 execv(appPath,
const_cast<char *
const *
>(args));
504 cerr << Phrases::Error <<
"Unable to execute \"" << appPath <<
"\": execv() failed" << Phrases::EndFlush;
518int TestApplication::execApp(
const char *
const *args,
string &output,
string &errors,
bool suppressLogging,
int timeout)
const
521 static unsigned int invocationCount = 0;
526 auto fallbackAppPath = string();
531 const char *
const testAppPath = m_parser.
executable();
532 const auto testAppPathLength = strlen(testAppPath);
533 if (testAppPathLength > 6 && !strcmp(testAppPath + testAppPathLength - 6,
"_tests")) {
534 fallbackAppPath.assign(testAppPath, testAppPathLength - 6);
535 appPath = fallbackAppPath.data();
538 throw runtime_error(
"Unable to execute application to be tested: no application path specified");
543 const auto newProfilingPath = [
appPath] {
544 auto path = string();
545 const char *
const llvmProfileFile = getenv(
"LLVM_PROFILE_FILE");
546 if (!llvmProfileFile) {
550 const char *
const llvmProfileFileEnd = strstr(llvmProfileFile,
".profraw");
551 if (!llvmProfileFileEnd) {
554 const auto llvmProfileFileWithoutExtension = string(llvmProfileFile, llvmProfileFileEnd);
556 const char *appName = strrchr(
appPath,
'/');
557 appName = appName ? appName + 1 :
appPath;
559 path =
argsToString(llvmProfileFileWithoutExtension,
'_', appName, invocationCount,
".profraw");
561 if (
const char *
const profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
562 ofstream(profrawListFile, ios_base::app) << path << endl;
567 return execAppInternal(
appPath, args, output, errors, suppressLogging, timeout, newProfilingPath);
577int execHelperApp(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout)
579 return execAppInternal(appPath, args, output, errors, suppressLogging, timeout,
string());
592int execHelperAppInSearchPath(
593 const char *appName,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout)
595 return execAppInternal(appName, args, output, errors, suppressLogging, timeout,
string(),
true);
602string TestApplication::readTestfilePathFromEnv()
604 const char *
const testFilesPathEnv = getenv(
"TEST_FILE_PATH");
605 if (!testFilesPathEnv || !*testFilesPathEnv) {
616std::vector<std::string> TestApplication::readTestfilePathFromSrcRef()
620 auto res = std::vector<std::string>();
621 auto binaryPath = std::string();
622#if defined(CPP_UTILITIES_USE_STANDARD_FILESYSTEM) && defined(PLATFORM_UNIX)
624 binaryPath = std::filesystem::read_symlink(
"/proc/self/exe").parent_path();
626 }
catch (
const std::filesystem::filesystem_error &e) {
627 cerr << Phrases::Warning <<
"Unable to detect binary path for finding \"srcdirref\": " << e.what() << Phrases::EndFlush;
630 const auto srcdirrefPath = binaryPath +
"srcdirref";
633 const auto srcDirContent =
readFile(srcdirrefPath, 2 * 1024);
634 if (srcDirContent.empty()) {
635 cerr << Phrases::Warning <<
"The file \"srcdirref\" is empty." << Phrases::EndFlush;
640 const auto srcPaths = splitStringSimple<std::vector<std::string_view>>(srcDirContent,
"\n");
641 for (
const auto &srcPath : srcPaths) {
642 auto testfilesPath =
argsToString(srcPath,
"/testfiles/");
643 if (dirExists(testfilesPath)) {
644 res.emplace_back(std::move(testfilesPath));
646 cerr << Phrases::Warning
647 <<
"The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist."
648 << Phrases::End <<
"Referenced source directory: " << testfilesPath << endl;
653 }
catch (
const std::ios_base::failure &e) {
654 cerr << Phrases::Warning <<
"The file \"" << srcdirrefPath <<
"\" can not be opened: " << e.what() << Phrases::EndFlush;
const char * executable() const
Returns the name of the current executable.
static constexpr std::size_t varValueCount
Denotes a variable number of values.
const char * firstValue() const
Returns the first parameter value of the first occurrence of the argument.
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.
StringType argsToString(Args &&...args)
std::fstream NativeFileStream