diff options
author | Jason McDonald <jason.mcdonald@nokia.com> | 2012-01-03 14:02:47 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-01-05 03:49:45 +0100 |
commit | ca81ec03d69817e838c7758844184447f530e0f6 (patch) | |
tree | 898948d4493c3b5ae19ad5cbef583140174f4ab7 | |
parent | 77ddb00a49fdde54df2b232e9e901a08e48cee6d (diff) | |
download | qtbase-ca81ec03d69817e838c7758844184447f530e0f6.tar.gz |
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 <rohan.mcgovern@nokia.com>
-rw-r--r-- | tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp | 53 |
1 files changed, 33 insertions, 20 deletions
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 <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 |