Skip to content

Commit

Permalink
simplify TemporaryFileWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
kreuzberger authored and ursfassler committed Aug 1, 2024
1 parent a8bdfab commit a4facb1
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions src/drivers/QtTestDriver.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"

#include <QTest>
#include <QTemporaryFile>
#include <QTextStream>
#include <QtTest/QtTest>

namespace cucumber {
namespace internal {

/**
* Wraps the QTemporaryFile creation
* Wraps the QTemporaryFile for Windows.
*
* On Windows the file could not be written as long as QTemporaryFile owner of the file.
* On Windows. the file can not be written as long as QTemporaryFile keeps it open.
*/
class TemporaryFileWrapper {
public:
static TemporaryFileWrapper create() {
QTemporaryFile tempFile(QString("%1/%2_%3")
.arg(
QDir::tempPath(),
qApp->applicationName().isEmpty() ? "qt_temp"
: qApp->applicationName()
)
.arg(qApp->applicationPid()));

if (!tempFile.open()) {
return {};
}

return {tempFile.fileName() + ".txt"};
}

TemporaryFileWrapper() :
filename{} {
}

TemporaryFileWrapper(QString filename) :
filename{filename} {
}

~TemporaryFileWrapper() {
QFile::remove(filename);
}
Expand All @@ -59,11 +35,19 @@ class TemporaryFileWrapper {
}

private:
QString filename;
const QString filename{getTmpFileName()};

static QString getTmpFileName() {
QTemporaryFile tempFile{};
if (!tempFile.open()) {
return {};
}
return tempFile.fileName() + ".txt";
}
};

const InvokeResult QtTestStep::invokeStepBody() {
const auto file = TemporaryFileWrapper::create();
const TemporaryFileWrapper file{};
if (!file.exists()) {
return InvokeResult::failure("Unable to open temporary file needed for this test");
}
Expand Down

0 comments on commit a4facb1

Please sign in to comment.