Skip to content

Commit fb1c702

Browse files
authored
Merge pull request #305 from iQiliO/windows-qttestdriver
Windows qttestdriver
2 parents 36563d3 + c142ff1 commit fb1c702

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

src/drivers/QtTestDriver.cpp

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

3-
#include <QtTest/QtTest>
4-
#include <QTextStream>
3+
#include <QTest>
54
#include <QTemporaryFile>
5+
#include <QTextStream>
66

77
namespace cucumber {
88
namespace internal {
99

10+
/**
11+
* Wraps the QTemporaryFile for Windows.
12+
*
13+
* On Windows. the file can not be written as long as QTemporaryFile keeps it open.
14+
*/
15+
class TemporaryFileWrapper {
16+
public:
17+
~TemporaryFileWrapper() {
18+
QFile::remove(filename);
19+
}
20+
21+
bool exists() const {
22+
return !filename.isEmpty();
23+
}
24+
25+
QString name() const {
26+
return filename;
27+
}
28+
29+
QString read() const {
30+
QFile file{filename};
31+
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
32+
return QString();
33+
QTextStream in(&file);
34+
return in.readAll();
35+
}
36+
37+
private:
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+
}
47+
};
48+
1049
const InvokeResult QtTestStep::invokeStepBody() {
11-
QTemporaryFile file;
12-
if (!file.open()) {
50+
const TemporaryFileWrapper file{};
51+
if (!file.exists()) {
1352
return InvokeResult::failure("Unable to open temporary file needed for this test");
1453
}
15-
file.close();
1654

1755
QtTestObject testObject{this};
18-
const QStringList args{"test", "-o", file.fileName() + ",tap"};
19-
int returnValue = QTest::qExec(&testObject, args);
56+
const QStringList args{"test", "-o", file.name() + ",tap"};
57+
const int returnValue = QTest::qExec(&testObject, args);
2058

2159
if (returnValue == 0) {
2260
return InvokeResult::success();
2361
} else {
24-
file.open();
25-
QTextStream ts(&file);
26-
return InvokeResult::failure(ts.readAll().toLocal8Bit());
62+
return InvokeResult::failure(file.read().toLocal8Bit());
2763
}
2864
}
2965

0 commit comments

Comments
 (0)