From a8fddad8043b9adf2924fc52a32ea4dcf10281e9 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 8 Jan 2022 23:35:01 +0100 Subject: [PATCH] Add WorkingCopyMode::Cleanup --- CMakeLists.txt | 4 ++-- tests/testutils.cpp | 40 ++++++++++++++++++++++++++++------------ tests/testutils.h | 3 ++- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 062b197..99f8442 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,8 +114,8 @@ set(META_APP_AUTHOR "Martchus") set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}") set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities") set(META_VERSION_MAJOR 5) -set(META_VERSION_MINOR 11) -set(META_VERSION_PATCH 4) +set(META_VERSION_MINOR 12) +set(META_VERSION_PATCH 0) # find required 3rd party libraries include(3rdParty) diff --git a/tests/testutils.cpp b/tests/testutils.cpp index 6178ae6..23cd51e 100644 --- a/tests/testutils.cpp +++ b/tests/testutils.cpp @@ -9,6 +9,7 @@ #include "../misc/parseerror.h" #include +#include #include #include #include @@ -293,10 +294,11 @@ string TestApplication::workingCopyPathAs( const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode) const { // ensure working directory is present + auto workingCopyPath = std::string(); if (!dirExists(m_workingDir) && !makeDir(m_workingDir)) { cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath << "\": can't create working directory \"" << m_workingDir << "\"." << Phrases::EndFlush; - return string(); + return workingCopyPath; } // ensure subdirectory exists @@ -319,26 +321,37 @@ string TestApplication::workingCopyPathAs( // fail otherwise cerr << Phrases::Error << "Unable to create working copy for \"" << relativeWorkingCopyPath << "\": can't create directory \"" << currentLevel << "\" (inside working directory)." << Phrases::EndFlush; - return string(); + return workingCopyPath; } } - // just return the path if we don't want to actually create a copy - if (mode == WorkingCopyMode::NoCopy) { - return m_workingDir + relativeWorkingCopyPath; + workingCopyPath = m_workingDir + relativeWorkingCopyPath; + switch (mode) { + case WorkingCopyMode::NoCopy: + // just return the path if we don't want to actually create a copy + return workingCopyPath; + case WorkingCopyMode::Cleanup: + // ensure the file does not exist in cleanup mode + if (std::remove(workingCopyPath.data()) != 0 && errno != ENOENT) { + const auto error = std::strerror(errno); + cerr << Phrases::Error << "Unable to delete \"" << workingCopyPath << "\": " << error << Phrases::EndFlush; + workingCopyPath.clear(); + } + return workingCopyPath; + default:; } // copy the file - const auto origFilePath(testFilePath(relativeTestFilePath)); - auto workingCopyPath(m_workingDir + relativeWorkingCopyPath); + const auto origFilePath = testFilePath(relativeTestFilePath); size_t workingCopyPathAttempt = 0; NativeFileStream origFile, workingCopy; origFile.open(origFilePath, ios_base::in | ios_base::binary); if (origFile.fail()) { cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath << "\": an IO error occurred when opening original file \"" << origFilePath << "\"." << Phrases::EndFlush; - cerr << "error: " << strerror(errno) << endl; - return string(); + cerr << "error: " << std::strerror(errno) << endl; + workingCopyPath.clear(); + return workingCopyPath; } workingCopy.open(workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc); while (workingCopy.fail() && fileSystemItemExists(workingCopyPath)) { @@ -351,7 +364,8 @@ string TestApplication::workingCopyPathAs( cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath << "\": an IO error occurred when opening target file \"" << workingCopyPath << "\"." << Phrases::EndFlush; cerr << "error: " << strerror(errno) << endl; - return string(); + workingCopyPath.clear(); + return workingCopyPath; } workingCopy << origFile.rdbuf(); workingCopy.close(); @@ -362,7 +376,8 @@ string TestApplication::workingCopyPathAs( cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath << "\": "; if (origFile.fail()) { cerr << "an IO error occurred when reading original file \"" << origFilePath << "\""; - return string(); + workingCopyPath.clear(); + return workingCopyPath; } if (workingCopy.fail()) { if (origFile.fail()) { @@ -371,7 +386,8 @@ string TestApplication::workingCopyPathAs( cerr << " an IO error occurred when writing to target file \"" << workingCopyPath << "\"."; } cerr << "error: " << strerror(errno) << endl; - return string(); + workingCopyPath.clear(); + return workingCopyPath; } #ifdef PLATFORM_UNIX diff --git a/tests/testutils.h b/tests/testutils.h index 671743a..122a376 100644 --- a/tests/testutils.h +++ b/tests/testutils.h @@ -15,7 +15,8 @@ namespace CppUtilities { */ enum class WorkingCopyMode { CreateCopy, /**< a working copy of the test file is created */ - NoCopy /**< only the directory for the working copy is created but not the test file itself */ + NoCopy, /**< only the directory for the working copy is created but not the test file itself */ + Cleanup, /**< the directory for the working copy is created if needed or a previously existing file is deleted */ }; class CPP_UTILITIES_EXPORT TestApplication {