Prevent QFileInfo test from leaving temporary files behind.
authorJason McDonald <jason.mcdonald@nokia.com>
Tue, 3 Jan 2012 04:02:47 +0000 (14:02 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 5 Jan 2012 02:49:45 +0000 (03:49 +0100)
Use a small helper class to ensure that the files created during the
test are removed afterwards, even if the test fails. Also, verify
creation of the files in the body of the test function, not in the
helper, as verifying in the helper won't terminate the test on failure.

Change-Id: I76eff20e54ef6a1ed71d9bbb31e00f41f3d14c38
Reviewed-by: Rohan McGovern <rohan.mcgovern@nokia.com>
tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp

index c3979fc..ae371ea 100644 (file)
@@ -49,6 +49,7 @@
 #include <qdir.h>
 #include <qfileinfo.h>
 #ifdef Q_OS_UNIX
+#include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/stat.h>
@@ -1492,41 +1493,53 @@ void tst_QFileInfo::testDecomposedUnicodeNames_data()
     QTest::newRow("no decomposed") << currPath + QString::fromUtf8("/4 øøøcopy.pdf") << QString::fromUtf8("4 øøøcopy.pdf") << true;
 }
 
-static void createFileNative(const QString &filePath)
+// This is a helper class that ensures that files created during the test
+// will be removed afterwards, even if the test fails or throws an exception.
+class NativeFileCreator
 {
+public:
+    NativeFileCreator(const QString &filePath)
+        : m_filePath(filePath), m_error(0)
+    {
 #ifdef Q_OS_UNIX
-    int fd = open(filePath.normalized(QString::NormalizationForm_D).toUtf8().constData(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
-    if (fd < 0) {
-        QFAIL("couldn't create file");
-    } else {
-        close(fd);
-    }
-#else
-    Q_UNUSED(filePath);
+        int fd = open(m_filePath.normalized(QString::NormalizationForm_D).toUtf8().constData(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
+        if (fd >= 0)
+            close(fd);
+        else
+            m_error = errno;
 #endif
-}
-
-static void removeFileNative(const QString &filePath)
-{
+    }
+    ~NativeFileCreator()
+    {
 #ifdef Q_OS_UNIX
-    unlink(filePath.normalized(QString::NormalizationForm_D).toUtf8().constData());
-#else
-    Q_UNUSED(filePath);
+        if (m_error == 0)
+            unlink(m_filePath.normalized(QString::NormalizationForm_D).toUtf8().constData());
 #endif
-}
+    }
+    int error() const
+    {
+        return m_error;
+    }
+
+private:
+    QString m_filePath;
+    int m_error;
+};
 
 void tst_QFileInfo::testDecomposedUnicodeNames()
 {
 #ifndef Q_OS_MAC
     QSKIP("This is a OS X only test (unless you know more about filesystems, then maybe you should try it ;)");
-#endif
+#else
     QFETCH(QString, filePath);
-    createFileNative(filePath);
+    NativeFileCreator nativeFileCreator(filePath);
+    int error = nativeFileCreator.error();
+    QVERIFY2(error == 0, qPrintable(QString("Couldn't create native file %1: %2").arg(filePath).arg(strerror(error))));
 
     QFileInfo file(filePath);
     QTEST(file.fileName(), "fileName");
     QTEST(file.exists(), "exists");
-    removeFileNative(filePath);
+#endif
 }
 
 void tst_QFileInfo::equalOperator() const