Improve/cleanup qtemporarydir (and qtemporaryfile) unit tests

To follow the comments in the review by João Abecasis.

Change-Id: Ie566705d3b4071b8628d269246aadcde4866f34f
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
This commit is contained in:
David Faure 2011-12-08 15:32:27 +01:00 committed by Qt by Nokia
parent 757ef710d8
commit 5cb05091d2
2 changed files with 33 additions and 75 deletions

View File

@ -55,12 +55,7 @@ class tst_QTemporaryDir : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
tst_QTemporaryDir();
virtual ~tst_QTemporaryDir();
public slots: public slots:
void init();
void cleanup();
void initTestCase(); void initTestCase();
void cleanupTestCase(); void cleanupTestCase();
@ -75,7 +70,6 @@ private slots:
void openOnRootDrives(); void openOnRootDrives();
void stressTest(); void stressTest();
void rename(); void rename();
void autoRemoveAfterFailedRename();
void QTBUG_4796_data(); void QTBUG_4796_data();
void QTBUG_4796(); void QTBUG_4796();
@ -115,23 +109,6 @@ void tst_QTemporaryDir::getSetCheck()
QCOMPARE(true, obj1.autoRemove()); QCOMPARE(true, obj1.autoRemove());
} }
tst_QTemporaryDir::tst_QTemporaryDir()
{
}
tst_QTemporaryDir::~tst_QTemporaryDir()
{
}
void tst_QTemporaryDir::init()
{
}
void tst_QTemporaryDir::cleanup()
{
}
void tst_QTemporaryDir::fileTemplate_data() void tst_QTemporaryDir::fileTemplate_data()
{ {
QTest::addColumn<QString>("constructorTemplate"); QTest::addColumn<QString>("constructorTemplate");
@ -177,7 +154,7 @@ void tst_QTemporaryDir::fileName()
dir.setAutoRemove(true); dir.setAutoRemove(true);
QString fileName = dir.path(); QString fileName = dir.path();
QVERIFY2(fileName.contains("/tst_qtemporarydir-"), qPrintable(fileName)); QVERIFY2(fileName.contains("/tst_qtemporarydir-"), qPrintable(fileName));
QVERIFY(QFile::exists(fileName)); QVERIFY(QDir(fileName).exists());
// Get path to the temp dir, without the file name. // Get path to the temp dir, without the file name.
QString absoluteFilePath = QFileInfo(fileName).absolutePath(); QString absoluteFilePath = QFileInfo(fileName).absolutePath();
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
@ -200,9 +177,9 @@ void tst_QTemporaryDir::autoRemove()
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// Windows seems unreliable here: sometimes it says the directory still exists, // Windows seems unreliable here: sometimes it says the directory still exists,
// immediately after we deleted it. // immediately after we deleted it.
QTRY_VERIFY(!QFile::exists(dirName)); QTRY_VERIFY(!QDir(dirName).exists());
#else #else
QVERIFY(!QFile::exists(dirName)); QVERIFY(!QDir(dirName).exists());
#endif #endif
// Test if disabling auto remove works. // Test if disabling auto remove works.
@ -212,9 +189,9 @@ void tst_QTemporaryDir::autoRemove()
QVERIFY(dir.isValid()); QVERIFY(dir.isValid());
dirName = dir.path(); dirName = dir.path();
} }
QVERIFY(QFile::exists(dirName)); QVERIFY(QDir(dirName).exists());
QVERIFY(QDir().rmdir(dirName)); QVERIFY(QDir().rmdir(dirName));
QVERIFY(!QFile::exists(dirName)); QVERIFY(!QDir(dirName).exists());
// Do not explicitly call setAutoRemove (tests if it really is the default as documented) // Do not explicitly call setAutoRemove (tests if it really is the default as documented)
{ {
@ -223,9 +200,9 @@ void tst_QTemporaryDir::autoRemove()
dirName = dir.path(); dirName = dir.path();
} }
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QTRY_VERIFY(!QFile::exists(dirName)); QTRY_VERIFY(!QDir(dirName).exists());
#else #else
QVERIFY(!QFile::exists(dirName)); QVERIFY(!QDir(dirName).exists());
#endif #endif
// Test autoremove with files and subdirs in the temp dir // Test autoremove with files and subdirs in the temp dir
@ -242,16 +219,25 @@ void tst_QTemporaryDir::autoRemove()
QCOMPARE(file.write("Hello"), 5LL); QCOMPARE(file.write("Hello"), 5LL);
} }
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
QTRY_VERIFY(!QFile::exists(dirName)); QTRY_VERIFY(!QDir(dirName).exists());
#else #else
QVERIFY(!QFile::exists(dirName)); QVERIFY(!QDir(dirName).exists());
#endif #endif
} }
void tst_QTemporaryDir::nonWritableCurrentDir() void tst_QTemporaryDir::nonWritableCurrentDir()
{ {
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
QString cwd = QDir::currentPath(); struct ChdirOnReturn
{
ChdirOnReturn(const QString& d) : dir(d) {}
~ChdirOnReturn() {
QDir::setCurrent(dir);
}
QString dir;
};
ChdirOnReturn cor(QDir::currentPath());
QDir::setCurrent("/"); QDir::setCurrent("/");
// QTemporaryDir("tempXXXXXX") is probably a bad idea in any app // QTemporaryDir("tempXXXXXX") is probably a bad idea in any app
// where the current dir could anything... // where the current dir could anything...
@ -260,7 +246,6 @@ void tst_QTemporaryDir::nonWritableCurrentDir()
dir.setAutoRemove(true); dir.setAutoRemove(true);
QVERIFY(!dir.isValid()); QVERIFY(!dir.isValid());
fileName = dir.path(); fileName = dir.path();
QDir::setCurrent(cwd);
#endif #endif
} }
@ -269,13 +254,13 @@ void tst_QTemporaryDir::openOnRootDrives()
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
unsigned int lastErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS); unsigned int lastErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
#endif #endif
// If it's possible to create a dir in the root directory, it // If it's possible to create a file in the root directory, it
// must be possible to create a temp dir there too. // must be possible to create a temp dir there too.
foreach (const QFileInfo &driveInfo, QDir::drives()) { foreach (const QFileInfo &driveInfo, QDir::drives()) {
QFile testFile(driveInfo.filePath() + "XXXXXX.txt"); QFile testFile(driveInfo.filePath() + "XXXXXX");
if (testFile.open(QIODevice::ReadWrite)) { if (testFile.open(QIODevice::ReadWrite)) {
testFile.remove(); testFile.remove();
QTemporaryDir dir(driveInfo.filePath() + "XXXXXX.txt"); QTemporaryDir dir(driveInfo.filePath() + "XXXXXX");
dir.setAutoRemove(true); dir.setAutoRemove(true);
QVERIFY(dir.isValid()); QVERIFY(dir.isValid());
} }
@ -330,41 +315,6 @@ void tst_QTemporaryDir::rename()
QVERIFY(!dir.exists()); QVERIFY(!dir.exists());
} }
void tst_QTemporaryDir::autoRemoveAfterFailedRename()
{
struct CleanOnReturn
{
~CleanOnReturn()
{
if (!tempName.isEmpty())
QVERIFY(QDir(tempName).removeRecursively());
}
void reset()
{
tempName.clear();
}
QString tempName;
};
CleanOnReturn cleaner;
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
cleaner.tempName = dir.path();
QVERIFY(QFile::exists(cleaner.tempName));
QVERIFY(!QFileInfo("i-do-not-exist").isDir());
QVERIFY(!QDir().rename(cleaner.tempName, "i-do-not-exist/dir.txt"));
QVERIFY(QFile::exists(cleaner.tempName));
}
QVERIFY(!QFile::exists(cleaner.tempName));
cleaner.reset();
}
void tst_QTemporaryDir::QTBUG_4796_data() void tst_QTemporaryDir::QTBUG_4796_data()
{ {
QTest::addColumn<QString>("prefix"); QTest::addColumn<QString>("prefix");
@ -461,7 +411,7 @@ void tst_QTemporaryDir::QTBUG_4796() // unicode support
QTest::qWait(20); QTest::qWait(20);
#endif #endif
foreach (const QString &tempName, cleaner.tempNames) foreach (const QString &tempName, cleaner.tempNames)
QVERIFY2(!QFile::exists(tempName), qPrintable(tempName)); QVERIFY2(!QDir(tempName).exists(), qPrintable(tempName));
cleaner.reset(); cleaner.reset();
} }

View File

@ -256,7 +256,16 @@ void tst_QTemporaryFile::autoRemove()
void tst_QTemporaryFile::nonWritableCurrentDir() void tst_QTemporaryFile::nonWritableCurrentDir()
{ {
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
QString cwd = QDir::currentPath(); struct ChdirOnReturn
{
ChdirOnReturn(const QString& d) : dir(d) {}
~ChdirOnReturn() {
QDir::setCurrent(dir);
}
QString dir;
};
ChdirOnReturn cor(QDir::currentPath());
QDir::setCurrent("/"); QDir::setCurrent("/");
// QTemporaryFile("tempXXXXXX") is probably a bad idea in any app // QTemporaryFile("tempXXXXXX") is probably a bad idea in any app
// where the current dir could anything... // where the current dir could anything...
@ -265,7 +274,6 @@ void tst_QTemporaryFile::nonWritableCurrentDir()
file.setAutoRemove(true); file.setAutoRemove(true);
QVERIFY(!file.open()); QVERIFY(!file.open());
fileName = file.fileName(); fileName = file.fileName();
QDir::setCurrent(cwd);
#endif #endif
} }