From bcb0f28a4d8625e546ae6d43e1365cc1f8b5373c Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 24 Feb 2012 14:18:08 +0100 Subject: Fix tests of QMimeDatabase. - Use temporary directories to avoid instabilities due to remains of previous failed tests and locked directories. - Replace SRCDIR by QFINDTESTDATA(), reference only the freedesktop.org.xml contained in the Qt source tree by $$QT.corelib.sources. - Improve some error messages, test suite instructions for Windows. Change-Id: Idee8e3767ef0a8299df3bdaaac20334164878db0 Reviewed-by: Friedemann Kleint Reviewed-by: David Faure --- .../mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 157 ++++++++++++++------- 1 file changed, 105 insertions(+), 52 deletions(-) (limited to 'tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp') diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 858f977a72..200944d198 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -44,49 +44,85 @@ #include "qstandardpaths.h" #include +#include +#include #include #include #include +static const char yastFileName[] ="yast2-metapackage-handler-mimetypes.xml"; + void initializeLang() { qputenv("LC_ALL", ""); qputenv("LANG", "en_US"); + QCoreApplication::setApplicationName("tst_qmimedatabase"); // temporary directory pattern +} + +static inline QString testSuiteWarning() +{ + + QString result; + QTextStream str(&result); + str << "\nCannot find the shared-mime-info test suite\nstarting from: " + << QDir::toNativeSeparators(QDir::currentPath()) << "\n" + "cd " << QDir::toNativeSeparators(QStringLiteral("tests/auto/corelib/mimetypes/qmimedatabase")) << "\n" + "wget http://cgit.freedesktop.org/xdg/shared-mime-info/snapshot/Release-1-0.zip\n" + "unzip Release-1-0.zip\n"; +#ifdef Q_OS_WIN + str << "mkdir testfiles\nxcopy /s Release-1-0\\tests testfiles\n"; +#else + str << "ln -s Release-1-0/tests testfiles\n"; +#endif + return result; } // Set LANG before QCoreApplication is created Q_CONSTRUCTOR_FUNCTION(initializeLang) +tst_QMimeDatabase::tst_QMimeDatabase() +{ +} + void tst_QMimeDatabase::initTestCase() { + QVERIFY(m_temporaryDir.isValid()); + // Create a "global" and a "local" XDG data dir, right here. // The local dir will be empty initially, while the global dir will contain a copy of freedesktop.org.xml - QDir here = QDir::currentPath(); + const QDir here = QDir(m_temporaryDir.path()); - qputenv("XDG_DATA_DIRS", QFile::encodeName(here.absolutePath())); - QDir(here.absolutePath() + "/mime").removeRecursively(); - here.mkpath(QString::fromLatin1("mime/packages")); + m_globalXdgDir = m_temporaryDir.path() + QStringLiteral("/global"); + m_localXdgDir = m_temporaryDir.path() + QStringLiteral("/local"); - QFile xml(QFile::decodeName(SRCDIR "../../../src/mimetypes/mime/packages/freedesktop.org.xml")); - const QString mimeDir = here.absolutePath() + QLatin1String("/mime"); - xml.copy(mimeDir + QLatin1String("/packages/freedesktop.org.xml")); + const QString globalPackageDir = m_globalXdgDir + QStringLiteral("/mime/packages"); + QVERIFY(here.mkpath(globalPackageDir) && here.mkpath(m_localXdgDir)); - m_dataHome = here.absolutePath() + QLatin1String("/../datahome"); - qputenv("XDG_DATA_HOME", QFile::encodeName(m_dataHome)); - //qDebug() << "XDG_DATA_HOME=" << m_dataHome; + qputenv("XDG_DATA_DIRS", QFile::encodeName(m_globalXdgDir)); + qputenv("XDG_DATA_HOME", QFile::encodeName(m_localXdgDir)); + qDebug() << "\nLocal XDG_DATA_HOME: " << m_localXdgDir + << "\nGlobal XDG_DATA_DIRS: " << m_globalXdgDir; - // Make sure we start clean - cleanupTestCase(); -} + const QString freeDesktopXml = QStringLiteral("freedesktop.org.xml"); + const QString xmlFileName = QLatin1String(CORE_SOURCES) + + QStringLiteral("/mimetypes/mime/packages/") + + freeDesktopXml; + QVERIFY2(QFileInfo(xmlFileName).exists(), qPrintable(xmlFileName + QStringLiteral(" does not exist"))); + QFile xml(xmlFileName); + QVERIFY(xml.copy(globalPackageDir + '/' + freeDesktopXml)); -void tst_QMimeDatabase::cleanupTestCase() -{ - QDir here = QDir::currentPath(); - here.remove(QString::fromLatin1("mime/packages/yast2-metapackage-handler-mimetypes.xml")); + m_testSuite = QFINDTESTDATA("testfiles"); + if (m_testSuite.isEmpty()) + qWarning("%s", qPrintable(testSuiteWarning())); + + m_yastMimeTypes = QFINDTESTDATA(yastFileName); + QVERIFY2(!m_yastMimeTypes.isEmpty(), + qPrintable(QString::fromLatin1("Cannot find '%1' starting from '%2'"). + arg(yastFileName, QDir::currentPath()))); - QDir(m_dataHome).removeRecursively(); + init(); } void tst_QMimeDatabase::mimeTypeForName() @@ -173,6 +209,19 @@ void tst_QMimeDatabase::mimeTypeForFileName_data() QTest::newRow("doesn't exist but has known extension") << "IDontExist.txt" << "text/plain"; } +static inline QByteArray msgMimeTypeForFileNameFailed(const QList &actual, + const QString &expected) +{ + QByteArray result = "Actual ("; + foreach (const QMimeType &m, actual) { + result += m.name().toLocal8Bit(); + result += ' '; + } + result += ") , expected: "; + result += expected.toLocal8Bit(); + return result; +} + void tst_QMimeDatabase::mimeTypeForFileName() { QFETCH(QString, fileName); @@ -186,8 +235,8 @@ void tst_QMimeDatabase::mimeTypeForFileName() if (expectedMimeType == "application/octet-stream") { QVERIFY(mimes.isEmpty()); } else { - QVERIFY(!mimes.isEmpty()); - QCOMPARE(mimes.count(), 1); + QVERIFY2(!mimes.isEmpty(), msgMimeTypeForFileNameFailed(mimes, expectedMimeType).constData()); + QVERIFY2(mimes.count() == 1, msgMimeTypeForFileNameFailed(mimes, expectedMimeType).constData()); QCOMPARE(mimes.first().name(), expectedMimeType); } } @@ -549,20 +598,14 @@ void tst_QMimeDatabase::findByFileName_data() QTest::addColumn("mimeTypeName"); QTest::addColumn("xFail"); - QString prefix = QLatin1String(SRCDIR "testfiles/"); - - QFile f(prefix + QLatin1String("list")); - if (!f.open(QIODevice::ReadOnly)) { - const QString warning = QString::fromLatin1( - "Please download the shared-mime-info test suite:\n" - "cd tests/auto/corelib/mimetypes/qmimedatabase\n" - "wget http://cgit.freedesktop.org/xdg/shared-mime-info/snapshot/Release-1-0.zip\n" - "unzip Release-1-0.zip\n" - "ln -s Release-1-0/tests testfiles\n" - ); - qWarning() << warning; + if (m_testSuite.isEmpty()) QSKIP("shared-mime-info test suite not available."); - } + + const QString prefix = m_testSuite + QLatin1Char('/'); + const QString fileName = prefix + QLatin1String("list"); + QFile f(fileName); + QVERIFY2(f.open(QIODevice::ReadOnly|QIODevice::Text), + qPrintable(QString::fromLatin1("Cannot open %1: %2").arg(fileName, f.errorString()))); QByteArray line(1024, Qt::Uninitialized); @@ -582,7 +625,9 @@ void tst_QMimeDatabase::findByFileName_data() if (list.size() >= 3) xFail = list.at(2); - QTest::newRow(filePath.toLatin1().constData()) << QString(prefix + filePath) << mimeTypeType << xFail; + QTest::newRow(filePath.toLatin1().constData()) + << QString(prefix + filePath) + << mimeTypeType << xFail; } } @@ -717,13 +762,21 @@ void tst_QMimeDatabase::fromThreads() static bool runUpdateMimeDatabase(const QString &path) // TODO make it a QMimeDatabase method? { - const QString umd = QStandardPaths::findExecutable(QString::fromLatin1("update-mime-database")); - if (umd.isEmpty()) + const QString umdCommand = QString::fromLatin1("update-mime-database"); + const QString umd = QStandardPaths::findExecutable(umdCommand); + if (umd.isEmpty()) { + qWarning("%s does not exist.", qPrintable(umdCommand)); return false; + } QProcess proc; proc.setProcessChannelMode(QProcess::MergedChannels); // silence output - proc.start(umd, QStringList() << path); + proc.start(umd, QStringList(path)); + if (!proc.waitForStarted()) { + qWarning("Cannot start %s: %s", + qPrintable(umd), qPrintable(proc.errorString())); + return false; + } proc.waitForFinished(); //qDebug() << "runUpdateMimeDatabase" << path; return true; @@ -733,7 +786,7 @@ static bool waitAndRunUpdateMimeDatabase(const QString &path) { QFileInfo mimeCacheInfo(path + QString::fromLatin1("/mime.cache")); if (mimeCacheInfo.exists()) { - // Wait until the begining of the next second + // Wait until the beginning of the next second while (mimeCacheInfo.lastModified().secsTo(QDateTime::currentDateTime()) == 0) { QTest::qSleep(200); } @@ -767,16 +820,15 @@ void tst_QMimeDatabase::installNewGlobalMimeType() QMimeDatabase db; QVERIFY(!db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid()); - const QString fileName = QLatin1String("yast2-metapackage-handler-mimetypes.xml"); - const QString srcFile = QFile::decodeName(SRCDIR) + fileName; - - QDir here = QDir::currentPath(); - const QString mimeDir = here.absolutePath() + QLatin1String("/mime"); + const QString mimeDir = m_globalXdgDir + QLatin1String("/mime"); const QString destDir = mimeDir + QLatin1String("/packages/"); - const QString destFile = destDir + fileName; + const QString destFile = destDir + QLatin1String(yastFileName); QFile::remove(destFile); //qDebug() << destFile; - QVERIFY(QFile::copy(srcFile, destFile)); + + if (!QFileInfo(destDir).isDir()) + QVERIFY(QDir(m_globalXdgDir).mkpath(destDir)); + QVERIFY(QFile::copy(m_yastMimeTypes, destFile)); if (!waitAndRunUpdateMimeDatabase(mimeDir)) QSKIP("shared-mime-info not found, skipping mime.cache test"); @@ -801,16 +853,17 @@ void tst_QMimeDatabase::installNewLocalMimeType() QMimeDatabase db; QVERIFY(!db.mimeTypeForName(QLatin1String("text/x-suse-ymp")).isValid()); - const QString fileName = QLatin1String("yast2-metapackage-handler-mimetypes.xml"); - const QString srcFile = QFile::decodeName(SRCDIR) + fileName; - const QString mimeDir = m_dataHome + QLatin1String("/mime"); + const QString mimeDir = m_localXdgDir + QLatin1String("/mime"); const QString destDir = mimeDir + QLatin1String("/packages/"); QDir().mkpath(destDir); - const QString destFile = destDir + fileName; + const QString destFile = destDir + QLatin1String(yastFileName); QFile::remove(destFile); - QVERIFY(QFile::copy(srcFile, destFile)); - if (!runUpdateMimeDatabase(mimeDir)) - QSKIP("shared-mime-info not found, skipping mime.cache test");; + QVERIFY(QFile::copy(m_yastMimeTypes, destFile)); + if (!runUpdateMimeDatabase(mimeDir)) { + const QString skipWarning = QStringLiteral("shared-mime-info not found, skipping mime.cache test (") + + QDir::toNativeSeparators(mimeDir) + QLatin1Char(')'); + QSKIP(qPrintable(skipWarning)); + } QCOMPARE(db.mimeTypeForFile(QLatin1String("foo.ymu"), QMimeDatabase::MatchExtension).name(), QString::fromLatin1("text/x-suse-ymu")); -- cgit v1.2.3 From 53229ec8f79c4895e0476d1f3fb950f0427ba427 Mon Sep 17 00:00:00 2001 From: David Faure Date: Thu, 1 Mar 2012 16:41:39 +0100 Subject: Add note about failing test when using shared-mime-info < 1.0 Change-Id: I3ba9d14d915a579b9e102114866f6c9e0344ba16 Reviewed-by: Friedemann Kleint --- tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp') diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 200944d198..63adcadb86 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -721,6 +721,9 @@ void tst_QMimeDatabase::findByFile_data() findByFileName_data(); } +// Note: this test fails on "testcompress.z" when using a shared-mime-info older than 1.0. +// This because of commit 0f9a506069c in shared-mime-info, which fixed the writing of +// case-insensitive patterns into mime.cache. void tst_QMimeDatabase::findByFile() { QFETCH(QString, filePath); -- cgit v1.2.3 From c78957766a5adba45289a0f7afe22949a183b34b Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 2 Mar 2012 20:33:55 +0100 Subject: QMimeDatabase: Fix crash on empty filename This is due to the search in the suffix tree starting at position fileName.length() - 1. Change-Id: I98501c1724c7dde2626351ace8ba19faa0d2e1e1 Reviewed-by: Ivan Komissarov Reviewed-by: Wolf-Michael Bolle --- tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp') diff --git a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp index 63adcadb86..9076f37c7c 100644 --- a/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp +++ b/tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp @@ -207,6 +207,7 @@ void tst_QMimeDatabase::mimeTypeForFileName_data() QTest::newRow("directory") << "/" << "inode/directory"; QTest::newRow("doesn't exist, no extension") << "IDontExist" << "application/octet-stream"; QTest::newRow("doesn't exist but has known extension") << "IDontExist.txt" << "text/plain"; + QTest::newRow("empty") << "" << "application/octet-stream"; } static inline QByteArray msgMimeTypeForFileNameFailed(const QList &actual, -- cgit v1.2.3