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 <lars.knoll@digia.com>bb10
parent
d3379cee8a
commit
61ca116a2e
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue