From 61ca116a2eaf8a275803524ade494ace6b9cb812 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 8 Jun 2015 21:49:48 +0200 Subject: QTest: Make qExtractTestData() return the created QTemporaryDir ... and enable auto-deletion on it. This allows users of the function to get rid of their own cleanup code. They just need to keep the shared pointer alive for as long as they need it. Drive-by changes: - replaced QStringLiterals that were only used as the rhs of op+ - replaced an instance of mid() used as the rhs of op+ with midRef() - enabled NRVO Change-Id: I161d39461e020c9e8d473c0810dea2109fe0d62d Reviewed-by: Lars Knoll --- src/testlib/qtestcase.cpp | 35 ++++++++++++++++++++--------------- src/testlib/qtestcase.h | 4 +++- 2 files changed, 23 insertions(+), 16 deletions(-) (limited to 'src/testlib') diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index e3c543671b..d9611f161e 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2794,36 +2794,39 @@ static inline bool isWindowsBuildDirectory(const QString &dirName) #endif /*! - Extract a directory from resources to disk. The content is extracted - recursively to a temporary folder. The extracted content is not removed - automatically. + Extracts a directory from resources to disk. The content is extracted + recursively to a temporary folder. The extracted content is removed + automatically once the last reference to the return value goes out of scope. \a dirName is the name of the directory to extract from resources. - Returns the path where the data was extracted or an empty string in case of + Returns the temporary directory where the data was extracted or null in case of errors. */ -QString QTest::qExtractTestData(const QString &dirName) +QSharedPointer QTest::qExtractTestData(const QString &dirName) { - QTemporaryDir temporaryDir; - temporaryDir.setAutoRemove(false); + QSharedPointer result; // null until success, then == tempDir - if (!temporaryDir.isValid()) - return QString(); + QSharedPointer tempDir = QSharedPointer::create(); - const QString dataPath = temporaryDir.path(); + tempDir->setAutoRemove(true); + + if (!tempDir->isValid()) + return result; + + const QString dataPath = tempDir->path(); const QString resourcePath = QLatin1Char(':') + dirName; const QFileInfo fileInfo(resourcePath); if (!fileInfo.isDir()) { qWarning("Resource path '%s' is not a directory.", qPrintable(resourcePath)); - return QString(); + return result; } QDirIterator it(resourcePath, QDirIterator::Subdirectories); if (!it.hasNext()) { qWarning("Resource directory '%s' is empty.", qPrintable(resourcePath)); - return QString(); + return result; } while (it.hasNext()) { @@ -2832,17 +2835,19 @@ QString QTest::qExtractTestData(const QString &dirName) QFileInfo fileInfo = it.fileInfo(); if (!fileInfo.isDir()) { - const QString destination = dataPath + QLatin1Char('/') + fileInfo.filePath().mid(resourcePath.length()); + const QString destination = dataPath + QLatin1Char('/') + fileInfo.filePath().midRef(resourcePath.length()); QFileInfo destinationFileInfo(destination); QDir().mkpath(destinationFileInfo.path()); if (!QFile::copy(fileInfo.filePath(), destination)) { qWarning("Failed to copy '%s'.", qPrintable(fileInfo.filePath())); - return QString(); + return result; } } } - return dataPath; + result = qMove(tempDir); + + return result; } /*! \internal diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 2c6a94faa1..f24283b65e 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -40,6 +40,8 @@ #include #include #include +#include +#include #include @@ -250,7 +252,7 @@ namespace QTest Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern); #endif - Q_TESTLIB_EXPORT QString qExtractTestData(const QString &dirName); + Q_TESTLIB_EXPORT QSharedPointer qExtractTestData(const QString &dirName); Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = 0, int line = 0, const char* builddir = 0); Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = 0, int line = 0, const char* builddir = 0); -- cgit v1.2.3 From b0a9eddf4da21075415c98ae4b8073043e92256e Mon Sep 17 00:00:00 2001 From: Tim Blechmann Date: Sat, 16 May 2015 14:59:48 +0200 Subject: testlib: fix compile error with macosx10.8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [NSDate date] returns an id, so one needs to send a message instead of accessing a property. Backport commit 92b3397a from the 5.5 branch. Change-Id: Id70915e1ac23994a081765e0a527802fef61b573 Reviewed-by: Tim Blechmann Reviewed-by: Thiago Macieira Reviewed-by: Tor Arne Vestbø --- src/testlib/qxctestlogger.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/testlib') diff --git a/src/testlib/qxctestlogger.mm b/src/testlib/qxctestlogger.mm index 833e566af5..34116a2670 100644 --- a/src/testlib/qxctestlogger.mm +++ b/src/testlib/qxctestlogger.mm @@ -126,7 +126,7 @@ private: if (!([NSDate timeIntervalSinceReferenceDate] > 0)) qFatal("error: Device date '%s' is bad, likely set to update automatically. Please correct.", - [NSDate date].description.UTF8String); + [[NSDate date] description].UTF8String); XCTestDriver *testDriver = nil; if ([QtTestLibWrapper usingTestManager]) -- cgit v1.2.3