From ca81ec03d69817e838c7758844184447f530e0f6 Mon Sep 17 00:00:00 2001 From: Jason McDonald Date: Tue, 3 Jan 2012 14:02:47 +1000 Subject: Prevent QFileInfo test from leaving temporary files behind. 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 --- tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp | 53 ++++++++++++++--------- 1 file changed, 33 insertions(+), 20 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index c3979fce15..ae371ea9fe 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -49,6 +49,7 @@ #include #include #ifdef Q_OS_UNIX +#include #include #include #include @@ -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 -- cgit v1.2.3