Improve coding style in test utilities

This commit is contained in:
Martchus 2017-10-24 01:02:07 +02:00
parent 2203e0a335
commit dab05bdade
2 changed files with 86 additions and 82 deletions

View File

@ -1,5 +1,5 @@
#ifndef CPPUNIT_H #ifndef TESTUTILS_CPPUNIT_H
#define CPPUNIT_H #define TESTUTILS_CPPUNIT_H
#include "./testutils.h" #include "./testutils.h"
@ -19,7 +19,10 @@ using namespace CPPUNIT_NS;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
TestApplication testApp(argc, argv); TestApplication testApp(argc, argv);
if (testApp) { if (!testApp) {
return -1;
}
// run tests // run tests
TextUi::TestRunner runner; TextUi::TestRunner runner;
TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry(); TestFactoryRegistry &registry = TestFactoryRegistry::getRegistry();
@ -38,9 +41,6 @@ int main(int argc, char **argv)
} }
} }
return !runner.run(string(), false); return !runner.run(string(), false);
} else {
return -1;
}
} }
#endif // CPPUNIT_H #endif // TESTUTILS_CPPUNIT_H

View File

@ -3,6 +3,7 @@
#include "../application/failure.h" #include "../application/failure.h"
#include "../conversion/stringbuilder.h" #include "../conversion/stringbuilder.h"
#include "../conversion/stringconversion.h" #include "../conversion/stringconversion.h"
#include "../io/ansiescapecodes.h"
#include "../io/catchiofailure.h" #include "../io/catchiofailure.h"
#include <cstdlib> #include <cstdlib>
@ -22,6 +23,7 @@
using namespace std; using namespace std;
using namespace ApplicationUtilities; using namespace ApplicationUtilities;
using namespace ConversionUtilities; using namespace ConversionUtilities;
using namespace EscapeCodes;
using namespace IoUtilities; using namespace IoUtilities;
/*! /*!
@ -77,6 +79,11 @@ TestApplication::TestApplication(int argc, char **argv)
// parse arguments // parse arguments
try { try {
m_parser.parseArgs(argc, argv); m_parser.parseArgs(argc, argv);
} catch (const Failure &failure) {
cerr << failure;
m_valid = false;
return;
}
// print help // print help
if (m_helpArg.isPresent()) { if (m_helpArg.isPresent()) {
@ -127,11 +134,7 @@ TestApplication::TestApplication(int argc, char **argv)
} }
m_valid = true; m_valid = true;
cerr << "Executing test cases ..." << endl; cerr << TextAttribute::Bold << "Executing test cases ..." << Phrases::EndFlush;
} catch (const Failure &failure) {
cerr << "Invalid arguments specified: " << failure.what() << endl;
m_valid = false;
}
} }
/*! /*!
@ -171,7 +174,7 @@ string TestApplication::testFilePath(const string &name) const
file.clear(); file.clear();
file.open(path = "./testfiles/" + name, ios_base::in); file.open(path = "./testfiles/" + name, ios_base::in);
if (!file.good()) { if (!file.good()) {
cerr << "Warning: The testfile \"" << path << "\" can not be located." << endl; cerr << Phrases::Warning << "The testfile \"" << path << "\" can not be located." << Phrases::EndFlush;
} }
return path; return path;
} }
@ -192,7 +195,7 @@ string TestApplication::workingCopyPathMode(const string &name, WorkingCopyMode
struct stat currentStat; struct stat currentStat;
if (stat(m_workingDir.c_str(), &currentStat) || !S_ISDIR(currentStat.st_mode)) { if (stat(m_workingDir.c_str(), &currentStat) || !S_ISDIR(currentStat.st_mode)) {
if (mkdir(m_workingDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { if (mkdir(m_workingDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
cerr << "Unable to create working copy for \"" << name << "\": can't create working directory." << endl; cerr << Phrases::Error << "Unable to create working copy for \"" << name << "\": can't create working directory." << Phrases::EndFlush;
return string(); return string();
} }
} }
@ -208,7 +211,8 @@ string TestApplication::workingCopyPathMode(const string &name, WorkingCopyMode
currentLevel += *i; currentLevel += *i;
if (stat(currentLevel.c_str(), &currentStat) || !S_ISDIR(currentStat.st_mode)) { if (stat(currentLevel.c_str(), &currentStat) || !S_ISDIR(currentStat.st_mode)) {
if (mkdir(currentLevel.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) { if (mkdir(currentLevel.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
cerr << "Unable to create working copy for \"" << name << "\": can't create working directory." << endl; cerr << Phrases::Error << "Unable to create working copy for \"" << name << "\": can't create working directory."
<< Phrases::EndFlush;
return string(); return string();
} }
} }
@ -225,7 +229,7 @@ string TestApplication::workingCopyPathMode(const string &name, WorkingCopyMode
return path; return path;
} catch (...) { } catch (...) {
catchIoFailure(); catchIoFailure();
cerr << "Unable to create working copy for \"" << name << "\": an IO error occured." << endl; cerr << Phrases::Error << "Unable to create working copy for \"" << name << "\": an IO error occured." << Phrases::EndFlush;
} }
} else { } else {
return m_workingDir + name; return m_workingDir + name;
@ -288,7 +292,7 @@ int execAppInternal(const char *appPath, const char *const *args, std::string &o
// poll succeeds // poll succeeds
if (fileDescriptorSet[0].revents & POLLIN) { if (fileDescriptorSet[0].revents & POLLIN) {
if ((count = read(readCoutPipe, buffer, sizeof(buffer))) > 0) { if ((count = read(readCoutPipe, buffer, sizeof(buffer))) > 0) {
output.append(buffer, count); output.append(buffer, static_cast<size_t>(count));
} }
} else if (fileDescriptorSet[0].revents & POLLHUP) { } else if (fileDescriptorSet[0].revents & POLLHUP) {
close(readCoutPipe); close(readCoutPipe);
@ -296,7 +300,7 @@ int execAppInternal(const char *appPath, const char *const *args, std::string &o
} }
if (fileDescriptorSet[1].revents & POLLIN) { if (fileDescriptorSet[1].revents & POLLIN) {
if ((count = read(readCerrPipe, buffer, sizeof(buffer))) > 0) { if ((count = read(readCerrPipe, buffer, sizeof(buffer))) > 0) {
errors.append(buffer, count); errors.append(buffer, static_cast<size_t>(count));
} }
} else if (fileDescriptorSet[1].revents & POLLHUP) { } else if (fileDescriptorSet[1].revents & POLLHUP) {
close(readCerrPipe); close(readCerrPipe);
@ -333,7 +337,7 @@ int execAppInternal(const char *appPath, const char *const *args, std::string &o
// -> execute application // -> execute application
execv(appPath, const_cast<char *const *>(args)); execv(appPath, const_cast<char *const *>(args));
cerr << "Unable to execute \"" << appPath << "\": execv() failed" << endl; cerr << Phrases::Error << "Unable to execute \"" << appPath << "\": execv() failed" << Phrases::EndFlush;
exit(-101); exit(-101);
} }
} }