diff options
Diffstat (limited to 'tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp')
-rw-r--r-- | tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp | 103 |
1 files changed, 79 insertions, 24 deletions
diff --git a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp index 7cad434906..84d0a505a4 100644 --- a/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp +++ b/tests/auto/corelib/io/qstandardpaths/tst_qstandardpaths.cpp @@ -1,6 +1,6 @@ // Copyright (C) 2020 The Qt Company Ltd. // Copyright (C) 2020 Intel Corporation. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <qstandardpaths.h> #include <QTest> @@ -27,7 +27,7 @@ using namespace Qt::StringLiterals; // Update this when adding new enum values; update enumNames too -static const int MaxStandardLocation = QStandardPaths::AppConfigLocation; +static const int MaxStandardLocation = QStandardPaths::GenericStateLocation; static QString genericCacheLoc() { @@ -38,6 +38,15 @@ static QString cacheLoc() return QStandardPaths::writableLocation(QStandardPaths::CacheLocation); } +static QString genericStateLoc() +{ + return QStandardPaths::writableLocation(QStandardPaths::GenericStateLocation); +} +static QString stateLoc() +{ + return QStandardPaths::writableLocation(QStandardPaths::StateLocation); +} + static QString genericDataLoc() { return QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); @@ -98,14 +107,17 @@ private: qputenv("XDG_CONFIG_DIRS", QFile::encodeName(m_globalConfigDir)); m_localAppDir = m_localAppTempDir.path(); m_globalAppDir = m_globalAppTempDir.path(); + m_stateDir = m_stateTempDir.path(); qputenv("XDG_DATA_HOME", QFile::encodeName(m_localAppDir)); qputenv("XDG_DATA_DIRS", QFile::encodeName(m_globalAppDir)); + qputenv("XDG_STATE_HOME", QFile::encodeName(m_stateDir)); } void setDefaultLocations() { qputenv("XDG_CONFIG_HOME", nullptr); qputenv("XDG_CONFIG_DIRS", nullptr); qputenv("XDG_DATA_HOME", nullptr); qputenv("XDG_DATA_DIRS", nullptr); + qputenv("XDG_STATE_HOME", nullptr); } #endif @@ -120,6 +132,8 @@ private: QTemporaryDir m_localAppTempDir; QString m_globalAppDir; QTemporaryDir m_globalAppTempDir; + QString m_stateDir; + QTemporaryDir m_stateTempDir; }; static const char * const enumNames[MaxStandardLocation + 1 - int(QStandardPaths::DesktopLocation)] = { @@ -141,7 +155,11 @@ static const char * const enumNames[MaxStandardLocation + 1 - int(QStandardPaths "GenericCacheLocation", "GenericConfigLocation", "AppDataLocation", - "AppConfigLocation" + "AppConfigLocation", + "PublicShareLocation", + "TemplatesLocation", + "StateLocation", + "GenericStateLocation" }; void tst_qstandardpaths::initTestCase() @@ -160,6 +178,7 @@ void tst_qstandardpaths::initTestCase() QVERIFY2(m_globalConfigTempDir.isValid(), qPrintable(m_globalConfigTempDir.errorString())); QVERIFY2(m_localAppTempDir.isValid(), qPrintable(m_localAppTempDir.errorString())); QVERIFY2(m_globalAppTempDir.isValid(), qPrintable(m_globalAppTempDir.errorString())); + QVERIFY2(m_stateTempDir.isValid(), qPrintable(m_stateTempDir.errorString())); } void tst_qstandardpaths::dump() @@ -205,6 +224,8 @@ void tst_qstandardpaths::testDefaultLocations() QCOMPARE(genericDataDirs.at(0), expectedDataHome); QCOMPARE(genericDataDirs.at(1), QString::fromLatin1("/usr/local/share")); QCOMPARE(genericDataDirs.at(2), QString::fromLatin1("/usr/share")); + const QString expectedGenericStateLocation = QDir::homePath() + QString::fromLatin1("/.local/state"); + QCOMPARE(genericStateLoc(), expectedGenericStateLocation); #endif } @@ -283,25 +304,34 @@ void tst_qstandardpaths::enableTestMode() // CacheLocation should be "GenericCacheLocation/organization-name/app-name" QCOMPARE(cacheLoc(), cacheDir + "/tst_qstandardpaths"_L1); + // *StateLocation + const QString stateDir = qttestDir + QLatin1String("/state"); + QCOMPARE(genericStateLoc(), stateDir); + const QStringList stateDirs = QStandardPaths::standardLocations(QStandardPaths::GenericStateLocation); + QCOMPARE(stateDirs, QStringList() << stateDir); + // StateLocation should be "GenericStateLocation/organization-name/app-name" + QCOMPARE(stateLoc(), stateDir + "/tst_qstandardpaths"_L1); + QCoreApplication::setOrganizationName("Qt"); QCOMPARE(appConfigLoc(), configDir + "/Qt/tst_qstandardpaths"_L1); QCOMPARE(appDataLoc(), dataDir + "/Qt/tst_qstandardpaths"_L1); QCOMPARE(appLocalDataLoc(), dataDir + "/Qt/tst_qstandardpaths"_L1); QCOMPARE(cacheLoc(), cacheDir + "/Qt/tst_qstandardpaths"_L1); - QCOMPARE(cacheLoc(), cacheDir + "/Qt/tst_qstandardpaths"_L1); + QCOMPARE(stateLoc(), stateDir + "/Qt/tst_qstandardpaths"_L1); QCoreApplication::setApplicationName("QtTest"); QCOMPARE(appConfigLoc(), configDir + "/Qt/QtTest"_L1); QCOMPARE(appDataLoc(), dataDir + "/Qt/QtTest"_L1); QCOMPARE(appLocalDataLoc(), dataDir + "/Qt/QtTest"_L1); - QCoreApplication::setApplicationName("QtTest"); QCOMPARE(cacheLoc(), cacheDir + "/Qt/QtTest"_L1); + QCOMPARE(stateLoc(), stateDir + "/Qt/QtTest"_L1); // Check these are unaffected by org/app names QCOMPARE(genericConfigLoc(), configDir); QCOMPARE(configLoc(), configDir); QCOMPARE(genericDataLoc(), dataDir); QCOMPARE(genericCacheLoc(), cacheDir); + QCOMPARE(genericStateLoc(), stateDir); #endif // On all platforms, we want to ensure that the writableLocation is different in test mode and real mode. @@ -315,6 +345,8 @@ void tst_qstandardpaths::enableTestMode() testLocations.insert(QStandardPaths::GenericConfigLocation, genericConfigLoc()); testLocations.insert(QStandardPaths::CacheLocation, cacheLoc()); testLocations.insert(QStandardPaths::GenericCacheLocation, genericCacheLoc()); + testLocations.insert(QStandardPaths::StateLocation, stateLoc()); + testLocations.insert(QStandardPaths::GenericStateLocation, genericStateLoc()); // On Windows, what should "Program Files" become, in test mode? //testLocations.insert(QStandardPaths::ApplicationsLocation, QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)); @@ -398,7 +430,7 @@ void tst_qstandardpaths::testAppConfigLocation() #endif } -#ifndef Q_OS_WIN +#if !defined(Q_OS_WIN) && !defined(Q_OS_WASM) // Find "sh" on Unix. // It may exist twice, in /bin/sh and /usr/bin/sh, in that case use the PATH order. static inline QFileInfo findSh() @@ -407,7 +439,7 @@ static inline QFileInfo findSh() QByteArray pEnv = qgetenv("PATH"); const QLatin1Char pathSep(':'); const QStringList rawPaths = QString::fromLocal8Bit(pEnv.constData()).split(pathSep, Qt::SkipEmptyParts); - foreach (const QString &path, rawPaths) { + for (const QString &path : rawPaths) { if (QFile::exists(path + sh)) return QFileInfo(path + sh); } @@ -452,6 +484,7 @@ void tst_qstandardpaths::testFindExecutable_data() << QString() << logo << logoPath; } #else +# ifndef Q_OS_WASM const QFileInfo shFi = findSh(); Q_ASSERT(shFi.exists()); const QString shPath = shFi.absoluteFilePath(); @@ -461,6 +494,7 @@ void tst_qstandardpaths::testFindExecutable_data() << QString() << shPath << shPath; QTest::newRow("unix-sh-relativepath") << QString(shFi.absolutePath()) << QString::fromLatin1("./sh") << shPath; +#endif /* !WASM */ #endif QTest::newRow("idontexist") << QString() << QString::fromLatin1("idontexist") << QString(); @@ -492,6 +526,9 @@ void tst_qstandardpaths::testFindExecutable() void tst_qstandardpaths::testFindExecutableLinkToDirectory() { +#ifdef Q_OS_WASM + QSKIP("No applicationdir on wasm"); +#else // link to directory const QString target = QDir::tempPath() + QDir::separator() + QLatin1String("link.lnk"); QFile::remove(target); @@ -499,9 +536,10 @@ void tst_qstandardpaths::testFindExecutableLinkToDirectory() QVERIFY(appFile.link(target)); QVERIFY(QStandardPaths::findExecutable(target).isEmpty()); QFile::remove(target); +#endif } -using RuntimeDirSetup = QString (*)(QDir &); +using RuntimeDirSetup = std::optional<QString> (*)(QDir &); Q_DECLARE_METATYPE(RuntimeDirSetup); void tst_qstandardpaths::testRuntimeDirectory() @@ -561,18 +599,18 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() QSKIP("Running this test as root doesn't make sense"); # endif - addRow("environment:non-existing", [](QDir &d) { + addRow("environment:non-existing", [](QDir &d) -> std::optional<QString> { return updateRuntimeDir(d.filePath("runtime")); }); - addRow("environment:existing", [](QDir &d) { + addRow("environment:existing", [](QDir &d) -> std::optional<QString> { QString p = d.filePath("runtime"); d.mkdir("runtime"); QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner); return updateRuntimeDir(p); }); - addRow("environment-to-existing-wrong-perm", [](QDir &d) { + addRow("environment-to-existing-wrong-perm", [](QDir &d) -> std::optional<QString> { QString p = d.filePath("runtime"); d.mkdir("runtime"); QFile::setPermissions(p, QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | @@ -585,7 +623,7 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() return fallbackXdgRuntimeDir(); }); - addRow("environment:wrong-owner", [](QDir &) { + addRow("environment:wrong-owner", [](QDir &) -> std::optional<QString> { QT_STATBUF st; QT_STAT("/", &st); @@ -600,10 +638,18 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() return fallbackXdgRuntimeDir(); }); - addRow("environment:file", [](QDir &d) { + // static so that it can be used in RuntimeDirSetup callable without capturing + static auto failedToOpen = [](const QFile &f) { + qCritical("QFile::Open: failed to open '%s': %s", + qPrintable(f.fileName()), qPrintable(f.errorString())); + return std::nullopt; + }; + + addRow("environment:file", [](QDir &d) -> std::optional<QString> { QString p = d.filePath("file"); QFile f(p); - f.open(QIODevice::WriteOnly); + if (!f.open(QIODevice::WriteOnly)) + return failedToOpen(f); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner); updateRuntimeDir(p); @@ -614,7 +660,7 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() return fallbackXdgRuntimeDir(); }); - addRow("environment:broken-symlink", [](QDir &d) { + addRow("environment:broken-symlink", [](QDir &d) -> std::optional<QString> { QString p = d.filePath("link"); QFile::link(d.filePath("this-goes-nowhere"), p); updateRuntimeDir(p); @@ -625,7 +671,7 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() return fallbackXdgRuntimeDir(); }); - addRow("environment:symlink-to-dir", [](QDir &d) { + addRow("environment:symlink-to-dir", [](QDir &d) -> std::optional<QString> { QString p = d.filePath("link"); d.mkdir("dir"); QFile::link(d.filePath("dir"), p); @@ -638,12 +684,12 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() return fallbackXdgRuntimeDir(); }); - addRow("no-environment:non-existing", [](QDir &) { + addRow("no-environment:non-existing", [](QDir &) -> std::optional<QString> { clearRuntimeDir(); return fallbackXdgRuntimeDir(); }); - addRow("no-environment:existing", [](QDir &d) { + addRow("no-environment:existing", [](QDir &d) -> std::optional<QString> { clearRuntimeDir(); QString p = fallbackXdgRuntimeDir(); d.mkdir(p); // probably has wrong permissions @@ -651,10 +697,11 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() return p; }); - addRow("no-environment:fallback-is-file", [](QDir &) { + addRow("no-environment:fallback-is-file", [](QDir &) -> std::optional<QString> { QString p = fallbackXdgRuntimeDir(); QFile f(p); - f.open(QIODevice::WriteOnly); + if (!f.open(QIODevice::WriteOnly)) + return failedToOpen(f); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner); clearRuntimeDir(); @@ -665,10 +712,11 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() return QString(); }); - addRow("environment-and-fallback-are-files", [](QDir &d) { + addRow("environment-and-fallback-are-files", [](QDir &d) -> std::optional<QString> { QString p = d.filePath("file1"); QFile f(p); - f.open(QIODevice::WriteOnly); + if (!f.open(QIODevice::WriteOnly)) + return failedToOpen(f); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup); updateRuntimeDir(p); QTest::ignoreMessage(QtWarningMsg, @@ -678,7 +726,8 @@ void tst_qstandardpaths::testCustomRuntimeDirectory_data() f.close(); f.setFileName(fallbackXdgRuntimeDir()); - f.open(QIODevice::WriteOnly); + if (!f.open(QIODevice::WriteOnly)) + return failedToOpen(f); f.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ReadGroup); QTest::ignoreMessage(QtWarningMsg, QString("QStandardPaths: runtime directory '%1' is not a directory, " @@ -718,7 +767,9 @@ void tst_qstandardpaths::testCustomRuntimeDirectory() qputenv("TMPDIR", QFile::encodeName(tempDir.path())); QFETCH(RuntimeDirSetup, setup); - QString expected = setup(d); + std::optional<QString> opt = setup(d); + QVERIFY(opt); + QString expected = *opt; QString runtimeDir = QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation); QCOMPARE(runtimeDir, expected); @@ -807,6 +858,10 @@ void tst_qstandardpaths::testXdgPathCleanup() const QString cacheDir = cacheLoc(); QCOMPARE_NE(cacheDir, relative); + qputenv("XDG_STATE_HOME", relative.toLatin1()); + const QString stateDir = stateLoc(); + QCOMPARE_NE(stateDir, relative); + qputenv("XDG_DATA_HOME", relative.toLatin1()); const QString localDataDir = genericDataLoc(); QCOMPARE_NE(localDataDir, relative); |