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>
26 #ifdef PLATFORM_WINDOWS
42 return stat(path.data(), &res) == 0;
44 const auto widePath(convertMultiByteToWide(path));
45 if (!widePath.first) {
48 const auto fileType(GetFileAttributesW(widePath.first.get()));
49 return fileType != INVALID_FILE_ATTRIBUTES;
57 return stat(path.data(), &res) == 0 && !S_ISDIR(res.st_mode);
59 const auto widePath(convertMultiByteToWide(path));
60 if (!widePath.first) {
63 const auto fileType(GetFileAttributesW(widePath.first.get()));
64 return (fileType != INVALID_FILE_ATTRIBUTES) && !(fileType & FILE_ATTRIBUTE_DIRECTORY) && !(fileType & FILE_ATTRIBUTE_DEVICE);
72 return stat(path.data(), &res) == 0 && S_ISDIR(res.st_mode);
74 const auto widePath(convertMultiByteToWide(path));
75 if (!widePath.first) {
78 const auto fileType(GetFileAttributesW(widePath.first.get()));
79 return (fileType != INVALID_FILE_ATTRIBUTES) && (fileType & FILE_ATTRIBUTE_DIRECTORY);
86 return mkdir(path.data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
88 const auto widePath(convertMultiByteToWide(path));
89 if (!widePath.first) {
92 return CreateDirectoryW(widePath.first.get(),
nullptr) || GetLastError() == ERROR_ALREADY_EXISTS;
96 TestApplication *TestApplication::s_instance =
nullptr;
108 TestApplication::TestApplication()
118 : m_listArg(
"list",
'l',
"lists available test units")
119 , m_runArg(
"run",
'r',
"runs the tests")
120 , m_testFilesPathArg(
"test-files-path",
'p',
"specifies the path of the directory with test files", {
"path" })
121 , m_applicationPathArg(
"app-path",
'a',
"specifies the path of the application to be tested", {
"path" })
122 , m_workingDirArg(
"working-dir",
'w',
"specifies the directory to store working copies of test files", {
"path" })
123 , m_unitsArg(
"units",
'u',
"specifies the units to test; omit to test all units", {
"unit1",
"unit2",
"unit3" })
127 throw runtime_error(
"only one TestApplication instance allowed at a time");
136 m_runArg.setImplicit(
true);
137 m_runArg.setSubArguments({ &m_testFilesPathArg, &m_applicationPathArg, &m_workingDirArg, &m_unitsArg });
138 m_parser.setMainArguments({ &m_runArg, &m_listArg, &m_parser.noColorArg(), &m_parser.helpArg() });
143 }
catch (
const ParseError &failure) {
150 if (m_parser.helpArg().isPresent()) {
157 if (m_testFilesPathArg.isPresent()) {
158 for (
const char *
const testFilesPath : m_testFilesPathArg.values()) {
159 if (*testFilesPath) {
160 m_testFilesPaths.emplace_back(
argsToString(testFilesPath,
'/'));
162 m_testFilesPaths.emplace_back(
"./");
167 bool hasTestFilePathFromEnv;
168 if (
auto testFilePathFromEnv = readTestfilePathFromEnv(); (hasTestFilePathFromEnv = !testFilePathFromEnv.empty())) {
169 m_testFilesPaths.emplace_back(move(testFilePathFromEnv));
172 if (
auto testFilePathFromSrcDirRef = readTestfilePathFromSrcRef(); !testFilePathFromSrcDirRef.empty()) {
173 m_testFilesPaths.emplace_back(move(testFilePathFromSrcDirRef));
176 m_testFilesPaths.emplace_back(
"./testfiles/");
177 for (
const auto &testFilesPath : m_testFilesPaths) {
178 cerr << testFilesPath <<
'\n';
182 if (m_workingDirArg.isPresent()) {
183 if (*m_workingDirArg.values().front()) {
184 (m_workingDir = m_workingDirArg.values().front()) +=
'/';
188 }
else if (
const char *workingDirEnv = getenv(
"WORKING_DIR")) {
189 if (*workingDirEnv) {
193 if ((m_testFilesPathArg.isPresent() && !m_testFilesPathArg.values().empty()) || hasTestFilePathFromEnv) {
194 m_workingDir = m_testFilesPaths.front() +
"workingdir/";
196 m_workingDir =
"./testfiles/workingdir/";
199 cerr <<
"Directory used to store working copies:\n" << m_workingDir <<
'\n';
202 if (
const char *profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
203 ofstream(profrawListFile, ios_base::trunc);
207 cerr << TextAttribute::Bold <<
"Executing test cases ..." << Phrases::EndFlush;
215 s_instance =
nullptr;
233 for (
const auto &testFilesPath : m_testFilesPaths) {
234 if (
fileExists(path = testFilesPath + relativeTestFilePath)) {
238 throw runtime_error(
"The testfile \"" % relativeTestFilePath %
"\" can not be located. Was looking under:"
239 +
joinStrings(m_testFilesPaths,
"\n",
false,
string(), relativeTestFilePath));
269 const std::string &relativeTestFilePath,
const std::string &relativeWorkingCopyPath,
WorkingCopyMode mode)
const
273 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": can't create working directory \""
274 << m_workingDir <<
"\"." << Phrases::EndFlush;
280 if (!parts.empty()) {
283 currentLevel.reserve(m_workingDir.size() + relativeWorkingCopyPath.size() + 1);
284 currentLevel.assign(m_workingDir);
285 for (
auto i = parts.cbegin(), end = parts.end() - 1;
i != end; ++
i) {
286 if (currentLevel.back() !=
'/') {
296 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeWorkingCopyPath <<
"\": can't create directory \""
297 << currentLevel <<
"\" (inside working directory)." << Phrases::EndFlush;
304 return m_workingDir + relativeWorkingCopyPath;
308 const auto origFilePath(
testFilePath(relativeTestFilePath));
310 size_t workingCopyPathAttempt = 0;
312 origFile.open(origFilePath, ios_base::in | ios_base::binary);
313 if (origFile.fail()) {
314 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
315 <<
"\": an IO error occurred when opening original file \"" << origFilePath <<
"\"." << Phrases::EndFlush;
316 cerr <<
"error: " << strerror(errno) << endl;
319 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
324 workingCopy.open(
workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
326 if (workingCopy.fail()) {
327 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath
328 <<
"\": an IO error occurred when opening target file \"" <<
workingCopyPath <<
"\"." << Phrases::EndFlush;
329 cerr <<
"error: " << strerror(errno) << endl;
332 workingCopy << origFile.rdbuf();
333 if (!origFile.fail() && !workingCopy.fail()) {
337 cerr << Phrases::Error <<
"Unable to create working copy for \"" << relativeTestFilePath <<
"\": ";
338 if (origFile.fail()) {
339 cerr <<
"an IO error occurred when reading original file \"" << origFilePath <<
"\"";
342 if (workingCopy.fail()) {
343 if (origFile.fail()) {
346 cerr <<
" an IO error occurred when writing to target file \"" <<
workingCopyPath <<
"\".";
348 cerr <<
"error: " << strerror(errno) << endl;
357 int execAppInternal(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout,
358 const std::string &newProfilingPath)
361 if (!suppressLogging) {
363 cout <<
'-' <<
' ' << appPath;
365 for (
const char *
const *
i = args + 1; *
i; ++
i) {
373 int coutPipes[2], cerrPipes[2];
376 const auto readCoutPipe = coutPipes[0], writeCoutPipe = coutPipes[1];
377 const auto readCerrPipe = cerrPipes[0], writeCerrPipe = cerrPipes[1];
380 if (
const auto child = fork()) {
382 close(writeCoutPipe);
383 close(writeCerrPipe);
387 throw runtime_error(
"Unable to create fork");
391 struct pollfd fileDescriptorSet[2];
392 fileDescriptorSet[0].fd = readCoutPipe;
393 fileDescriptorSet[1].fd = readCerrPipe;
394 fileDescriptorSet[0].events = fileDescriptorSet[1].events = POLLIN;
403 const auto retpoll = poll(fileDescriptorSet, 2, timeout);
405 throw runtime_error(
"Poll time-out");
408 throw runtime_error(
"Poll failed");
410 if (fileDescriptorSet[0].revents & POLLIN) {
411 const auto count = read(readCoutPipe, buffer,
sizeof(buffer));
413 output.append(buffer, static_cast<size_t>(count));
415 }
else if (fileDescriptorSet[0].revents & POLLHUP) {
417 fileDescriptorSet[0].fd = -1;
419 if (fileDescriptorSet[1].revents & POLLIN) {
420 const auto count = read(readCerrPipe, buffer,
sizeof(buffer));
422 errors.append(buffer, static_cast<size_t>(count));
424 }
else if (fileDescriptorSet[1].revents & POLLHUP) {
426 fileDescriptorSet[1].fd = -1;
428 }
while (fileDescriptorSet[0].fd >= 0 || fileDescriptorSet[1].fd >= 0);
438 waitpid(child, &childReturnCode, 0);
439 return childReturnCode;
443 dup2(writeCoutPipe, STDOUT_FILENO);
444 dup2(writeCerrPipe, STDERR_FILENO);
446 close(writeCoutPipe);
448 close(writeCerrPipe);
451 if (!newProfilingPath.empty()) {
452 setenv(
"LLVM_PROFILE_FILE", newProfilingPath.data(),
true);
456 execv(appPath, const_cast<char *const *>(args));
457 cerr << Phrases::Error <<
"Unable to execute \"" << appPath <<
"\": execv() failed" << Phrases::EndFlush;
471 int TestApplication::execApp(
const char *
const *args,
string &output,
string &errors,
bool suppressLogging,
int timeout)
const
474 static unsigned int invocationCount = 0;
479 string fallbackAppPath;
484 const char *
const testAppPath = m_parser.
executable();
485 const size_t testAppPathLength = strlen(testAppPath);
486 if (testAppPathLength > 6 && !strcmp(testAppPath + testAppPathLength - 6,
"_tests")) {
487 fallbackAppPath.assign(testAppPath, testAppPathLength - 6);
488 appPath = fallbackAppPath.data();
491 throw runtime_error(
"Unable to execute application to be tested: no application path specified");
496 string newProfilingPath;
497 if (
const char *llvmProfileFile = getenv(
"LLVM_PROFILE_FILE")) {
499 if (
const char *llvmProfileFileEnd = strstr(llvmProfileFile,
".profraw")) {
500 const string llvmProfileFileWithoutExtension(llvmProfileFile, llvmProfileFileEnd);
502 const char *appName = strrchr(
appPath,
'/');
503 appName = appName ? appName + 1 :
appPath;
505 newProfilingPath =
argsToString(llvmProfileFileWithoutExtension,
'_', appName, invocationCount,
".profraw");
507 if (
const char *profrawListFile = getenv(
"LLVM_PROFILE_LIST_FILE")) {
508 ofstream(profrawListFile, ios_base::app) << newProfilingPath << endl;
513 return execAppInternal(
appPath, args, output, errors, suppressLogging, timeout, newProfilingPath);
523 int execHelperApp(
const char *appPath,
const char *
const *args, std::string &output, std::string &errors,
bool suppressLogging,
int timeout)
525 return execAppInternal(appPath, args, output, errors, suppressLogging, timeout,
string());
527 #endif // PLATFORM_UNIX
529 string TestApplication::readTestfilePathFromEnv()
531 const char *
const testFilesPathEnv = getenv(
"TEST_FILE_PATH");
532 if (!testFilesPathEnv || !*testFilesPathEnv) {
538 string TestApplication::readTestfilePathFromSrcRef()
543 auto srcDirContent(
readFile(
"srcdirref", 2 * 1024));
544 if (srcDirContent.empty()) {
545 cerr << Phrases::Warning <<
"The file \"srcdirref\" is empty." << Phrases::EndFlush;
548 srcDirContent +=
"/testfiles/";
552 cerr << Phrases::Warning
553 <<
"The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist."
554 << Phrases::End <<
"Referenced source directory: " << srcDirContent << endl;
557 return srcDirContent;
559 }
catch (
const std::ios_base::failure &) {
560 cerr << Phrases::Warning <<
"The file \"srcdirref\" can not be opened. It likely just doesn't exist in the working directory."
561 << Phrases::EndFlush;