summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestcase.cpp
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 /src/testlib/qtestcase.cpp
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>
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r--src/testlib/qtestcase.cpp35
1 files changed, 20 insertions, 15 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