Files
BiLauncher/tests/unit/tst_launcherpaths.cpp
T
galeon 0dd6bef715
tests / windows (push) Skipped
tests / macos (push) Skipped
tests / package-smoke-linux (push) Skipped
tests / package-smoke-windows (push) Skipped
tests / package-smoke-macos (push) Skipped
tests / linux (push) Successful in 1m59s
test fixes
Signed-off-by: galeon <dertiellis@gmail.com>
2026-10-13 08:51:07 +03:00

120 lines
5.3 KiB
C++

#include "testenv.h"
#include "launcherpaths.h"
#include "localization.h"
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QScopeGuard>
class TestLauncherPaths : public QObject
{
Q_OBJECT
private slots:
void init() { TestEnv::resetLauncherRoot(); }
void rootIsInsideContainer()
{
QVERIFY(LauncherPaths::rootDir().startsWith(LauncherPaths::containerDir() + u'/'));
QCOMPARE(QFileInfo(LauncherPaths::rootDir()).fileName(), QStringLiteral("BiLauncher"));
}
void containerPerPlatform()
{
// Не QDir::homePath(): на Windows он смотрит в настоящий профиль, а пути
// лаунчера в тестах идут от BILAUNCHER_HOME (песочницы).
const QString home = QDir::cleanPath(TestEnv::homeDir());
#if defined(Q_OS_WIN)
QCOMPARE(LauncherPaths::containerDir(), home + QStringLiteral("/AppData/Roaming"));
QCOMPARE(LauncherPaths::defaultMinecraftDir(), LauncherPaths::containerDir() + "/.minecraft");
#elif defined(Q_OS_MACOS)
QCOMPARE(LauncherPaths::containerDir(), home + QStringLiteral("/Library/Application Support"));
QCOMPARE(LauncherPaths::defaultMinecraftDir(), LauncherPaths::containerDir() + "/minecraft");
#else
QCOMPARE(LauncherPaths::containerDir(), home);
QCOMPARE(LauncherPaths::defaultMinecraftDir(), home + "/.minecraft");
#endif
}
void homeOverrideFromEnvironment()
{
const QString saved = qEnvironmentVariable("BILAUNCHER_HOME");
const auto restore = qScopeGuard([&] { TestEnv::setEnvPath("BILAUNCHER_HOME", saved); });
QCOMPARE(LauncherPaths::homeOverride(), QDir::cleanPath(TestEnv::homeDir()));
const QString other = TestEnv::freshDir("other-home");
TestEnv::setEnvPath("BILAUNCHER_HOME", QDir::toNativeSeparators(other));
QCOMPARE(LauncherPaths::homeOverride(), QDir::cleanPath(other));
QVERIFY(LauncherPaths::rootDir().startsWith(QDir::cleanPath(other) + u'/'));
// Относительный путь зависел бы от текущей папки — игнорируется, как и пустой.
for (const char *ignored : {"relative/home", ""}) {
qputenv("BILAUNCHER_HOME", ignored);
QVERIFY2(LauncherPaths::homeOverride().isEmpty(), ignored);
}
}
void filesLayout_data()
{
QTest::addColumn<QString>("actual");
QTest::addColumn<QString>("relative");
QTest::newRow("settings") << LauncherPaths::settingsFile() << "settings.json";
QTest::newRow("profiles") << LauncherPaths::profilesFile() << "profiles.json";
QTest::newRow("customBuilds") << LauncherPaths::customBuildsFile() << "customBuilds.json";
QTest::newRow("legacyBuilds") << LauncherPaths::legacyCustomBuildsFile() << "versions.json";
QTest::newRow("buildStorage") << LauncherPaths::buildStorageDir() << "builds";
QTest::newRow("buildDir") << LauncherPaths::buildDir(7) << "builds/7";
QTest::newRow("season") << LauncherPaths::seasonalStateFile(7) << "builds/7/season.json";
QTest::newRow("runtime") << LauncherPaths::runtimeDir() << "runtime";
QTest::newRow("java") << LauncherPaths::javaDir() << "java";
QTest::newRow("cache") << LauncherPaths::cacheDir() << "cache";
QTest::newRow("javaCatalog") << LauncherPaths::javaCatalogFile() << "cache/java_runtimes.json";
QTest::newRow("javaDownload") << LauncherPaths::javaDownloadDir() << "cache/java";
QTest::newRow("manifest") << LauncherPaths::versionManifestFile() << "cache/version_manifest.json";
QTest::newRow("seasonal") << LauncherPaths::seasonalCatalogFile() << "cache/seasonal_builds.json";
QTest::newRow("loaderCache") << LauncherPaths::loaderCacheFile("forge") << "cache/loader_forge.json";
QTest::newRow("loaderDownload") << LauncherPaths::loaderDownloadDir() << "cache/loaders";
}
void filesLayout()
{
QFETCH(QString, actual);
QFETCH(QString, relative);
QCOMPARE(actual, LauncherPaths::rootDir() + u'/' + relative);
}
void ensureRootCreatesTree()
{
QString error;
QVERIFY2(LauncherPaths::ensureRootExists(&error), qPrintable(error));
for (const QString &dir : {LauncherPaths::rootDir(), LauncherPaths::runtimeDir(),
LauncherPaths::cacheDir(), LauncherPaths::loaderDownloadDir(),
LauncherPaths::buildStorageDir(), LauncherPaths::javaDir(),
LauncherPaths::javaDownloadDir()}) {
QVERIFY2(QFileInfo(dir).isDir(), qPrintable(dir));
}
// Повторный вызов на готовом дереве — тоже успех.
QVERIFY(LauncherPaths::ensureRootExists(&error));
}
void ensureRootFailsWhenFileBlocksFolder()
{
QDir().mkpath(LauncherPaths::rootDir());
QFile blocker(LauncherPaths::cacheDir());
QVERIFY(blocker.open(QIODevice::WriteOnly));
blocker.close();
QString error;
QVERIFY(!LauncherPaths::ensureRootExists(&error));
QVERIFY(!error.isEmpty());
QVERIFY(error.contains(QDir::toNativeSeparators(LauncherPaths::cacheDir())));
}
};
BILAUNCHER_TEST_MAIN(TestLauncherPaths)
#include "tst_launcherpaths.moc"