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")) {
// 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.
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));
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,
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);
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);
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);
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());
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());
** $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>
#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)
{
#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;
+ }
+
+ 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;
+ }
- QStringList createdDirectories;
- QStringList createdFiles;
+ QTemporaryDir m_temporaryDir;
};
#endif // include guard