Fix tests of QMimeDatabase.
authorFriedemann Kleint <Friedemann.Kleint@nokia.com>
Fri, 24 Feb 2012 13:18:08 +0000 (14:18 +0100)
committerQt by Nokia <qt-info@nokia.com>
Wed, 29 Feb 2012 15:52:19 +0000 (16:52 +0100)
- 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 <Friedemann.Kleint@nokia.com>
Reviewed-by: David Faure <faure@kde.org>
tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/qmimedatabase-cache.pro
tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-cache/tst_qmimedatabase-cache.cpp
tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/qmimedatabase-xml.pro
tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase-xml/tst_qmimedatabase-xml.cpp
tests/auto/corelib/mimetypes/qmimedatabase/qmimedatabase.pro
tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.cpp
tests/auto/corelib/mimetypes/qmimedatabase/tst_qmimedatabase.h

index 815401c..ba68167 100644 (file)
@@ -7,6 +7,6 @@ QT = core testlib concurrent
 SOURCES = tst_qmimedatabase-cache.cpp
 HEADERS = ../tst_qmimedatabase.h
 
-DEFINES += SRCDIR='"\\"$$PWD/../\\""'
+DEFINES += CORE_SOURCES='"\\"$$QT.core.sources\\""'
 
 *-g++*:QMAKE_CXXFLAGS += -W -Wall -Wextra -Werror -Wshadow -Wno-long-long -Wnon-virtual-dtor
index 205331d..5ef04dc 100644 (file)
 
 #include "../tst_qmimedatabase.cpp"
 
-tst_QMimeDatabase::tst_QMimeDatabase()
+void tst_QMimeDatabase::init()
 {
-    QDir here = QDir::currentPath();
-    const QString tempMime = here.absolutePath() + QString::fromLatin1("/mime");
-    runUpdateMimeDatabase(tempMime);
-    QVERIFY(QFile::exists(tempMime + QString::fromLatin1("/mime.cache")));
+    const QString mimeDirName = m_globalXdgDir + QStringLiteral("/mime");
+    runUpdateMimeDatabase(mimeDirName);
+    QVERIFY(QFile::exists(mimeDirName + QStringLiteral("/mime.cache")));
 }
index ac7515f..4c00e84 100644 (file)
@@ -9,6 +9,6 @@ CONFIG += depend_includepath
 SOURCES += tst_qmimedatabase-xml.cpp
 HEADERS += ../tst_qmimedatabase.h
 
-DEFINES += SRCDIR='"\\"$$PWD/../\\""'
+DEFINES += CORE_SOURCES='"\\"$$QT.core.sources\\""'
 
 *-g++*:QMAKE_CXXFLAGS += -W -Wall -Wextra -Werror -Wshadow -Wno-long-long -Wnon-virtual-dtor
index 13ca372..30c7677 100644 (file)
 ****************************************************************************/
 
 #include "../tst_qmimedatabase.h"
-#include <QDebug>
-#include <QDir>
 
-tst_QMimeDatabase::tst_QMimeDatabase()
+void tst_QMimeDatabase::init()
 {
     qputenv("QT_NO_MIME_CACHE", "1");
 }
index 876b437..1ff5546 100644 (file)
@@ -1,3 +1,3 @@
 TEMPLATE = subdirs
 SUBDIRS = qmimedatabase-xml
-unix: SUBDIRS += qmimedatabase-cache
+unix:!mac: SUBDIRS += qmimedatabase-cache
index 858f977..200944d 100644 (file)
 #include "qstandardpaths.h"
 
 #include <QtCore/QFile>
+#include <QtCore/QFileInfo>
+#include <QtCore/QTextStream>
 #include <QtConcurrent/QtConcurrentRun>
 #include <QtConcurrent/QFuture>
 
 #include <QtTest/QtTest>
 
+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<QMimeType> &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<QString>("mimeTypeName");
     QTest::addColumn<QString>("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"));
index 8699904..94baf77 100644 (file)
@@ -43,6 +43,7 @@
 #define TST_QMIMEDATABASE_H
 
 #include <QtCore/QObject>
+#include <QtCore/QTemporaryDir>
 
 class tst_QMimeDatabase : public QObject
 {
@@ -53,7 +54,6 @@ public:
 
 private slots:
     void initTestCase();
-    void cleanupTestCase();
 
     void mimeTypeForName();
     void mimeTypeForFileName_data();
@@ -93,7 +93,13 @@ private slots:
     void installNewLocalMimeType();
 
 private:
-    QString m_dataHome;
+    void init(); // test-specific
+
+    QString m_globalXdgDir;
+    QString m_localXdgDir;
+    QString m_yastMimeTypes;
+    QTemporaryDir m_temporaryDir;
+    QString m_testSuite;
 };
 
 #endif   // TST_QMIMEDATABASE_H