|
1 | 1 | #include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"
|
2 | 2 |
|
3 |
| -#include <QtTest/QtTest> |
4 |
| -#include <QTextStream> |
| 3 | +#include <QTest> |
5 | 4 | #include <QTemporaryFile>
|
| 5 | +#include <QTextStream> |
6 | 6 |
|
7 | 7 | namespace cucumber {
|
8 | 8 | namespace internal {
|
9 | 9 |
|
| 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 | + |
10 | 49 | const InvokeResult QtTestStep::invokeStepBody() {
|
11 |
| - QTemporaryFile file; |
12 |
| - if (!file.open()) { |
| 50 | + const TemporaryFileWrapper file{}; |
| 51 | + if (!file.exists()) { |
13 | 52 | return InvokeResult::failure("Unable to open temporary file needed for this test");
|
14 | 53 | }
|
15 |
| - file.close(); |
16 | 54 |
|
17 | 55 | 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); |
20 | 58 |
|
21 | 59 | if (returnValue == 0) {
|
22 | 60 | return InvokeResult::success();
|
23 | 61 | } else {
|
24 |
| - file.open(); |
25 |
| - QTextStream ts(&file); |
26 |
| - return InvokeResult::failure(ts.readAll().toLocal8Bit()); |
| 62 | + return InvokeResult::failure(file.read().toLocal8Bit()); |
27 | 63 | }
|
28 | 64 | }
|
29 | 65 |
|
|
0 commit comments