98 lines
3.0 KiB
C++
98 lines
3.0 KiB
C++
#include "testenv.h"
|
|
|
|
#include "launcherpaths.h"
|
|
#include "localization.h"
|
|
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QTemporaryDir>
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
namespace {
|
|
|
|
const char kMarker[] = ".bilauncher-test-sandbox";
|
|
|
|
QTemporaryDir *g_sandbox = nullptr;
|
|
bool g_realProfile = false; // Windows: работаем в настоящем профиле под меткой
|
|
|
|
[[noreturn]] void abortSetup(const QString &message)
|
|
{
|
|
std::fprintf(stderr, "TestEnv: %s\n", qPrintable(message));
|
|
std::exit(2);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void TestEnv::prepareSandbox()
|
|
{
|
|
g_sandbox = new QTemporaryDir(QDir::tempPath() + QStringLiteral("/bilauncher-test-XXXXXX"));
|
|
if (!g_sandbox->isValid())
|
|
abortSetup(QStringLiteral("не удалось создать временную папку"));
|
|
|
|
const QByteArray home = QDir::toNativeSeparators(g_sandbox->path()).toLocal8Bit();
|
|
qputenv("HOME", home);
|
|
qputenv("USERPROFILE", home);
|
|
// Чужая Java из окружения машины не должна влиять на выбор Java в тестах.
|
|
qunsetenv("JAVA_HOME");
|
|
|
|
std::atexit([] {
|
|
if (g_realProfile)
|
|
QDir(LauncherPaths::rootDir()).removeRecursively();
|
|
delete g_sandbox;
|
|
g_sandbox = nullptr;
|
|
});
|
|
}
|
|
|
|
void TestEnv::finishSetup()
|
|
{
|
|
QCoreApplication::setApplicationName(QStringLiteral("BiLauncher"));
|
|
QCoreApplication::setOrganizationName(QStringLiteral("GaleonDev"));
|
|
|
|
const QString root = QDir::cleanPath(LauncherPaths::rootDir());
|
|
const QString sandbox = QDir::cleanPath(g_sandbox->path());
|
|
if (!root.startsWith(sandbox + u'/')) {
|
|
#if defined(Q_OS_WIN)
|
|
if (QDir(root).exists() && !QFile::exists(root + u'/' + QLatin1String(kMarker))) {
|
|
abortSetup(QStringLiteral(
|
|
"%1 уже существует и создана не тестами — отказываюсь её трогать. "
|
|
"Запускайте тесты под отдельной учётной записью.")
|
|
.arg(QDir::toNativeSeparators(root)));
|
|
}
|
|
g_realProfile = true;
|
|
resetLauncherRoot();
|
|
#else
|
|
abortSetup(QStringLiteral("папка лаунчера %1 вне песочницы %2").arg(root, sandbox));
|
|
#endif
|
|
}
|
|
|
|
if (!Localization::instance().load())
|
|
abortSetup(QStringLiteral("не загрузился каталог переводов"));
|
|
Localization::instance().setLanguage(QStringLiteral("en"));
|
|
}
|
|
|
|
QString TestEnv::homeDir()
|
|
{
|
|
return g_sandbox->path();
|
|
}
|
|
|
|
QString TestEnv::freshDir(const QString &name)
|
|
{
|
|
const QString path = g_sandbox->path() + QStringLiteral("/work/") + name;
|
|
QDir(path).removeRecursively();
|
|
QDir().mkpath(path);
|
|
return path;
|
|
}
|
|
|
|
void TestEnv::resetLauncherRoot()
|
|
{
|
|
const QString root = LauncherPaths::rootDir();
|
|
QDir(root).removeRecursively();
|
|
if (g_realProfile) {
|
|
QDir().mkpath(root);
|
|
QFile marker(root + u'/' + QLatin1String(kMarker));
|
|
marker.open(QIODevice::WriteOnly);
|
|
}
|
|
}
|