From 40da5cf32718fe632f983886a7a875bc8febc941 Mon Sep 17 00:00:00 2001 From: Joerg Kreuzberger Date: Mon, 8 Apr 2024 10:27:38 +0200 Subject: [PATCH 1/7] adding windows test driver fix --- src/drivers/QtTestDriver.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp index e95e32fd..3bc54cb7 100644 --- a/src/drivers/QtTestDriver.cpp +++ b/src/drivers/QtTestDriver.cpp @@ -8,23 +8,29 @@ namespace cucumber { namespace internal { const InvokeResult QtTestStep::invokeStepBody() { - QTemporaryFile file; - if (!file.open()) { - return InvokeResult::failure("Unable to open temporary file needed for this test"); + QString file_name; + { + QTemporaryFile file; + if (!file.open()) { + return InvokeResult::failure("Unable to open temporary file needed for this test"); + } + file.close(); } - file.close(); - QtTestObject testObject{this}; - const QStringList args{"test", "-o", file.fileName() + ",tap"}; - int returnValue = QTest::qExec(&testObject, args); + file_name += ".txt"; - if (returnValue == 0) { - return InvokeResult::success(); - } else { - file.open(); - QTextStream ts(&file); - return InvokeResult::failure(ts.readAll().toLocal8Bit()); + QtTestObject testObject{this}; + const QStringList args{"test", "-o", file_name + ",tap"}; + const int returnValue = QTest::qExec(&testObject, args); + const bool ok = returnValue == 0; + std::string error_text; + QFile file( file_name ); + if(!ok) { + file.open( QIODevice::ReadOnly ); + error_text = QString::fromLocal8Bit( file.readAll() ).toStdString(); } + //file.remove(); + return ok ? InvokeResult::success() : InvokeResult::failure( error_text ); } } From 114d5a342233d27b73dcb224cfb35a1ab877a871 Mon Sep 17 00:00:00 2001 From: Joerg Kreuzberger Date: Thu, 11 Apr 2024 09:04:44 +0200 Subject: [PATCH 2/7] integrate changes from review --- src/drivers/QtTestDriver.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp index 3bc54cb7..b91445f6 100644 --- a/src/drivers/QtTestDriver.cpp +++ b/src/drivers/QtTestDriver.cpp @@ -10,27 +10,36 @@ namespace internal { const InvokeResult QtTestStep::invokeStepBody() { QString file_name; { - QTemporaryFile file; + QTemporaryFile file(QString("%1/%2_%3") + .arg( + QDir::tempPath(), + qApp->applicationName().isEmpty() ? "qt_temp" + : qApp->applicationName() + ) + .arg(qApp->applicationPid())); if (!file.open()) { return InvokeResult::failure("Unable to open temporary file needed for this test"); } - file.close(); + file_name = file.fileName() + ".txt"; } - file_name += ".txt"; - QtTestObject testObject{this}; const QStringList args{"test", "-o", file_name + ",tap"}; const int returnValue = QTest::qExec(&testObject, args); const bool ok = returnValue == 0; - std::string error_text; - QFile file( file_name ); - if(!ok) { - file.open( QIODevice::ReadOnly ); - error_text = QString::fromLocal8Bit( file.readAll() ).toStdString(); + + if (ok) { + QFile::remove(file_name); + return InvokeResult::success(); + } else { + std::string error_text; + QFile file(file_name); + file.open(QIODevice::ReadOnly); + error_text = QString::fromLocal8Bit(file.readAll()).toStdString(); + file.close(); + file.remove(); + return InvokeResult::failure(error_text); } - //file.remove(); - return ok ? InvokeResult::success() : InvokeResult::failure( error_text ); } } From 9d89a24238aac54047183fc5b12a419517e6d36a Mon Sep 17 00:00:00 2001 From: Joerg Kreuzberger Date: Fri, 12 Apr 2024 09:01:25 +0200 Subject: [PATCH 3/7] integrate QTemporaryTestDriver suggestion --- src/drivers/QtTestDriver.cpp | 72 ++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp index b91445f6..6964fffd 100644 --- a/src/drivers/QtTestDriver.cpp +++ b/src/drivers/QtTestDriver.cpp @@ -7,38 +7,64 @@ namespace cucumber { namespace internal { -const InvokeResult QtTestStep::invokeStepBody() { - QString file_name; + +// wraps the QTemporaryFile creation +// on Windows the file could not be written as long as QTemporaryFile owner of the file. +class TemporaryFileWrapper { +public: + static TemporaryFileWrapper create() { - QTemporaryFile file(QString("%1/%2_%3") - .arg( - QDir::tempPath(), - qApp->applicationName().isEmpty() ? "qt_temp" - : qApp->applicationName() - ) - .arg(qApp->applicationPid())); - if (!file.open()) { - return InvokeResult::failure("Unable to open temporary file needed for this test"); + QTemporaryFile tempFile{}; + + if(!tempFile.open()) { + return {}; } - file_name = file.fileName() + ".txt"; + + return {tempFile.fileName() + ".txt"}; + } + + TemporaryFileWrapper(): filename{} { + } + + TemporaryFileWrapper(QString filename): filename{filename} { + } + + ~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: + QString filename; +}; + +const InvokeResult QtTestStep::invokeStepBody() { + const auto file = TemporaryFileWrapper::create(); + if (!file.exists()) { + return InvokeResult::failure("Unable to open temporary file needed for this test"); } QtTestObject testObject{this}; - const QStringList args{"test", "-o", file_name + ",tap"}; + const QStringList args{"test", "-o", file.name() + ",tap"}; const int returnValue = QTest::qExec(&testObject, args); - const bool ok = returnValue == 0; - if (ok) { - QFile::remove(file_name); + if (returnValue == 0) { return InvokeResult::success(); } else { - std::string error_text; - QFile file(file_name); - file.open(QIODevice::ReadOnly); - error_text = QString::fromLocal8Bit(file.readAll()).toStdString(); - file.close(); - file.remove(); - return InvokeResult::failure(error_text); + return InvokeResult::failure(file.read().toLocal8Bit()); } } From ad5ad6c05d2a5e5c7c9010ea29f700bf8a3c13ae Mon Sep 17 00:00:00 2001 From: Joerg Kreuzberger Date: Fri, 12 Apr 2024 09:02:30 +0200 Subject: [PATCH 4/7] fix clang-format of driver --- src/drivers/QtTestDriver.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp index 6964fffd..42701fda 100644 --- a/src/drivers/QtTestDriver.cpp +++ b/src/drivers/QtTestDriver.cpp @@ -1,46 +1,51 @@ #include "cucumber-cpp/internal/drivers/QtTestDriver.hpp" -#include -#include #include +#include +#include namespace cucumber { namespace internal { - -// wraps the QTemporaryFile creation -// on Windows the file could not be written as long as QTemporaryFile owner of the file. +/** + * Wraps the QTemporaryFile creation + * + * On Windows the file could not be written as long as QTemporaryFile owner of the file. + */ class TemporaryFileWrapper { public: - static TemporaryFileWrapper create() - { + static TemporaryFileWrapper create() { QTemporaryFile tempFile{}; - if(!tempFile.open()) { + if (!tempFile.open()) { return {}; } return {tempFile.fileName() + ".txt"}; } - TemporaryFileWrapper(): filename{} { + TemporaryFileWrapper() : + filename{} { } - TemporaryFileWrapper(QString filename): filename{filename} { + TemporaryFileWrapper(QString filename) : + filename{filename} { } ~TemporaryFileWrapper() { QFile::remove(filename); } - bool exists() const { return !filename.isEmpty(); } + bool exists() const { + return !filename.isEmpty(); + } QString name() const { return filename; } QString read() const { - QFile file{ filename }; + QFile file{filename}; if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return QString(); QTextStream in(&file); From c4143f9708aa5f48c5d24f47dd75b03fec8d06f6 Mon Sep 17 00:00:00 2001 From: Joerg Kreuzberger Date: Fri, 12 Apr 2024 09:15:27 +0200 Subject: [PATCH 5/7] add pid and app name to temp file template --- src/drivers/QtTestDriver.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp index 42701fda..b3e9e046 100644 --- a/src/drivers/QtTestDriver.cpp +++ b/src/drivers/QtTestDriver.cpp @@ -15,7 +15,15 @@ namespace internal { class TemporaryFileWrapper { public: static TemporaryFileWrapper create() { - QTemporaryFile tempFile{}; + QTemporaryFile tempFile( + QString("%1/%2_%3") + .arg( + QDir::tempPath(), + qApp->applicationName().isEmpty() ? "qt_temp" + : qApp->applicationName() + ) + .arg(qApp->applicationPid()) + ); if (!tempFile.open()) { return {}; From a8bdfabc1645f410ebf576637fd9a8902caebc53 Mon Sep 17 00:00:00 2001 From: Joerg Kreuzberger Date: Fri, 12 Apr 2024 09:16:27 +0200 Subject: [PATCH 6/7] clang-format changed QtTestDriver --- src/drivers/QtTestDriver.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp index b3e9e046..cb230094 100644 --- a/src/drivers/QtTestDriver.cpp +++ b/src/drivers/QtTestDriver.cpp @@ -15,15 +15,13 @@ namespace internal { class TemporaryFileWrapper { public: static TemporaryFileWrapper create() { - QTemporaryFile tempFile( - QString("%1/%2_%3") - .arg( - QDir::tempPath(), - qApp->applicationName().isEmpty() ? "qt_temp" - : qApp->applicationName() - ) - .arg(qApp->applicationPid()) - ); + QTemporaryFile tempFile(QString("%1/%2_%3") + .arg( + QDir::tempPath(), + qApp->applicationName().isEmpty() ? "qt_temp" + : qApp->applicationName() + ) + .arg(qApp->applicationPid())); if (!tempFile.open()) { return {}; From c142ff1dafa17fffc5cabd948922f95eba959b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Urs=20F=C3=A4ssler?= Date: Thu, 11 Apr 2024 16:25:52 +0200 Subject: [PATCH 7/7] simplify TemporaryFileWrapper --- src/drivers/QtTestDriver.cpp | 42 +++++++++++------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/src/drivers/QtTestDriver.cpp b/src/drivers/QtTestDriver.cpp index cb230094..5e4e40d6 100644 --- a/src/drivers/QtTestDriver.cpp +++ b/src/drivers/QtTestDriver.cpp @@ -1,43 +1,19 @@ #include "cucumber-cpp/internal/drivers/QtTestDriver.hpp" +#include #include #include -#include namespace cucumber { namespace internal { /** - * Wraps the QTemporaryFile creation + * Wraps the QTemporaryFile for Windows. * - * On Windows the file could not be written as long as QTemporaryFile owner of the file. + * On Windows. the file can not be written as long as QTemporaryFile keeps it open. */ class TemporaryFileWrapper { public: - static TemporaryFileWrapper create() { - QTemporaryFile tempFile(QString("%1/%2_%3") - .arg( - QDir::tempPath(), - qApp->applicationName().isEmpty() ? "qt_temp" - : qApp->applicationName() - ) - .arg(qApp->applicationPid())); - - if (!tempFile.open()) { - return {}; - } - - return {tempFile.fileName() + ".txt"}; - } - - TemporaryFileWrapper() : - filename{} { - } - - TemporaryFileWrapper(QString filename) : - filename{filename} { - } - ~TemporaryFileWrapper() { QFile::remove(filename); } @@ -59,11 +35,19 @@ class TemporaryFileWrapper { } private: - QString filename; + const QString filename{getTmpFileName()}; + + static QString getTmpFileName() { + QTemporaryFile tempFile{}; + if (!tempFile.open()) { + return {}; + } + return tempFile.fileName() + ".txt"; + } }; const InvokeResult QtTestStep::invokeStepBody() { - const auto file = TemporaryFileWrapper::create(); + const TemporaryFileWrapper file{}; if (!file.exists()) { return InvokeResult::failure("Unable to open temporary file needed for this test"); }