summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2020-02-07 14:03:43 +0100
committerMårten Nordheim <marten.nordheim@qt.io>2020-03-24 16:20:03 +0100
commit737fe89691a3d8dfa779862c9daaa002e00f3dca (patch)
treedf7498f54d2a5d57ff5de93bd8630fe2b3ecaa3a /tests/auto/corelib/io
parentc4ef0b92d5fb2c621e880347bd48d01b6f31eb24 (diff)
Q{File,FileInfo,Dir}: add std::filesystem::path overloads
Add some overloads where (I thought) it makes sense for QDir and QFile to accept std::filesystem::path objects. Currently my thinking is to not add overloads for static functions where std::filesystem can already do the same job, e.g. create directory or file. Template and enable_if is needed due to both QString and std::filesystem::path being able to be constructed from string literals. The common shared code is currently in QFile because QDir had an implicit include of QFile, made explicit in this patch, and QFileInfo has an include to QFile as well. The QT_HAS_STD_FILESYSTEM macro is visible in user-code which I currently take advantage of in the tests, and users could too. Change-Id: I8d05d3c34c6c17e20972a6a2053862b8891d6c3c Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/io')
-rw-r--r--tests/auto/corelib/io/qdir/qdir.pro2
-rw-r--r--tests/auto/corelib/io/qdir/tst_qdir.cpp41
-rw-r--r--tests/auto/corelib/io/qfile/stdinprocess/stdinprocess.pro1
-rw-r--r--tests/auto/corelib/io/qfile/test.pro2
-rw-r--r--tests/auto/corelib/io/qfile/tst_qfile.cpp51
-rw-r--r--tests/auto/corelib/io/qfileinfo/qfileinfo.pro3
-rw-r--r--tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp93
7 files changed, 193 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qdir/qdir.pro b/tests/auto/corelib/io/qdir/qdir.pro
index a8b106e250..f6ff0d0421 100644
--- a/tests/auto/corelib/io/qdir/qdir.pro
+++ b/tests/auto/corelib/io/qdir/qdir.pro
@@ -12,3 +12,5 @@ contains(CONFIG, builtin_testdata): DEFINES += BUILTIN_TESTDATA
android:!android-embedded {
RESOURCES += android_testdata.qrc
}
+
+qtConfig(c++17): CONFIG += c++17
diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp
index 1162fb31e4..5462a63fab 100644
--- a/tests/auto/corelib/io/qdir/tst_qdir.cpp
+++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp
@@ -216,6 +216,8 @@ private slots:
void emptyDir();
void nonEmptyDir();
+ void stdfilesystem();
+
private:
#ifdef BUILTIN_TESTDATA
QString m_dataPath;
@@ -2403,6 +2405,45 @@ void tst_QDir::nonEmptyDir()
QVERIFY(!dir.isEmpty());
}
+void tst_QDir::stdfilesystem()
+{
+#if QT_CONFIG(cxx17_filesystem)
+ namespace fs = std::filesystem;
+ fs::path path(".");
+ QDir dir(path);
+ QCOMPARE(dir, QDir(QStringLiteral(".")));
+
+ path = path / "testdir" / "dir";
+ dir.setPath(path);
+
+ QCOMPARE(dir, QDir(QStringLiteral("./testdir/dir")));
+
+ auto fsPath = dir.filesystemPath();
+ QCOMPARE(QString::fromStdU16String(fsPath.u16string()), dir.path());
+ fsPath = dir.filesystemAbsolutePath();
+ QCOMPARE(QString::fromStdU16String(fsPath.u16string()), dir.absolutePath());
+ fsPath = dir.filesystemCanonicalPath();
+ QCOMPARE(QString::fromStdU16String(fsPath.u16string()), dir.canonicalPath());
+
+ QDir emptyPath(fs::path{});
+ QCOMPARE(emptyPath, QDir(QStringLiteral(".")));
+
+ {
+ // Test QDir ctor with filter and sorting reversed
+ QDir filteredDir(fs::path{"."} / "searchdir", "subdir*",
+ QDir::SortFlag::Reversed, QDir::Filter::Dirs);
+ QStringList entries = filteredDir.entryList();
+ QCOMPARE(entries, QStringList() << "subdir2" << "subdir1");
+ QCOMPARE(filteredDir.sorting(), QDir::SortFlag::Reversed);
+ QCOMPARE(filteredDir.filter(), QDir::Filter::Dirs);
+ QCOMPARE(filteredDir.nameFilters().length(), 1);
+ QCOMPARE(filteredDir.nameFilters().first(), "subdir*");
+ }
+#else
+ QSKIP("Not supported");
+#endif
+}
+
QTEST_MAIN(tst_QDir)
#include "tst_qdir.moc"
diff --git a/tests/auto/corelib/io/qfile/stdinprocess/stdinprocess.pro b/tests/auto/corelib/io/qfile/stdinprocess/stdinprocess.pro
index 512da8939b..bc48a9fb39 100644
--- a/tests/auto/corelib/io/qfile/stdinprocess/stdinprocess.pro
+++ b/tests/auto/corelib/io/qfile/stdinprocess/stdinprocess.pro
@@ -2,3 +2,4 @@ SOURCES += main.cpp
QT = core
load(qt_test_helper)
+CONFIG += c++17
diff --git a/tests/auto/corelib/io/qfile/test.pro b/tests/auto/corelib/io/qfile/test.pro
index 7a2767bf3c..47b778bc33 100644
--- a/tests/auto/corelib/io/qfile/test.pro
+++ b/tests/auto/corelib/io/qfile/test.pro
@@ -24,3 +24,5 @@ TESTDATA += \
resources/file1.ext1
win32:!winrt: QMAKE_USE += ole32 uuid
+
+CONFIG += c++17
diff --git a/tests/auto/corelib/io/qfile/tst_qfile.cpp b/tests/auto/corelib/io/qfile/tst_qfile.cpp
index 350bff0652..b8f0d29078 100644
--- a/tests/auto/corelib/io/qfile/tst_qfile.cpp
+++ b/tests/auto/corelib/io/qfile/tst_qfile.cpp
@@ -282,6 +282,8 @@ private slots:
void moveToTrash_data();
void moveToTrash();
+ void stdfilesystem();
+
private:
#ifdef BUILTIN_TESTDATA
QSharedPointer<QTemporaryDir> m_dataDir;
@@ -3812,5 +3814,54 @@ void tst_QFile::moveToTrash()
}
}
+void tst_QFile::stdfilesystem()
+{
+#if QT_CONFIG(cxx17_filesystem)
+ namespace fs = std::filesystem;
+ auto toFSPath = [](const QFile &file) { return fs::path(file.fileName().toStdU16String()); };
+ fs::path path { "./path" };
+ QFile file(path);
+ QCOMPARE(toFSPath(file), path);
+
+ QCOMPARE(path, file.filesystemFileName());
+
+ {
+ QFile parentedFile(path, this);
+ QCOMPARE(file.fileName(), parentedFile.fileName());
+ QCOMPARE(parentedFile.parent(), this);
+ }
+
+ path = path / "filename";
+ file.setFileName(path);
+ QCOMPARE(toFSPath(file), path);
+
+ path = "test-file";
+ file.setFileName(path);
+ QVERIFY(file.open(QIODevice::WriteOnly));
+ file.close();
+
+ path = "tile-fest";
+ QVERIFY(file.rename(path));
+ QVERIFY(fs::exists(path));
+ fs::path linkfile { "test-link" };
+ QVERIFY(file.link(linkfile));
+ QVERIFY(fs::exists(linkfile));
+
+ fs::path copyfile { "copy-file" };
+ QVERIFY(file.copy(copyfile));
+ QVERIFY(fs::exists(copyfile));
+
+ QFileDevice::Permissions p = QFile::permissions(path);
+ QVERIFY(p.testFlag(QFile::WriteUser) || p.testFlag(QFile::WriteOwner)); // some we know for sure
+ if (p.testFlag(QFile::ReadUser))
+ p.setFlag(QFile::ReadUser, false);
+ else if (p.testFlag(QFile::ReadOwner))
+ p.setFlag(QFile::ReadOwner, false);
+ QVERIFY(QFile::setPermissions(path, p));
+#else
+ QSKIP("Not supported");
+#endif
+}
+
QTEST_MAIN(tst_QFile)
#include "tst_qfile.moc"
diff --git a/tests/auto/corelib/io/qfileinfo/qfileinfo.pro b/tests/auto/corelib/io/qfileinfo/qfileinfo.pro
index d181d16a3e..af764f3679 100644
--- a/tests/auto/corelib/io/qfileinfo/qfileinfo.pro
+++ b/tests/auto/corelib/io/qfileinfo/qfileinfo.pro
@@ -6,3 +6,6 @@ RESOURCES += qfileinfo.qrc \
testdata.qrc
win32:!winrt: QMAKE_USE += advapi32 netapi32
+
+# for std::filesystem tests
+qtConfig(c++17): CONFIG += c++17
diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
index ebb9a0a44c..a1f9ca0b87 100644
--- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
+++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp
@@ -287,6 +287,8 @@ private slots:
void invalidState();
void nonExistingFile();
+ void stdfilesystem();
+
private:
const QString m_currentDir;
QString m_sourceFile;
@@ -2269,6 +2271,97 @@ void tst_QFileInfo::nonExistingFile()
stateCheck(info, dirname, filename);
}
+void tst_QFileInfo::stdfilesystem()
+{
+#if QT_CONFIG(cxx17_filesystem)
+
+ namespace fs = std::filesystem;
+
+ // Verify constructing with fs::path leads to valid objects
+ {
+ // We compare using absoluteFilePath since QFileInfo::operator== ends up using
+ // canonicalFilePath which evaluates to empty-string for non-existent paths causing
+ // these tests to always succeed.
+#define COMPARE_CONSTRUCTION(filepath) \
+ QCOMPARE(QFileInfo(fs::path(filepath)).absoluteFilePath(), \
+ QFileInfo(QString::fromLocal8Bit(filepath)).absoluteFilePath()); \
+ QCOMPARE(QFileInfo(base, fs::path(filepath)).absoluteFilePath(), \
+ QFileInfo(base, QString::fromLocal8Bit(filepath)).absoluteFilePath())
+
+ QDir base{ "../" }; // Used for the QFileInfo(QDir, <path>) ctor
+
+ COMPARE_CONSTRUCTION("./file");
+
+#ifdef Q_OS_WIN32
+ COMPARE_CONSTRUCTION("C:\\path\\to\\file.txt");
+ COMPARE_CONSTRUCTION("x:\\path/to\\file.txt");
+ COMPARE_CONSTRUCTION("D:/path/TO/file.txt");
+ COMPARE_CONSTRUCTION("//sharename/folder/file.txt");
+#endif
+ COMPARE_CONSTRUCTION("/path/TO/file.txt");
+ COMPARE_CONSTRUCTION("./path/TO/file.txt");
+ COMPARE_CONSTRUCTION("../file.txt");
+ COMPARE_CONSTRUCTION("./filæ.txt");
+
+#undef COMPARE_CONSTRUCTION
+ {
+ // One proper comparison with operator== for each ctor
+ QFile file(QStringLiteral("./filesystem_test_file.txt"));
+ if (!file.open(QFile::NewOnly))
+ QVERIFY(file.exists());
+ file.close();
+
+ QFileInfo pinfo{ fs::path{ "./filesystem_test_file.txt" } };
+ QFileInfo info{ QStringLiteral("./filesystem_test_file.txt") };
+ QCOMPARE(pinfo, info);
+ }
+
+ {
+ // And once more for QFileInfo(QDir, <path>)
+ const QString &subdir = QStringLiteral("./filesystem_test_dir/");
+ base = QDir(QStringLiteral("."));
+ if (!base.exists(subdir))
+ QVERIFY(base.mkdir(subdir));
+ base.cd(subdir);
+ QFile file{ base.filePath(QStringLiteral("./filesystem_test_file.txt")) };
+ if (!file.open(QFile::NewOnly))
+ QVERIFY(file.exists());
+ file.close();
+ QFileInfo pinfo{ base, fs::path{ "filesystem_test_file.txt" } };
+ QFileInfo info{ base, QStringLiteral("filesystem_test_file.txt") };
+ QCOMPARE(pinfo, info);
+ }
+ }
+
+ // Verify that functions returning path all point to the same place
+ {
+#define COMPARE_PATHS(actual, expected) \
+ QCOMPARE(QString::fromStdU16String(actual.u16string()), expected)
+
+ QFile file(QStringLiteral("./orig"));
+ if (!file.open(QFile::NewOnly))
+ QVERIFY(file.exists());
+ file.close();
+
+ QFileInfo info{ QStringLiteral("./orig") };
+ COMPARE_PATHS(info.filesystemPath(), info.path());
+ COMPARE_PATHS(info.filesystemAbsolutePath(), info.absolutePath());
+ COMPARE_PATHS(info.filesystemCanonicalPath(), info.canonicalPath());
+ COMPARE_PATHS(info.filesystemFilePath(), info.filePath());
+ COMPARE_PATHS(info.filesystemAbsoluteFilePath(), info.absoluteFilePath());
+ COMPARE_PATHS(info.filesystemCanonicalFilePath(), info.canonicalFilePath());
+
+ QVERIFY(file.link(QStringLiteral("./filesystem_test_symlink.lnk")));
+ info = QFileInfo{ "./filesystem_test_symlink.lnk" };
+
+ COMPARE_PATHS(info.filesystemSymLinkTarget(), info.symLinkTarget());
+#undef COMPARE_PATHS
+ }
+
+#else
+ QSKIP("Not supported");
+#endif
+}
QTEST_MAIN(tst_QFileInfo)
#include "tst_qfileinfo.moc"