Skip to content

Commit a8fddad

Browse files
committed
Add WorkingCopyMode::Cleanup
1 parent ef37ae4 commit a8fddad

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ set(META_APP_AUTHOR "Martchus")
114114
set(META_APP_URL "https://github.com/${META_APP_AUTHOR}/${META_PROJECT_NAME}")
115115
set(META_APP_DESCRIPTION "Useful C++ classes and routines such as argument parser, IO and conversion utilities")
116116
set(META_VERSION_MAJOR 5)
117-
set(META_VERSION_MINOR 11)
118-
set(META_VERSION_PATCH 4)
117+
set(META_VERSION_MINOR 12)
118+
set(META_VERSION_PATCH 0)
119119

120120
# find required 3rd party libraries
121121
include(3rdParty)

tests/testutils.cpp

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../misc/parseerror.h"
1010

1111
#include <cerrno>
12+
#include <cstdio>
1213
#include <cstdlib>
1314
#include <cstring>
1415
#include <fstream>
@@ -293,10 +294,11 @@ string TestApplication::workingCopyPathAs(
293294
const std::string &relativeTestFilePath, const std::string &relativeWorkingCopyPath, WorkingCopyMode mode) const
294295
{
295296
// ensure working directory is present
297+
auto workingCopyPath = std::string();
296298
if (!dirExists(m_workingDir) && !makeDir(m_workingDir)) {
297299
cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath << "\": can't create working directory \""
298300
<< m_workingDir << "\"." << Phrases::EndFlush;
299-
return string();
301+
return workingCopyPath;
300302
}
301303

302304
// ensure subdirectory exists
@@ -319,26 +321,37 @@ string TestApplication::workingCopyPathAs(
319321
// fail otherwise
320322
cerr << Phrases::Error << "Unable to create working copy for \"" << relativeWorkingCopyPath << "\": can't create directory \""
321323
<< currentLevel << "\" (inside working directory)." << Phrases::EndFlush;
322-
return string();
324+
return workingCopyPath;
323325
}
324326
}
325327

326-
// just return the path if we don't want to actually create a copy
327-
if (mode == WorkingCopyMode::NoCopy) {
328-
return m_workingDir + relativeWorkingCopyPath;
328+
workingCopyPath = m_workingDir + relativeWorkingCopyPath;
329+
switch (mode) {
330+
case WorkingCopyMode::NoCopy:
331+
// just return the path if we don't want to actually create a copy
332+
return workingCopyPath;
333+
case WorkingCopyMode::Cleanup:
334+
// ensure the file does not exist in cleanup mode
335+
if (std::remove(workingCopyPath.data()) != 0 && errno != ENOENT) {
336+
const auto error = std::strerror(errno);
337+
cerr << Phrases::Error << "Unable to delete \"" << workingCopyPath << "\": " << error << Phrases::EndFlush;
338+
workingCopyPath.clear();
339+
}
340+
return workingCopyPath;
341+
default:;
329342
}
330343

331344
// copy the file
332-
const auto origFilePath(testFilePath(relativeTestFilePath));
333-
auto workingCopyPath(m_workingDir + relativeWorkingCopyPath);
345+
const auto origFilePath = testFilePath(relativeTestFilePath);
334346
size_t workingCopyPathAttempt = 0;
335347
NativeFileStream origFile, workingCopy;
336348
origFile.open(origFilePath, ios_base::in | ios_base::binary);
337349
if (origFile.fail()) {
338350
cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath
339351
<< "\": an IO error occurred when opening original file \"" << origFilePath << "\"." << Phrases::EndFlush;
340-
cerr << "error: " << strerror(errno) << endl;
341-
return string();
352+
cerr << "error: " << std::strerror(errno) << endl;
353+
workingCopyPath.clear();
354+
return workingCopyPath;
342355
}
343356
workingCopy.open(workingCopyPath, ios_base::out | ios_base::binary | ios_base::trunc);
344357
while (workingCopy.fail() && fileSystemItemExists(workingCopyPath)) {
@@ -351,7 +364,8 @@ string TestApplication::workingCopyPathAs(
351364
cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath
352365
<< "\": an IO error occurred when opening target file \"" << workingCopyPath << "\"." << Phrases::EndFlush;
353366
cerr << "error: " << strerror(errno) << endl;
354-
return string();
367+
workingCopyPath.clear();
368+
return workingCopyPath;
355369
}
356370
workingCopy << origFile.rdbuf();
357371
workingCopy.close();
@@ -362,7 +376,8 @@ string TestApplication::workingCopyPathAs(
362376
cerr << Phrases::Error << "Unable to create working copy for \"" << relativeTestFilePath << "\": ";
363377
if (origFile.fail()) {
364378
cerr << "an IO error occurred when reading original file \"" << origFilePath << "\"";
365-
return string();
379+
workingCopyPath.clear();
380+
return workingCopyPath;
366381
}
367382
if (workingCopy.fail()) {
368383
if (origFile.fail()) {
@@ -371,7 +386,8 @@ string TestApplication::workingCopyPathAs(
371386
cerr << " an IO error occurred when writing to target file \"" << workingCopyPath << "\".";
372387
}
373388
cerr << "error: " << strerror(errno) << endl;
374-
return string();
389+
workingCopyPath.clear();
390+
return workingCopyPath;
375391
}
376392

377393
#ifdef PLATFORM_UNIX

tests/testutils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace CppUtilities {
1515
*/
1616
enum class WorkingCopyMode {
1717
CreateCopy, /**< a working copy of the test file is created */
18-
NoCopy /**< only the directory for the working copy is created but not the test file itself */
18+
NoCopy, /**< only the directory for the working copy is created but not the test file itself */
19+
Cleanup, /**< the directory for the working copy is created if needed or a previously existing file is deleted */
1920
};
2021

2122
class CPP_UTILITIES_EXPORT TestApplication {

0 commit comments

Comments
 (0)