Skip to content

Commit a4facb1

Browse files
kreuzbergerursfassler
authored andcommitted
simplify TemporaryFileWrapper
1 parent a8bdfab commit a4facb1

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

src/drivers/QtTestDriver.cpp

+13-29
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,19 @@
11
#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"
22

3+
#include <QTest>
34
#include <QTemporaryFile>
45
#include <QTextStream>
5-
#include <QtTest/QtTest>
66

77
namespace cucumber {
88
namespace internal {
99

1010
/**
11-
* Wraps the QTemporaryFile creation
11+
* Wraps the QTemporaryFile for Windows.
1212
*
13-
* On Windows the file could not be written as long as QTemporaryFile owner of the file.
13+
* On Windows. the file can not be written as long as QTemporaryFile keeps it open.
1414
*/
1515
class TemporaryFileWrapper {
1616
public:
17-
static TemporaryFileWrapper create() {
18-
QTemporaryFile tempFile(QString("%1/%2_%3")
19-
.arg(
20-
QDir::tempPath(),
21-
qApp->applicationName().isEmpty() ? "qt_temp"
22-
: qApp->applicationName()
23-
)
24-
.arg(qApp->applicationPid()));
25-
26-
if (!tempFile.open()) {
27-
return {};
28-
}
29-
30-
return {tempFile.fileName() + ".txt"};
31-
}
32-
33-
TemporaryFileWrapper() :
34-
filename{} {
35-
}
36-
37-
TemporaryFileWrapper(QString filename) :
38-
filename{filename} {
39-
}
40-
4117
~TemporaryFileWrapper() {
4218
QFile::remove(filename);
4319
}
@@ -59,11 +35,19 @@ class TemporaryFileWrapper {
5935
}
6036

6137
private:
62-
QString filename;
38+
const QString filename{getTmpFileName()};
39+
40+
static QString getTmpFileName() {
41+
QTemporaryFile tempFile{};
42+
if (!tempFile.open()) {
43+
return {};
44+
}
45+
return tempFile.fileName() + ".txt";
46+
}
6347
};
6448

6549
const InvokeResult QtTestStep::invokeStepBody() {
66-
const auto file = TemporaryFileWrapper::create();
50+
const TemporaryFileWrapper file{};
6751
if (!file.exists()) {
6852
return InvokeResult::failure("Unable to open temporary file needed for this test");
6953
}

0 commit comments

Comments
 (0)