Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows qttestdriver #305

Merged
merged 7 commits into from
Sep 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading