summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2015-06-08 21:49:48 +0200
committerJani Heikkinen <jani.heikkinen@theqtcompany.com>2015-06-14 15:19:31 +0000
commit61ca116a2eaf8a275803524ade494ace6b9cb812 (patch)
tree96eb327d81ee7681d3742fcb9adac597dbfff10f
parentd3379cee8a81afaf0f0691d248a399c69025a68f (diff)
QTest: Make qExtractTestData() return the created QTemporaryDirv5.5.0-rc1
... 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 <lars.knoll@digia.com>
-rw-r--r--src/testlib/qtestcase.cpp35
-rw-r--r--src/testlib/qtestcase.h4
-rw-r--r--tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp15
3 files changed, 31 insertions, 23 deletions
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<QTemporaryDir> QTest::qExtractTestData(const QString &dirName)
{
- QTemporaryDir temporaryDir;
- temporaryDir.setAutoRemove(false);
+ QSharedPointer<QTemporaryDir> result; // null until success, then == tempDir
- if (!temporaryDir.isValid())
- return QString();
+ QSharedPointer<QTemporaryDir> tempDir = QSharedPointer<QTemporaryDir>::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 <QtCore/qnamespace.h>
#include <QtCore/qmetatype.h>
#include <QtCore/qtypetraits.h>
+#include <QtCore/qsharedpointer.h>
+#include <QtCore/qtemporarydir.h>
#include <string.h>
@@ -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<QTemporaryDir> 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);
diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
index 62e183f619..1e51f2c5f4 100644
--- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
+++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
@@ -263,21 +263,23 @@ private slots:
private:
const QString m_currentDir;
- QString m_dataPath;
QString m_sourceFile;
QString m_proFile;
QString m_resourcesDir;
QTemporaryDir m_dir;
+ QSharedPointer<QTemporaryDir> m_dataDir;
};
void tst_QFileInfo::initTestCase()
{
- m_dataPath = QEXTRACTTESTDATA("/testdata");
- QVERIFY(!m_dataPath.isEmpty());
+ m_dataDir = QEXTRACTTESTDATA("/testdata");
+ QVERIFY(m_dataDir);
+ const QString dataPath = m_dataDir->path();
+ QVERIFY(!dataPath.isEmpty());
- m_sourceFile = m_dataPath + QStringLiteral("/tst_qfileinfo.cpp");
- m_resourcesDir = m_dataPath + QStringLiteral("/resources");
- m_proFile = m_dataPath + QStringLiteral("/tst_qfileinfo.pro");
+ m_sourceFile = dataPath + QLatin1String("/tst_qfileinfo.cpp");
+ m_resourcesDir = dataPath + QLatin1String("/resources");
+ m_proFile = dataPath + QLatin1String("/tst_qfileinfo.pro");
QVERIFY(m_dir.isValid());
QVERIFY(QDir::setCurrent(m_dir.path()));
@@ -286,7 +288,6 @@ void tst_QFileInfo::initTestCase()
void tst_QFileInfo::cleanupTestCase()
{
QDir::setCurrent(m_currentDir); // Release temporary directory so that it can be deleted on Windows
- QDir(m_dataPath).removeRecursively();
}
// Testing get/set functions