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] 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"); }