Skip to content

Commit

Permalink
Merge pull request #305 from iQiliO/windows-qttestdriver
Browse files Browse the repository at this point in the history
Windows qttestdriver
  • Loading branch information
ursfassler authored Sep 27, 2024
2 parents 36563d3 + c142ff1 commit fb1c702
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions src/drivers/QtTestDriver.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,65 @@
#include "cucumber-cpp/internal/drivers/QtTestDriver.hpp"

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

namespace cucumber {
namespace internal {

/**
* Wraps the QTemporaryFile for Windows.
*
* On Windows. the file can not be written as long as QTemporaryFile keeps it open.
*/
class TemporaryFileWrapper {
public:
~TemporaryFileWrapper() {
QFile::remove(filename);
}

bool exists() const {
return !filename.isEmpty();
}

QString name() const {
return filename;
}

QString read() const {
QFile file{filename};
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QString();
QTextStream in(&file);
return in.readAll();
}

private:
const QString filename{getTmpFileName()};

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

const InvokeResult QtTestStep::invokeStepBody() {
QTemporaryFile file;
if (!file.open()) {
const TemporaryFileWrapper file{};
if (!file.exists()) {
return InvokeResult::failure("Unable to open temporary file needed for this test");
}
file.close();

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

if (returnValue == 0) {
return InvokeResult::success();
} else {
file.open();
QTextStream ts(&file);
return InvokeResult::failure(ts.readAll().toLocal8Bit());
return InvokeResult::failure(file.read().toLocal8Bit());
}
}

Expand Down

0 comments on commit fb1c702

Please sign in to comment.