Testlib: Add function to extract files from resources to disk
Change-Id: I7ae1cdfea751a759298cf277e36cfef7a9c46edb Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
This commit is contained in:
parent
3769f7c974
commit
2d9c2a0b08
@ -48,6 +48,8 @@
|
||||
#include <QtCore/qdebug.h>
|
||||
#include <QtCore/qlibraryinfo.h>
|
||||
#include <QtCore/private/qtools_p.h>
|
||||
#include <QtCore/qdiriterator.h>
|
||||
#include <QtCore/qtemporarydir.h>
|
||||
|
||||
#include <QtTest/private/qtestlog_p.h>
|
||||
#include <QtTest/private/qtesttable_p.h>
|
||||
@ -2755,6 +2757,58 @@ 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.
|
||||
|
||||
\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
|
||||
errors.
|
||||
*/
|
||||
QString QTest::qExtractTestData(const QString &dirName)
|
||||
{
|
||||
QTemporaryDir temporaryDir;
|
||||
temporaryDir.setAutoRemove(false);
|
||||
|
||||
if (!temporaryDir.isValid())
|
||||
return QString();
|
||||
|
||||
const QString dataPath = temporaryDir.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();
|
||||
}
|
||||
|
||||
QDirIterator it(resourcePath, QDirIterator::Subdirectories);
|
||||
if (!it.hasNext()) {
|
||||
qWarning("Resource directory '%s' is empty.", qPrintable(resourcePath));
|
||||
return QString();
|
||||
}
|
||||
|
||||
while (it.hasNext()) {
|
||||
it.next();
|
||||
|
||||
QFileInfo fileInfo = it.fileInfo();
|
||||
|
||||
if (!fileInfo.isDir()) {
|
||||
const QString destination = dataPath + QLatin1Char('/') + fileInfo.filePath().mid(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 dataPath;
|
||||
}
|
||||
|
||||
/*! \internal
|
||||
*/
|
||||
|
||||
|
@ -209,6 +209,9 @@ do {\
|
||||
QTest::qFindTestData(basepath, __FILE__, __LINE__)
|
||||
#endif
|
||||
|
||||
# define QEXTRACTTESTDATA(resourcePath) \
|
||||
QTest::qExtractTestData(resourcePath)
|
||||
|
||||
class QObject;
|
||||
class QTestData;
|
||||
|
||||
@ -245,6 +248,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 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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user