From 251ab925fe8e3dfbc8a8e5f9ed999777d3f6af99 Mon Sep 17 00:00:00 2001 From: David Faure Date: Tue, 12 Sep 2023 18:34:24 +0200 Subject: [PATCH] tst_qmimedatabase: separate xml files for Global and Local tests For historic reasons, the test had a single list of override xml files to copy first into the global dir, and then into the local dir. But glob-deleteall only makes sense in the local dir (as per the MIME spec). Having two definitions for the same mimetype in the same dir is undefined behavior, so the test was working by chance only, and my upcoming refactoring/fixes caught that. Change-Id: I4717683b4b3f9ba69f1fd815669460789700e877 Reviewed-by: Thiago Macieira --- .../qmimedatabase-cache/CMakeLists.txt | 1 + .../qmimedatabase-xml/CMakeLists.txt | 1 + .../qmimedatabase/tst_qmimedatabase.cpp | 89 ++++++++++--------- 3 files changed, 50 insertions(+), 41 deletions(-) diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/CMakeLists.txt b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/CMakeLists.txt index 187f224100..3f336e70cc 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/CMakeLists.txt +++ b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/CMakeLists.txt @@ -20,6 +20,7 @@ qt_internal_add_test(tst_qmimedatabase-cache ../tst_qmimedatabase.h tst_qmimedatabase-cache.cpp LIBRARIES + Qt::CorePrivate Qt::Concurrent ) diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/CMakeLists.txt b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/CMakeLists.txt index 31fdd48dc5..78a040a2e7 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/CMakeLists.txt +++ b/tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/CMakeLists.txt @@ -21,6 +21,7 @@ qt_internal_add_test(tst_qmimedatabase-xml tst_qmimedatabase-xml.cpp LIBRARIES Qt::Concurrent + Qt::CorePrivate ) # Resources: diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 40100789da..559bc29b36 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -30,15 +31,22 @@ using namespace Qt::StringLiterals; -static const char *const additionalMimeFiles[] = { "yast2-metapackage-handler-mimetypes.xml", - "qml-again.xml", - "text-x-objcsrc.xml", - "text-plain-subclass.xml", - "invalid-magic1.xml", - "invalid-magic2.xml", - "invalid-magic3.xml", - "magic-and-hierarchy.xml", - 0 }; +static const std::array additionalGlobalMimeFiles = { + "yast2-metapackage-handler-mimetypes.xml", + "qml-again.xml", + "magic-and-hierarchy.xml", +}; + +static const std::array additionalLocalMimeFiles = { + "yast2-metapackage-handler-mimetypes.xml", + "qml-again.xml", + "text-x-objcsrc.xml", + "text-plain-subclass.xml", + "invalid-magic1.xml", + "invalid-magic2.xml", + "invalid-magic3.xml", + "magic-and-hierarchy.xml", +}; static const auto s_resourcePrefix = ":/qt-project.org/qmime/"_L1; static const auto s_inodeMimetype = "inode/directory"_L1; @@ -118,7 +126,6 @@ void tst_QMimeDatabase::initTestCase() if (QDir(m_localMimeDir).exists()) { QVERIFY2(QDir(m_localMimeDir).removeRecursively(), qPrintable(m_localMimeDir + ": " + qt_error_string())); } - QString errorMessage; #ifdef USE_XDG_DATA_DIRS // Create a temporary "global" XDG data dir for later use @@ -138,6 +145,7 @@ void tst_QMimeDatabase::initTestCase() const QString freeDesktopXml = QStringLiteral("freedesktop.org.xml"); const QString xmlFileName = s_resourcePrefix + "packages/"_L1 + freeDesktopXml; const QString xmlTargetFileName = globalPackageDir + QLatin1Char('/') + freeDesktopXml; + QString errorMessage; QVERIFY2(copyResourceFile(xmlFileName, xmlTargetFileName, &errorMessage), qPrintable(errorMessage)); #endif @@ -145,14 +153,6 @@ void tst_QMimeDatabase::initTestCase() if (m_testSuite.isEmpty()) qWarning("%s", qPrintable(testSuiteWarning())); - errorMessage = QString::fromLatin1("Cannot find '%1'"); - for (uint i = 0; i < sizeof additionalMimeFiles / sizeof additionalMimeFiles[0] - 1; i++) { - const QString resourceFilePath = s_resourcePrefix + QLatin1String(additionalMimeFiles[i]); - QVERIFY2(QFile::exists(resourceFilePath), qPrintable(errorMessage.arg(resourceFilePath))); - m_additionalMimeFileNames.append(QLatin1String(additionalMimeFiles[i])); - m_additionalMimeFilePaths.append(resourceFilePath); - } - initTestCaseInternal(); m_isUsingCacheProvider = !qEnvironmentVariableIsSet("QT_NO_MIME_CACHE"); } @@ -1071,6 +1071,30 @@ QT_BEGIN_NAMESPACE extern Q_CORE_EXPORT int qmime_secondsBetweenChecks; // see qmimeprovider.cpp QT_END_NAMESPACE +void copyFiles(const QSpan &additionalMimeFiles, const QString &destDir) +{ + const QString notFoundErrorMessage = QString::fromLatin1("Cannot find '%1'"); + for (const char *mimeFile : additionalMimeFiles) { + const QString resourceFilePath = s_resourcePrefix + QLatin1String(mimeFile); + QVERIFY2(QFile::exists(resourceFilePath), + qPrintable(notFoundErrorMessage.arg(resourceFilePath))); + + const QString destFile = destDir + QLatin1String(mimeFile); + QFile::remove(destFile); + QString errorMessage; + QVERIFY2(copyResourceFile(resourceFilePath, destFile, &errorMessage), + qPrintable(errorMessage)); + } +} + +void deleteFiles(const QSpan &additionalMimeFiles, const QString &destDir) +{ + for (const char *mimeFile : additionalMimeFiles) { + const QString destFile = destDir + QLatin1String(mimeFile); + QFile::remove(destFile); + } +} + void tst_QMimeDatabase::installNewGlobalMimeType() { #if !defined(USE_XDG_DATA_DIRS) @@ -1090,18 +1114,11 @@ void tst_QMimeDatabase::installNewGlobalMimeType() if (!QFileInfo(destDir).isDir()) QVERIFY(QDir(m_globalXdgDir).mkpath(destDir)); - QString errorMessage; - for (int i = 0; i < m_additionalMimeFileNames.size(); ++i) { - const QString destFile = destDir + m_additionalMimeFileNames.at(i); - QFile::remove(destFile); - QVERIFY2(copyResourceFile(m_additionalMimeFilePaths.at(i), destFile, &errorMessage), qPrintable(errorMessage)); - } + copyFiles(additionalGlobalMimeFiles, destDir); + QVERIFY(!QTest::currentTestFailed()); if (m_isUsingCacheProvider && !waitAndRunUpdateMimeDatabase(mimeDir)) QSKIP("shared-mime-info not found, skipping mime.cache test"); - if (!m_isUsingCacheProvider) - ignoreInvalidMimetypeWarnings(mimeDir); - QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(), QString::fromLatin1("text/x-SuSE-ymu")); QVERIFY(db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid()); @@ -1115,12 +1132,6 @@ void tst_QMimeDatabase::installNewGlobalMimeType() QCOMPARE(db.mimeTypeForFile(qmlTestFile).name(), QString::fromLatin1("text/x-qml")); - { - QMimeType objcsrc = db.mimeTypeForName(QStringLiteral("text/x-objcsrc")); - QVERIFY(objcsrc.isValid()); - QCOMPARE(objcsrc.globPatterns(), QStringList()); - } - const QString fooTestFile = s_resourcePrefix + "magic-and-hierarchy.foo"_L1; QCOMPARE(db.mimeTypeForFile(fooTestFile).name(), QString::fromLatin1("application/foo")); @@ -1142,8 +1153,7 @@ void tst_QMimeDatabase::installNewGlobalMimeType() } // Now test removing the mimetype definitions again - for (int i = 0; i < m_additionalMimeFileNames.size(); ++i) - QFile::remove(destDir + m_additionalMimeFileNames.at(i)); + deleteFiles(additionalGlobalMimeFiles, destDir); if (m_isUsingCacheProvider && !waitAndRunUpdateMimeDatabase(mimeDir)) QSKIP("shared-mime-info not found, skipping mime.cache test"); QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(), @@ -1167,12 +1177,9 @@ void tst_QMimeDatabase::installNewLocalMimeType() const QString destDir = m_localMimeDir + QLatin1String("/packages/"); QVERIFY(QDir().mkpath(destDir)); - QString errorMessage; - for (int i = 0; i < m_additionalMimeFileNames.size(); ++i) { - const QString destFile = destDir + m_additionalMimeFileNames.at(i); - QFile::remove(destFile); - QVERIFY2(copyResourceFile(m_additionalMimeFilePaths.at(i), destFile, &errorMessage), qPrintable(errorMessage)); - } + + copyFiles(additionalLocalMimeFiles, destDir); + QVERIFY(!QTest::currentTestFailed()); if (m_isUsingCacheProvider && !waitAndRunUpdateMimeDatabase(m_localMimeDir)) { const QString skipWarning = QStringLiteral("shared-mime-info not found, skipping mime.cache test (") + QDir::toNativeSeparators(m_localMimeDir) + QLatin1Char(')');