diff options
author | Rainer Keller <rainer.keller@theqtcompany.com> | 2015-02-05 13:40:51 +0100 |
---|---|---|
committer | Rainer Keller <rainer.keller@theqtcompany.com> | 2015-02-18 08:19:10 +0000 |
commit | 2d9c2a0b08d0c5f268faeb9b46ecd961456d042e (patch) | |
tree | a8ccc00e6f9d601dbb97a11aac365e7fecaa7f93 /src/testlib/qtestcase.cpp | |
parent | 3769f7c974e7df8d240e90cbaafae60626053d26 (diff) |
Testlib: Add function to extract files from resources to disk
Change-Id: I7ae1cdfea751a759298cf277e36cfef7a9c46edb
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@theqtcompany.com>
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r-- | src/testlib/qtestcase.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 16dade9154..acac632410 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -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 */ |