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;
|
FileEngineHandler handler;
|
||||||
|
|
||||||
QFETCH(QString, fileName);
|
QFETCH(QString, fileName);
|
||||||
|
const QString absName = fs.absoluteFilePath(fileName);
|
||||||
|
|
||||||
QVERIFY(QFileInfo(fileName).isDir());
|
QVERIFY(QFileInfo(absName).isDir());
|
||||||
QDir dir(fileName);
|
QDir dir(absName);
|
||||||
QCOMPARE(dir.entryList(), (QStringList() << "bar" << "foo"));
|
QCOMPARE(dir.entryList(), (QStringList() << "bar" << "foo"));
|
||||||
QDir dir2;
|
QDir dir2(fs.path());
|
||||||
bool found = false;
|
bool found = false;
|
||||||
foreach (QFileInfo info, dir2.entryInfoList()) {
|
foreach (QFileInfo info, dir2.entryInfoList()) {
|
||||||
if (info.fileName() == QLatin1String("test.tar")) {
|
if (info.fileName() == QLatin1String("test.tar")) {
|
||||||
|
@ -1540,25 +1540,26 @@ void tst_QDir::updateFileLists()
|
|||||||
// Test setup
|
// Test setup
|
||||||
|
|
||||||
FileSystem fs;
|
FileSystem fs;
|
||||||
|
const QString dirName = QStringLiteral("update-file-lists");
|
||||||
|
|
||||||
QVERIFY( fs.createDirectory("update-file-lists") );
|
QVERIFY( fs.createDirectory(dirName));
|
||||||
QVERIFY( fs.createFile("update-file-lists/file1.txt") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/file1.txt")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/file2.doc") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/file2.doc")) );
|
||||||
|
|
||||||
QVERIFY( fs.createDirectory("update-file-lists/sub-dir1") );
|
QVERIFY( fs.createDirectory(dirName + QStringLiteral("/sub-dir1")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/sub-dir1/file3.txt") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir1/file3.txt")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/sub-dir1/file4.doc") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir1/file4.doc")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/sub-dir1/file5.txt") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir1/file5.txt")) );
|
||||||
|
|
||||||
QVERIFY( fs.createDirectory("update-file-lists/sub-dir2") );
|
QVERIFY( fs.createDirectory(dirName + QStringLiteral("/sub-dir2")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file6.txt") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file6.txt")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file7.txt") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file7.txt")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file8.doc") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file8.doc")) );
|
||||||
QVERIFY( fs.createFile("update-file-lists/sub-dir2/file9.doc") );
|
QVERIFY( fs.createFile(dirName + QStringLiteral("/sub-dir2/file9.doc")) );
|
||||||
|
|
||||||
// Actual test
|
// Actual test
|
||||||
|
|
||||||
QDir dir("update-file-lists");
|
QDir dir(fs.absoluteFilePath(dirName));
|
||||||
|
|
||||||
#if defined(Q_OS_WINCE)
|
#if defined(Q_OS_WINCE)
|
||||||
//no . and .. on these OS.
|
//no . and .. on these OS.
|
||||||
@ -1577,7 +1578,7 @@ void tst_QDir::updateFileLists()
|
|||||||
QCOMPARE(dir.count(), uint(4));
|
QCOMPARE(dir.count(), uint(4));
|
||||||
QCOMPARE(dir.entryInfoList().size(), 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.entryInfoList().size(), 3);
|
||||||
QCOMPARE(dir.count(), uint(3));
|
QCOMPARE(dir.count(), uint(3));
|
||||||
@ -1589,7 +1590,7 @@ void tst_QDir::updateFileLists()
|
|||||||
QCOMPARE(dir.entryList().size(), 2);
|
QCOMPARE(dir.entryList().size(), 2);
|
||||||
QCOMPARE(dir.count(), uint(2));
|
QCOMPARE(dir.count(), uint(2));
|
||||||
|
|
||||||
dir.setPath("update-file-lists");
|
dir.setPath(fs.absoluteFilePath(dirName));
|
||||||
dir = QDir(dir.path(),
|
dir = QDir(dir.path(),
|
||||||
"*.txt",
|
"*.txt",
|
||||||
QDir::Name | QDir::DirsLast,
|
QDir::Name | QDir::DirsLast,
|
||||||
@ -1608,7 +1609,7 @@ void tst_QDir::updateFileLists()
|
|||||||
QCOMPARE(dir.entryList(), QStringList() << "sub-dir1" << "sub-dir2" << "file1.txt");
|
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);
|
QDir dir2(dir);
|
||||||
|
|
||||||
|
@ -1439,20 +1439,14 @@ void tst_QCompleter::task247560_keyboardNavigation()
|
|||||||
void tst_QCompleter::QTBUG_14292_filesystem()
|
void tst_QCompleter::QTBUG_14292_filesystem()
|
||||||
{
|
{
|
||||||
FileSystem fs;
|
FileSystem fs;
|
||||||
QDir tmpDir = QDir::currentPath();
|
|
||||||
|
|
||||||
qsrand(QTime::currentTime().msec());
|
QVERIFY(fs.createDirectory(QStringLiteral("hello")));
|
||||||
QString d = "tst_QCompleter_" + QString::number(qrand());
|
QVERIFY(fs.createDirectory(QStringLiteral("holla")));
|
||||||
QVERIFY(fs.createDirectory(tmpDir.filePath(d)));
|
|
||||||
|
|
||||||
QVERIFY(tmpDir.cd(d));
|
|
||||||
QVERIFY(fs.createDirectory(tmpDir.filePath("hello")));
|
|
||||||
QVERIFY(fs.createDirectory(tmpDir.filePath("holla")));
|
|
||||||
|
|
||||||
QLineEdit edit;
|
QLineEdit edit;
|
||||||
QCompleter comp;
|
QCompleter comp;
|
||||||
QFileSystemModel model;
|
QFileSystemModel model;
|
||||||
model.setRootPath(tmpDir.path());
|
model.setRootPath(fs.path());
|
||||||
comp.setModel(&model);
|
comp.setModel(&model);
|
||||||
edit.setCompleter(&comp);
|
edit.setCompleter(&comp);
|
||||||
|
|
||||||
@ -1464,7 +1458,7 @@ void tst_QCompleter::QTBUG_14292_filesystem()
|
|||||||
QTRY_VERIFY(edit.hasFocus());
|
QTRY_VERIFY(edit.hasFocus());
|
||||||
|
|
||||||
QVERIFY(!comp.popup()->isVisible());
|
QVERIFY(!comp.popup()->isVisible());
|
||||||
edit.setText(tmpDir.path());
|
edit.setText(fs.path());
|
||||||
QTest::keyClick(&edit, '/');
|
QTest::keyClick(&edit, '/');
|
||||||
QTRY_VERIFY(comp.popup()->isVisible());
|
QTRY_VERIFY(comp.popup()->isVisible());
|
||||||
QCOMPARE(comp.popup()->model()->rowCount(), 2);
|
QCOMPARE(comp.popup()->model()->rowCount(), 2);
|
||||||
@ -1475,12 +1469,12 @@ void tst_QCompleter::QTBUG_14292_filesystem()
|
|||||||
QCOMPARE(comp.popup()->model()->rowCount(), 1);
|
QCOMPARE(comp.popup()->model()->rowCount(), 1);
|
||||||
QTest::keyClick(&edit, 'r');
|
QTest::keyClick(&edit, 'r');
|
||||||
QTRY_VERIFY(!comp.popup()->isVisible());
|
QTRY_VERIFY(!comp.popup()->isVisible());
|
||||||
QVERIFY(fs.createDirectory(tmpDir.filePath("hero")));
|
QVERIFY(fs.createDirectory(QStringLiteral("hero")));
|
||||||
QTRY_VERIFY(comp.popup()->isVisible());
|
QTRY_VERIFY(comp.popup()->isVisible());
|
||||||
QCOMPARE(comp.popup()->model()->rowCount(), 1);
|
QCOMPARE(comp.popup()->model()->rowCount(), 1);
|
||||||
QTest::keyClick(comp.popup(), Qt::Key_Escape);
|
QTest::keyClick(comp.popup(), Qt::Key_Escape);
|
||||||
QTRY_VERIFY(!comp.popup()->isVisible());
|
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
|
//there is no reason creating a file should open a popup, it did in Qt 4.7.0
|
||||||
QTest::qWait(60);
|
QTest::qWait(60);
|
||||||
QVERIFY(!comp.popup()->isVisible());
|
QVERIFY(!comp.popup()->isVisible());
|
||||||
@ -1497,7 +1491,7 @@ void tst_QCompleter::QTBUG_14292_filesystem()
|
|||||||
QVERIFY(QTest::qWaitForWindowActive(&w));
|
QVERIFY(QTest::qWaitForWindowActive(&w));
|
||||||
QVERIFY(!edit.hasFocus() && !comp.popup()->hasFocus());
|
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
|
//there is no reason creating a file should open a popup, it did in Qt 4.7.0
|
||||||
QTest::qWait(60);
|
QTest::qWait(60);
|
||||||
QVERIFY(!comp.popup()->isVisible());
|
QVERIFY(!comp.popup()->isVisible());
|
||||||
|
@ -38,13 +38,14 @@
|
|||||||
** $QT_END_LICENSE$
|
** $QT_END_LICENSE$
|
||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
// Helper functions for creating file-system hierarchies and cleaning up.
|
|
||||||
|
|
||||||
#ifndef QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
|
#ifndef QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
|
||||||
#define QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
|
#define QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
#include <QTemporaryDir>
|
||||||
|
#include <QScopedPointer>
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QFile>
|
#include <QFile>
|
||||||
|
|
||||||
@ -60,54 +61,37 @@
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct FileSystem
|
// QTemporaryDir-based helper class for creating file-system hierarchies and cleaning up.
|
||||||
|
class FileSystem
|
||||||
{
|
{
|
||||||
~FileSystem()
|
Q_DISABLE_COPY(FileSystem)
|
||||||
{
|
public:
|
||||||
Q_FOREACH(QString fileName, createdFiles)
|
FileSystem() : m_temporaryDir(FileSystem::tempFilePattern()) {}
|
||||||
QFile::remove(fileName);
|
|
||||||
|
|
||||||
Q_FOREACH(QString dirName, createdDirectories)
|
QString path() const { return m_temporaryDir.path(); }
|
||||||
currentDir.rmdir(dirName);
|
QString absoluteFilePath(const QString &fileName) const { return path() + QLatin1Char('/') + fileName; }
|
||||||
}
|
|
||||||
|
|
||||||
bool createDirectory(const QString &dirName)
|
bool createDirectory(const QString &relativeDirName)
|
||||||
{
|
{
|
||||||
if (currentDir.mkdir(dirName)) {
|
if (m_temporaryDir.isValid()) {
|
||||||
createdDirectories.prepend(dirName);
|
QDir dir(m_temporaryDir.path());
|
||||||
return true;
|
return dir.mkpath(relativeDirName);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool createFile(const QString &fileName)
|
bool createFile(const QString &relativeFileName)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QScopedPointer<QFile> file(openFileForWrite(relativeFileName));
|
||||||
if (file.open(QIODevice::WriteOnly)) {
|
return !file.isNull();
|
||||||
createdFiles << fileName;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 createFileWithContent(const QString &fileName)
|
qint64 createFileWithContent(const QString &relativeFileName)
|
||||||
{
|
{
|
||||||
QFile file(fileName);
|
QScopedPointer<QFile> file(openFileForWrite(relativeFileName));
|
||||||
if (file.open(QIODevice::WriteOnly)) {
|
return file.isNull() ? qint64(-1) : file->write(relativeFileName.toUtf8());
|
||||||
createdFiles << fileName;
|
|
||||||
return file.write(fileName.toUtf8());
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
|
||||||
static void createNtfsJunction(QString target, QString linkName)
|
static void createNtfsJunction(QString target, QString linkName)
|
||||||
{
|
{
|
||||||
@ -163,10 +147,30 @@ struct FileSystem
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
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;
|
QFile *openFileForWrite(const QString &fileName) const
|
||||||
QStringList createdFiles;
|
{
|
||||||
|
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
|
#endif // include guard
|
||||||
|
Loading…
x
Reference in New Issue
Block a user