From 0e9b51ebac1c9ba70d703c5729def6e2aab764f9 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 14 Jul 2015 10:15:35 +0200 Subject: tst_qmimedatabase.cpp: Set write permission on files extracted from resources. Set QFileDevice::WriteUser on all files extracted from resources. These are read-only, which is preserved by QFile::copy(). This caused the deletion of the temporary directory to fail on Windows. Change-Id: Id99de9160471c38bcec68025c89cfabbe209bdbe Reviewed-by: Oswald Buddenhagen Reviewed-by: David Faure --- .../mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 46 ++++++++++++++++++---- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'tests/auto/corelib/mimetypes') diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index a0edf29607..1c9add86f7 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -72,6 +72,34 @@ static inline QString testSuiteWarning() return result; } +static bool copyResourceFile(const QString &sourceFileName, const QString &targetFileName, + QString *errorMessage) +{ + + QFile sourceFile(sourceFileName); + if (!sourceFile.exists()) { + *errorMessage = QDir::toNativeSeparators(sourceFileName) + QLatin1String(" does not exist."); + return false; + } + if (!sourceFile.copy(targetFileName)) { + *errorMessage = QLatin1String("Cannot copy ") + + QDir::toNativeSeparators(sourceFileName) + QLatin1String(" to ") + + QDir::toNativeSeparators(targetFileName) + QLatin1String(": ") + + sourceFile.errorString(); + return false; + } + // QFile::copy() sets the permissions of the source file which are read-only for + // resource files. Set write permission to enable deletion of the temporary directory. + QFile targetFile(targetFileName); + if (!targetFile.setPermissions(targetFile.permissions() | QFileDevice::WriteUser)) { + *errorMessage = QLatin1String("Cannot set write permission on ") + + QDir::toNativeSeparators(targetFileName) + QLatin1String(": ") + + targetFile.errorString(); + return false; + } + return true; +} + // Set LANG before QCoreApplication is created Q_CONSTRUCTOR_FUNCTION(initializeLang) @@ -101,15 +129,15 @@ void tst_QMimeDatabase::initTestCase() const QString freeDesktopXml = QStringLiteral("freedesktop.org.xml"); const QString xmlFileName = QLatin1String(RESOURCE_PREFIX) + freeDesktopXml; - QVERIFY2(QFileInfo(xmlFileName).exists(), qPrintable(xmlFileName + QStringLiteral(" does not exist"))); - QFile xml(xmlFileName); - QVERIFY(xml.copy(globalPackageDir + '/' + freeDesktopXml)); + const QString xmlTargetFileName = globalPackageDir + QLatin1Char('/') + freeDesktopXml; + QString errorMessage; + QVERIFY2(copyResourceFile(xmlFileName, xmlTargetFileName, &errorMessage), qPrintable(errorMessage)); m_testSuite = QFINDTESTDATA("testfiles"); if (m_testSuite.isEmpty()) qWarning("%s", qPrintable(testSuiteWarning())); - const QString errorMessage = QString::fromLatin1("Cannot find '%1'"); + errorMessage = QString::fromLatin1("Cannot find '%1'"); m_yastMimeTypes = QLatin1String(RESOURCE_PREFIX) + yastFileName; QVERIFY2(QFile::exists(m_yastMimeTypes), qPrintable(errorMessage.arg(yastFileName))); m_qmlAgainFileName = QLatin1String(RESOURCE_PREFIX) + qmlAgainFileName; @@ -830,8 +858,9 @@ void tst_QMimeDatabase::installNewGlobalMimeType() if (!QFileInfo(destDir).isDir()) QVERIFY(QDir(m_globalXdgDir).mkpath(destDir)); - QVERIFY(QFile::copy(m_yastMimeTypes, destFile)); - QVERIFY(QFile::copy(m_qmlAgainFileName, destQmlFile)); + QString errorMessage; + QVERIFY2(copyResourceFile(m_yastMimeTypes, destFile, &errorMessage), qPrintable(errorMessage)); + QVERIFY2(copyResourceFile(m_qmlAgainFileName, destQmlFile, &errorMessage), qPrintable(errorMessage)); if (!waitAndRunUpdateMimeDatabase(mimeDir)) QSKIP("shared-mime-info not found, skipping mime.cache test"); @@ -876,8 +905,9 @@ void tst_QMimeDatabase::installNewLocalMimeType() QFile::remove(destFile); const QString destQmlFile = destDir + QLatin1String(qmlAgainFileName); QFile::remove(destQmlFile); - QVERIFY(QFile::copy(m_yastMimeTypes, destFile)); - QVERIFY(QFile::copy(m_qmlAgainFileName, destQmlFile)); + QString errorMessage; + QVERIFY2(copyResourceFile(m_yastMimeTypes, destFile, &errorMessage), qPrintable(errorMessage)); + QVERIFY2(copyResourceFile(m_qmlAgainFileName, destQmlFile, &errorMessage), qPrintable(errorMessage)); if (!runUpdateMimeDatabase(mimeDir)) { const QString skipWarning = QStringLiteral("shared-mime-info not found, skipping mime.cache test (") + QDir::toNativeSeparators(mimeDir) + QLatin1Char(')'); -- cgit v1.2.3 From 5f1a0cb42a5aa509e0660fc892f2291593fb08ef Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 5 Aug 2015 08:49:16 -0700 Subject: Seed the random number generator before using QTemporaryDir Otherwise, on some systems (Windows), we'll always create the same dirs. Change-Id: Id3d5c7bf4d4c45069621ffff13f79ba91e0f974b Reviewed-by: Christopher Adams --- tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'tests/auto/corelib/mimetypes') diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 1c9add86f7..99ebf77a3d 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -103,7 +103,14 @@ static bool copyResourceFile(const QString &sourceFileName, const QString &targe // Set LANG before QCoreApplication is created Q_CONSTRUCTOR_FUNCTION(initializeLang) +static QString seedAndTemplate() +{ + qsrand(QDateTime::currentDateTimeUtc().toTime_t()); + return QDir::tempPath() + "/tst_qmimedatabase-XXXXXX"; +} + tst_QMimeDatabase::tst_QMimeDatabase() + : m_temporaryDir(seedAndTemplate()) { } -- cgit v1.2.3