3 #include "../conversion/stringbuilder.h"
4 #include "../conversion/stringconversion.h"
5 #include "../io/ansiescapecodes.h"
6 #include "../io/misc.h"
7 #include "../io/nativefilestream.h"
8 #include "../io/path.h"
9 #include "../misc/parseerror.h"
15 #include <initializer_list>
20 #ifdef CPP_UTILITIES_USE_STANDARD_FILESYSTEM
29 #ifdef PLATFORM_WINDOWS
42 bool fileSystemItemExists(
const string &path)
46 return stat(path.data(), &res) == 0;
48 const auto widePath(convertMultiByteToWide(path));
49 if (!widePath.first) {
52 const auto fileType(GetFileAttributesW(widePath.first.get()));
53 return fileType != INVALID_FILE_ATTRIBUTES;
57 bool fileExists(
const string &path)
61 return stat(path.data(), &res) == 0 && !S_ISDIR(res.st_mode);
63 const auto widePath(convertMultiByteToWide(path));
64 if (!widePath.first) {
67 const auto fileType(GetFileAttributesW(widePath.first.get()));
68 return (fileType != INVALID_FILE_ATTRIBUTES) && !(fileType & FILE_ATTRIBUTE_DIRECTORY) && !(fileType & FILE_ATTRIBUTE_DEVICE);
72 bool dirExists(
const string &path)
76 return stat(path.data(), &res) == 0 && S_ISDIR(res.st_mode);
78 const auto widePath(convertMultiByteToWide(path));
79 if (!widePath.first) {
82 const auto fileType(GetFileAttributesW(widePath.first.get()));
83 return (fileType != INVALID_FILE_ATTRIBUTES) && (fileType & FILE_ATTRIBUTE_DIRECTORY);
87 bool makeDir(
const string &path)
90 return mkdir(path.data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
92 const auto widePath(convertMultiByteToWide(path));
93 if (!widePath.first) {
96 return CreateDirectoryW(widePath.first.get(),
nullptr) || GetLastError() == ERROR_ALREADY_EXISTS;
101 TestApplication *TestApplication::s_instance =
nullptr;
115 TestApplication::TestApplication()
125 : m_listArg(
"list",
'l',
"lists available test units")
126 , m_runArg(
"run",
'r',
"runs the tests")
127 , m_testFilesPathArg(
"test-files-path",
'p',
"specifies the path of the directory with test files", {
"path" })
128 , m_applicationPathArg(
"app-path",
'a',
"specifies the path of the application to be tested", {
"path" })
129 , m_workingDirArg(
"working-dir",
'w',
"specifies the directory to store working copies of test files", {
"path" })
130 , m_unitsArg(
"units",
'u',
"specifies the units to test; omit to test all units", {
"unit1",
"unit2",
"unit3" })
134 throw runtime_error(
"only one TestApplication instance allowed at a time");
143 m_runArg.setImplicit(
true);
144 m_runArg.setSubArguments({ &m_testFilesPathArg, &m_applicationPathArg, &m_workingDirArg, &m_unitsArg });
145 m_parser.setMainArguments({ &m_runArg, &m_listArg, &m_parser.noColorArg(), &m_parser.helpArg() });
150 }
catch (
const ParseError &failure) {
157 if (m_parser.helpArg().isPresent()) {
164 if (m_testFilesPathArg.isPresent()) {
165 for (
const char *
const testFilesPath : m_testFilesPathArg.values()) {
166 if (*testFilesPath) {
167 m_testFilesPaths.emplace_back(
argsToString(testFilesPath,
'/'));
169 m_testFilesPaths.emplace_back(
"./");
174 bool hasTestFilePathFromEnv;
175 if (
auto testFilePathFromEnv = readTestfilePathFromEnv(); (hasTestFilePathFromEnv = !testFilePathFromEnv.empty())) {
176 m_testFilesPaths.emplace_back(move(testFilePathFromEnv));
179 if (
auto testFilePathFromSrcDirRef = readTestfilePathFromSrcRef(); !testFilePathFromSrcDirRef.empty()) {
180 m_testFilesPaths.emplace_back(move(testFilePathFromSrcDirRef));
183 m_testFilesPaths.emplace_back(
"./testfiles/");
184 for (
const auto &testFilesPath : m_testFilesPaths) {
185 cerr << testFilesPath <<
'\n';
189 if (m_workingDirArg.isPresent()) {
190 if (*m_workingDirArg.values().front()) {
191 (m_workingDir = m_workingDirArg.values().front()) +=
'/';
195 }
else if (
const char *
const workingDirEnv = getenv(
"WORKING_DIR")) {
196 if (*workingDirEnv) {
200 if ((m_testFilesPathArg.isPresent() && !m_testFilesPathArg.values().empty()) || hasTestFilePathFromEnv) {
201 m_workingDir = m_testFilesPaths.front() +
"workingdir/";
203 m_workingDir =
"./testfiles/workingdir/";
206 cerr <<
"Directory used to store working copies:\n" << m_workingDir <<
'\n';
209 if (
const char *
const profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
210 ofstream(profrawListFile, ios_base::trunc);
221 s_instance =
nullptr;
239 for (
const auto &testFilesPath : m_testFilesPaths) {
240 if (fileExists(path = testFilesPath + relativeTestFilePath)) {
244 throw runtime_error(
"The testfile \"" % relativeTestFilePath %
"\" can not be located. Was looking under:"
245 +
joinStrings(m_testFilesPaths,
"\n",
false,
string(), relativeTestFilePath));
275 const std::string &relativeTestFilePath,
const std::string &relativeWorkingCopyPath,
WorkingCopyMode mode)
const
278 if (!dirExists(m_workingDir) && !makeDir(m_workingDir)) {
279 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": can't create working directory \""
280 << m_workingDir <<
"\"." << Phrases::EndFlush;
286 if (!parts.empty()) {
289 currentLevel.reserve(m_workingDir.size() + relativeWorkingCopyPath.size() + 1);
290 currentLevel.assign(m_workingDir);
291 for (
auto i = parts.cbegin(), end = parts.end() - 1;
i != end; ++
i) {
292 if (currentLevel.back() !=
'/') {
298 if (dirExists(currentLevel) || makeDir(currentLevel)) {
302 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeWorkingCopyPath <<
"\": can't create directory \""
303 << currentLevel <<
"\" (inside working directory)." << Phrases::EndFlush;
310 return m_workingDir + relativeWorkingCopyPath;
314 const auto origFilePath(
testFilePath(relativeTestFilePath));
316 size_t workingCopyPathAttempt = 0;
318 origFile.open(origFilePath, ios_base::in | ios_base::binary);
319 if (origFile.fail()) {
320 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
321 <<
"\": an IO error occurred when opening original file \"" << origFilePath <<
"\"." << Phrases::EndFlush;
322 cerr <<
"error: " << strerror(errno) << endl;
325 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
330 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
332 if (workingCopy.fail()) {
333 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
334 <<
"\": an IO error occurred when opening target file \"" <<
workingCopyPath <<
"\"." << Phrases::EndFlush;
335 cerr <<
"error: " << strerror(errno) << endl;
338 workingCopy << origFile.rdbuf();
340 if (!origFile.fail() && !workingCopy.fail()) {
344 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": ";
345 if (origFile.fail()) {
346 cerr <<
"an IO error occurred when reading original file \"" << origFilePath <<
"\"";
349 if (workingCopy.fail()) {
350 if (origFile.fail()) {
353 cerr <<
" an IO error occurred when writing to target file \"" <<
workingCopyPath <<
"\".";
355 cerr <<
"error: " << strerror(errno) << endl;
364 static int execAppInternal(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout,
365 const std::string &newProfilingPath,
bool enableSearchPath =
false)
368 if (!suppressLogging) {
370 cout <<
'-' <<
' ' << appPath;
372 for (
const char *
const *
i = args + 1; *
i; ++
i) {
380 int coutPipes[2], cerrPipes[2];
383 const auto readCoutPipe = coutPipes[0], writeCoutPipe = coutPipes[1];
384 const auto readCerrPipe = cerrPipes[0], writeCerrPipe = cerrPipes[1];
387 if (
const auto child = fork()) {
389 close(writeCoutPipe);
390 close(writeCerrPipe);
394 throw runtime_error(
"Unable to create fork");
398 struct pollfd fileDescriptorSet[2];
399 fileDescriptorSet[0].fd = readCoutPipe;
400 fileDescriptorSet[1].fd = readCerrPipe;
401 fileDescriptorSet[0].events = fileDescriptorSet[1].events = POLLIN;
410 const auto retpoll = poll(fileDescriptorSet, 2, timeout);
412 throw runtime_error(
"Poll time-out");
415 throw runtime_error(
"Poll failed");
417 if (fileDescriptorSet[0].revents & POLLIN) {
418 const auto count = read(readCoutPipe, buffer,
sizeof(buffer));
420 output.append(buffer,
static_cast<size_t>(count));
422 }
else if (fileDescriptorSet[0].revents & POLLHUP) {
424 fileDescriptorSet[0].fd = -1;
426 if (fileDescriptorSet[1].revents & POLLIN) {
427 const auto count = read(readCerrPipe, buffer,
sizeof(buffer));
429 errors.append(buffer,
static_cast<size_t>(count));
431 }
else if (fileDescriptorSet[1].revents & POLLHUP) {
433 fileDescriptorSet[1].fd = -1;
435 }
while (fileDescriptorSet[0].fd >= 0 || fileDescriptorSet[1].fd >= 0);
445 waitpid(child, &childReturnCode, 0);
446 return childReturnCode;
450 dup2(writeCoutPipe, STDOUT_FILENO);
451 dup2(writeCerrPipe, STDERR_FILENO);
453 close(writeCoutPipe);
455 close(writeCerrPipe);
458 if (!newProfilingPath.empty()) {
459 setenv(
"LLVM_PROFILE_FILE", newProfilingPath.data(),
true);
463 if (enableSearchPath) {
464 execvp(appPath,
const_cast<char *
const *
>(args));
467 execv(appPath,
const_cast<char *
const *
>(args));
469 cerr << Phrases::Error <<
"Unable to execute \"" << appPath <<
"\": execv() failed" << Phrases::EndFlush;
483 int TestApplication::execApp(
const char *
const *args,
string &output,
string &errors,
bool suppressLogging,
int timeout)
const
486 static unsigned int invocationCount = 0;
491 string fallbackAppPath;
496 const char *
const testAppPath = m_parser.
executable();
497 const size_t testAppPathLength = strlen(testAppPath);
498 if (testAppPathLength > 6 && !strcmp(testAppPath + testAppPathLength - 6,
"_tests")) {
499 fallbackAppPath.assign(testAppPath, testAppPathLength - 6);
500 appPath = fallbackAppPath.data();
503 throw runtime_error(
"Unable to execute application to be tested: no application path specified");
508 const auto newProfilingPath = [
appPath] {
509 string newProfilingPath;
510 const char *
const llvmProfileFile = getenv(
"LLVM_PROFILE_FILE");
511 if (!llvmProfileFile) {
512 return newProfilingPath;
515 const char *
const llvmProfileFileEnd = strstr(llvmProfileFile,
".profraw");
516 if (!llvmProfileFileEnd) {
517 return newProfilingPath;
519 const string llvmProfileFileWithoutExtension(llvmProfileFile, llvmProfileFileEnd);
521 const char *appName = strrchr(
appPath,
'/');
522 appName = appName ? appName + 1 :
appPath;
524 newProfilingPath =
argsToString(llvmProfileFileWithoutExtension,
'_', appName, invocationCount,
".profraw");
526 if (
const char *
const profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
527 ofstream(profrawListFile, ios_base::app) << newProfilingPath << endl;
529 return newProfilingPath;
532 return execAppInternal(
appPath, args, output, errors, suppressLogging, timeout, newProfilingPath);
542 int execHelperApp(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout)
544 return execAppInternal(appPath, args, output, errors, suppressLogging, timeout,
string());
557 int execHelperAppInSearchPath(
558 const char *appName,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout)
560 return execAppInternal(appName, args, output, errors, suppressLogging, timeout,
string(),
true);
562 #endif // PLATFORM_UNIX
567 string TestApplication::readTestfilePathFromEnv()
569 const char *
const testFilesPathEnv = getenv(
"TEST_FILE_PATH");
570 if (!testFilesPathEnv || !*testFilesPathEnv) {
581 string TestApplication::readTestfilePathFromSrcRef()
585 std::string binaryPath;
586 #if defined(CPP_UTILITIES_USE_STANDARD_FILESYSTEM) && defined(PLATFORM_UNIX)
588 binaryPath = std::filesystem::read_symlink(
"/proc/self/exe").parent_path();
590 }
catch (
const std::filesystem::filesystem_error &e) {
591 cerr << Phrases::Warning <<
"Unable to detect binary path for finding \"srcdirref\": " << e.what() << Phrases::EndFlush;
596 auto srcDirContent(
readFile(binaryPath +
"srcdirref", 2 * 1024));
597 if (srcDirContent.empty()) {
598 cerr << Phrases::Warning <<
"The file \"srcdirref\" is empty." << Phrases::EndFlush;
601 srcDirContent +=
"/testfiles/";
604 if (!dirExists(srcDirContent)) {
605 cerr << Phrases::Warning
606 <<
"The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist."
607 << Phrases::End <<
"Referenced source directory: " << srcDirContent << endl;
610 return srcDirContent;
612 }
catch (
const std::ios_base::failure &) {
613 cerr << Phrases::Warning <<
"The file \"srcdirref\" can not be opened. It likely just doesn't exist in the working directory."
614 << Phrases::EndFlush;