Base 'FileSystem' test helper class on QTemporaryDir.
This avoids test instabilities and prevents test directories from being cluttered with temporary files. Change tests accordingly. Remove unused createLink() method. Change-Id: I843c28ab81c8a476c71c5211a7479b22d3d9fc93 Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
This commit is contained in:
parent
e083d25395
commit
db83f2d73d
@ -878,11 +878,12 @@ void tst_QAbstractFileEngine::mounting()
|
||||
FileEngineHandler handler;
|
||||
|
||||
QFETCH(QString, fileName);
|
||||
const QString absName = fs.absoluteFilePath(fileName);
|
||||
|
||||
QVERIFY(QFileInfo(fileName).isDir());
|
||||
QDir dir(fileName);
|
||||
QVERIFY(QFileInfo(absName).isDir());
|
||||
QDir dir(absName);
|
||||
QCOMPARE(dir.entryList(), (QStringList() << "bar" << "foo"));
|
||||
QDir dir2;
|
||||
QDir dir2(fs.path());
|
||||
bool found = false;
|
||||
foreach (QFileInfo info, dir2.entryInfoList()) {
|
||||
if (info.fileName() == QLatin1String("test.tar")) {
|
||||
|
@ -1540,25 +1540,26 @@ void tst_QDir::updateFileLists()
|
||||
// Test setup
|
||||
|
||||
FileSystem fs;
|
||||
const QString dirName = QStringLiteral("update-file-lists");
|
||||
|
||||
QVERIFY( fs.createDirectory("update-file-lists") );
|
||||
QVERIFY( fs.createFile("update-file-lists/file1.txt") );
|
||||
QVERIFY( fs.createFile("update-file-lists/file2.doc") );
|
||||
QVERIFY( fs.createDirectory(dirName));
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/file1.txt")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/file2.doc")) );
|
||||
|
||||
QVERIFY( fs.createDirectory("update-file-lists/sub-dir1") );
|
||||
QVERIFY( fs.createFile("update-file-lists/sub-dir1/file3.txt") );
|
||||
QVERIFY( fs.createFile("update-file-lists/sub-dir1/file4.doc") );
|
||||
QVERIFY( fs.createFile("update-file-lists/sub-dir1/file5.txt") );
|
||||
QVERIFY( fs.createDirectory(dirName + QStringLiteral("/sub-dir1")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir1/file3.txt")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir1/file4.doc")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir1/file5.txt")) );
|
||||
|
||||
QVERIFY( fs.createDirectory("update-file-lists/sub-dir2") );
|
||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file6.txt") );
|
||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file7.txt") );
|
||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file8.doc") );
|
||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file9.doc") );
|
||||
QVERIFY( fs.createDirectory(dirName + QStringLiteral("/sub-dir2")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file6.txt")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file7.txt")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file8.doc")) );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file9.doc")) );
|
||||
|
||||
// Actual test
|
||||
|
||||
QDir dir("update-file-lists");
|
||||
QDir dir(fs.absoluteFilePath(dirName));
|
||||
|
||||
#if defined(Q_OS_WINCE)
|
||||
//no . and .. on these OS.
|
||||
@ -1577,7 +1578,7 @@ void tst_QDir::updateFileLists()
|
||||
QCOMPARE(dir.count(), uint(4));
|
||||
QCOMPARE(dir.entryInfoList().size(), 4);
|
||||
|
||||
dir.setPath("update-file-lists/sub-dir1");
|
||||
dir.setPath(fs.absoluteFilePath(dirName + QStringLiteral("/sub-dir1")));
|
||||
|
||||
QCOMPARE(dir.entryInfoList().size(), 3);
|
||||
QCOMPARE(dir.count(), uint(3));
|
||||
@ -1589,7 +1590,7 @@ void tst_QDir::updateFileLists()
|
||||
QCOMPARE(dir.entryList().size(), 2);
|
||||
QCOMPARE(dir.count(), uint(2));
|
||||
|
||||
dir.setPath("update-file-lists");
|
||||
dir.setPath(fs.absoluteFilePath(dirName));
|
||||
dir = QDir(dir.path(),
|
||||
"*.txt",
|
||||
QDir::Name | QDir::DirsLast,
|
||||
@ -1608,7 +1609,7 @@ void tst_QDir::updateFileLists()
|
||||
QCOMPARE(dir.entryList(), QStringList() << "sub-dir1" << "sub-dir2" << "file1.txt");
|
||||
|
||||
{
|
||||
QVERIFY( fs.createFile("update-file-lists/extra-file.txt") );
|
||||
QVERIFY( fs.createFile(dirName + QStringLiteral("/extra-file.txt")) );
|
||||
|
||||
QDir dir2(dir);
|
||||
|
||||
|
@ -1439,20 +1439,14 @@ void tst_QCompleter::task247560_keyboardNavigation()
|
||||
void tst_QCompleter::QTBUG_14292_filesystem()
|
||||
{
|
||||
FileSystem fs;
|
||||
QDir tmpDir = QDir::currentPath();
|
||||
|
||||
qsrand(QTime::currentTime().msec());
|
||||
QString d = "tst_QCompleter_" + QString::number(qrand());
|
||||
QVERIFY(fs.createDirectory(tmpDir.filePath(d)));
|
||||
|
||||
QVERIFY(tmpDir.cd(d));
|
||||
QVERIFY(fs.createDirectory(tmpDir.filePath("hello")));
|
||||
QVERIFY(fs.createDirectory(tmpDir.filePath("holla")));
|
||||
QVERIFY(fs.createDirectory(QStringLiteral("hello")));
|
||||
QVERIFY(fs.createDirectory(QStringLiteral("holla")));
|
||||
|
||||
QLineEdit edit;
|
||||
QCompleter comp;
|
||||
QFileSystemModel model;
|
||||
model.setRootPath(tmpDir.path());
|
||||
model.setRootPath(fs.path());
|
||||
comp.setModel(&model);
|
||||
edit.setCompleter(&comp);
|
||||
|
||||
@ -1464,7 +1458,7 @@ void tst_QCompleter::QTBUG_14292_filesystem()
|
||||
QTRY_VERIFY(edit.hasFocus());
|
||||
|
||||
QVERIFY(!comp.popup()->isVisible());
|
||||
edit.setText(tmpDir.path());
|
||||
edit.setText(fs.path());
|
||||
QTest::keyClick(&edit, '/');
|
||||
QTRY_VERIFY(comp.popup()->isVisible());
|
||||
QCOMPARE(comp.popup()->model()->rowCount(), 2);
|
||||
@ -1475,12 +1469,12 @@ void tst_QCompleter::QTBUG_14292_filesystem()
|
||||
QCOMPARE(comp.popup()->model()->rowCount(), 1);
|
||||
QTest::keyClick(&edit, 'r');
|
||||
QTRY_VERIFY(!comp.popup()->isVisible());
|
||||
QVERIFY(fs.createDirectory(tmpDir.filePath("hero")));
|
||||
QVERIFY(fs.createDirectory(QStringLiteral("hero")));
|
||||
QTRY_VERIFY(comp.popup()->isVisible());
|
||||
QCOMPARE(comp.popup()->model()->rowCount(), 1);
|
||||
QTest::keyClick(comp.popup(), Qt::Key_Escape);
|
||||
QTRY_VERIFY(!comp.popup()->isVisible());
|
||||
QVERIFY(fs.createDirectory(tmpDir.filePath("nothingThere")));
|
||||
QVERIFY(fs.createDirectory(QStringLiteral("nothingThere")));
|
||||
//there is no reason creating a file should open a popup, it did in Qt 4.7.0
|
||||
QTest::qWait(60);
|
||||
QVERIFY(!comp.popup()->isVisible());
|
||||
@ -1497,7 +1491,7 @@ void tst_QCompleter::QTBUG_14292_filesystem()
|
||||
QVERIFY(QTest::qWaitForWindowActive(&w));
|
||||
QVERIFY(!edit.hasFocus() && !comp.popup()->hasFocus());
|
||||
|
||||
QVERIFY(fs.createDirectory(tmpDir.filePath("hemo")));
|
||||
QVERIFY(fs.createDirectory(QStringLiteral("hemo")));
|
||||
//there is no reason creating a file should open a popup, it did in Qt 4.7.0
|
||||
QTest::qWait(60);
|
||||
QVERIFY(!comp.popup()->isVisible());
|
||||
|
@ -38,13 +38,14 @@
|
||||
** $QT_END_LICENSE$
|
||||
**
|
||||
****************************************************************************/
|
||||
// Helper functions for creating file-system hierarchies and cleaning up.
|
||||
|
||||
#ifndef QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
|
||||
#define QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
|
||||
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QTemporaryDir>
|
||||
#include <QScopedPointer>
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
|
||||
@ -60,54 +61,37 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
struct FileSystem
|
||||
// QTemporaryDir-based helper class for creating file-system hierarchies and cleaning up.
|
||||
class FileSystem
|
||||
{
|
||||
~FileSystem()
|
||||
{
|
||||
Q_FOREACH(QString fileName, createdFiles)
|
||||
QFile::remove(fileName);
|
||||
Q_DISABLE_COPY(FileSystem)
|
||||
public:
|
||||
FileSystem() : m_temporaryDir(FileSystem::tempFilePattern()) {}
|
||||
|
||||
Q_FOREACH(QString dirName, createdDirectories)
|
||||
currentDir.rmdir(dirName);
|
||||
}
|
||||
QString path() const { return m_temporaryDir.path(); }
|
||||
QString absoluteFilePath(const QString &fileName) const { return path() + QLatin1Char('/') + fileName; }
|
||||
|
||||
bool createDirectory(const QString &dirName)
|
||||
bool createDirectory(const QString &relativeDirName)
|
||||
{
|
||||
if (currentDir.mkdir(dirName)) {
|
||||
createdDirectories.prepend(dirName);
|
||||
return true;
|
||||
if (m_temporaryDir.isValid()) {
|
||||
QDir dir(m_temporaryDir.path());
|
||||
return dir.mkpath(relativeDirName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool createFile(const QString &fileName)
|
||||
bool createFile(const QString &relativeFileName)
|
||||
{
|
||||
QFile file(fileName);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
createdFiles << fileName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
QScopedPointer<QFile> file(openFileForWrite(relativeFileName));
|
||||
return !file.isNull();
|
||||
}
|
||||
|
||||
qint64 createFileWithContent(const QString &fileName)
|
||||
qint64 createFileWithContent(const QString &relativeFileName)
|
||||
{
|
||||
QFile file(fileName);
|
||||
if (file.open(QIODevice::WriteOnly)) {
|
||||
createdFiles << fileName;
|
||||
return file.write(fileName.toUtf8());
|
||||
}
|
||||
return -1;
|
||||
QScopedPointer<QFile> file(openFileForWrite(relativeFileName));
|
||||
return file.isNull() ? qint64(-1) : file->write(relativeFileName.toUtf8());
|
||||
}
|
||||
|
||||
bool createLink(const QString &destination, const QString &linkName)
|
||||
{
|
||||
if (QFile::link(destination, linkName)) {
|
||||
createdFiles << linkName;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
|
||||
static void createNtfsJunction(QString target, QString linkName)
|
||||
{
|
||||
@ -163,10 +147,30 @@ struct FileSystem
|
||||
#endif
|
||||
|
||||
private:
|
||||
QDir currentDir;
|
||||
static QString tempFilePattern()
|
||||
{
|
||||
QString result = QDir::tempPath();
|
||||
if (!result.endsWith(QLatin1Char('/')))
|
||||
result.append(QLatin1Char('/'));
|
||||
result += QStringLiteral("qt-test-filesystem-");
|
||||
result += QCoreApplication::applicationName();
|
||||
result += QStringLiteral("-XXXXXX");
|
||||
return result;
|
||||
}
|
||||
|
||||
QStringList createdDirectories;
|
||||
QStringList createdFiles;
|
||||
QFile *openFileForWrite(const QString &fileName) const
|
||||
{
|
||||
if (m_temporaryDir.isValid()) {
|
||||
const QString absName = absoluteFilePath(fileName);
|
||||
QScopedPointer<QFile> file(new QFile(absName));
|
||||
if (file->open(QIODevice::WriteOnly))
|
||||
return file.take();
|
||||
qWarning("Cannot open '%s' for writing: %s", qPrintable(absName), qPrintable(file->errorString()));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
QTemporaryDir m_temporaryDir;
|
||||
};
|
||||
|
||||
#endif // include guard
|
||||
|
Loading…
x
Reference in New Issue
Block a user