1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// SPDX-FileCopyrightText: 2022 Gleb Popov <arrowd@FreeBSD.org>
// SPDX-License-Identifier: BSD-3-Clause
#include "test_craftruntime.h"
#include <QFile>
#include <QProcess>
#include <QStandardPaths>
#include <QTest>
#include <KIO/CopyJob>
#include <KProcess>
#include <tests/testcore.h>
#include <tests/testhelpers.h>
#include "../craftruntime.h"
using namespace KDevelop;
QTEST_MAIN(CraftRuntimeTest)
class TempDirWrapper
{
public:
TempDirWrapper() = default;
TempDirWrapper(const QString& craftRoot, const QString& pythonExecutable)
: m_tempCraftRoot(new QTemporaryDir())
{
QVERIFY(m_tempCraftRoot->isValid());
copyCraftRoot(craftRoot);
m_runtime = std::make_shared<CraftRuntime>(m_tempCraftRoot->path(), pythonExecutable);
}
QString path() const
{
QVERIFY_RETURN(m_tempCraftRoot, QString());
return m_tempCraftRoot->path();
}
CraftRuntime* operator->() const
{
QVERIFY_RETURN(m_runtime, nullptr);
return m_runtime.get();
}
private:
void copyCraftRoot(const QString& oldRoot) const
{
const QLatin1String craftSettingsRelativePath("/etc/CraftSettings.ini");
const QDir dest(m_tempCraftRoot->path());
auto* job = KIO::copy(QUrl::fromLocalFile(oldRoot + QLatin1String("/craft")), QUrl::fromLocalFile(dest.path()));
QVERIFY(job->exec());
QVERIFY(dest.mkpath(QLatin1String("bin")));
QVERIFY(dest.mkpath(QLatin1String("etc")));
QVERIFY(QFile::copy(oldRoot + craftSettingsRelativePath, dest.path() + craftSettingsRelativePath));
}
std::shared_ptr<CraftRuntime> m_runtime;
std::shared_ptr<QTemporaryDir> m_tempCraftRoot;
};
Q_DECLARE_METATYPE(TempDirWrapper)
// When this test itself is ran under a Craft root, its environment gets in the way
static void breakoutFromCraftRoot()
{
auto craftRoot = qgetenv("KDEROOT");
if (craftRoot.isEmpty())
return;
auto paths = qgetenv("PATH").split(':');
const auto it = std::remove_if(paths.begin(), paths.end(), [craftRoot](const QByteArray& path) {
return path.startsWith(craftRoot);
});
paths.erase(it, paths.end());
qputenv("PATH", paths.join(':'));
qunsetenv("KDEROOT");
qunsetenv("craftRoot");
}
void CraftRuntimeTest::initTestCase_data()
{
breakoutFromCraftRoot();
const QString pythonExecutable = CraftRuntime::findPython();
if (pythonExecutable.isEmpty())
QSKIP("No python found, skipping kdevcraft tests.");
QTest::addColumn<TempDirWrapper>("runtimeInstance");
QTest::newRow("Mock") << TempDirWrapper(QStringLiteral(CRAFT_ROOT_MOCK), pythonExecutable);
auto craftRoot = CraftRuntime::findCraftRoot(Path(QStringLiteral(".")));
if (!craftRoot.isEmpty())
QTest::newRow("Real") << TempDirWrapper(craftRoot, pythonExecutable);
}
void CraftRuntimeTest::testFindCraftRoot()
{
QFETCH_GLOBAL(TempDirWrapper, runtimeInstance);
QCOMPARE(CraftRuntime::findCraftRoot(Path(runtimeInstance.path())), runtimeInstance.path());
QCOMPARE(CraftRuntime::findCraftRoot(Path(runtimeInstance.path()).cd(QStringLiteral("bin"))),
runtimeInstance.path());
}
void CraftRuntimeTest::testGetenv()
{
QFETCH_GLOBAL(TempDirWrapper, runtimeInstance);
QVERIFY(!runtimeInstance->getenv("KDEROOT").isEmpty());
QDir craftDir1 = QDir(QString::fromLocal8Bit(runtimeInstance->getenv("KDEROOT")));
QDir craftDir2 = QDir(runtimeInstance.path());
QCOMPARE(craftDir1.canonicalPath(), craftDir2.canonicalPath());
QString pythonpathValue = QString::fromLocal8Bit(runtimeInstance->getenv("PYTHONPATH"));
QVERIFY(!pythonpathValue.isEmpty());
QDir craftPythonPathDir = QDir(pythonpathValue);
QVERIFY(craftPythonPathDir.path().startsWith(craftDir1.path()));
}
void CraftRuntimeTest::testStartProcess()
{
QFETCH_GLOBAL(TempDirWrapper, runtimeInstance);
QString envPath = QStandardPaths::findExecutable(QStringLiteral("env"));
if (envPath.isEmpty())
QSKIP("Skipping startProcess() test, no \"env\" executable found");
QString envUnderCraftPath = runtimeInstance.path() + QStringLiteral("/bin/env");
QVERIFY(QFile::copy(envPath, envUnderCraftPath));
QProcess p;
p.setProgram(QStringLiteral("env"));
runtimeInstance->startProcess(&p);
// test that CraftRuntime::startProcess prefers programs under Craft root
QCOMPARE(QDir(p.program()).canonicalPath(), QDir(envUnderCraftPath).canonicalPath());
p.waitForFinished();
QVERIFY(QFile::remove(envUnderCraftPath));
}
void CraftRuntimeTest::testStartProcessEnv()
{
QFETCH_GLOBAL(TempDirWrapper, runtimeInstance);
QString printenvPath = QStandardPaths::findExecutable(QStringLiteral("printenv"));
if (printenvPath.isEmpty())
QSKIP("Skipping startProcess() test, no \"printenv\" executable found");
QString printenvUnderCraftPath = runtimeInstance.path() + QStringLiteral("/bin/printenv");
QVERIFY(QFile::copy(printenvPath, printenvUnderCraftPath));
KProcess p;
p.setProgram(QStringLiteral("printenv"), QStringList{QStringLiteral("PYTHONPATH")});
p.setOutputChannelMode(KProcess::OnlyStdoutChannel);
runtimeInstance->startProcess(&p);
p.waitForFinished();
QVERIFY(p.readAllStandardOutput().contains("site-packages"));
}
#include "moc_test_craftruntime.cpp"
|