mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-05 10:34:13 +02:00
379cb72941
The test set the elements folder in a settings file under XDG_CONFIG_HOME, which macOS does not read, so common:// paths did not resolve and the first placement failed. It now passes --common-elements-dir.
91 lines
3.3 KiB
C++
91 lines
3.3 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#include <QtTest>
|
|
|
|
#include <QFile>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QProcess>
|
|
#include <QProcessEnvironment>
|
|
#include <QTemporaryDir>
|
|
|
|
// Placing the same symbol twice in lmdg.qet: its embedded collection has a
|
|
// "k_elem" category before "import". Imported symbols were filed under the
|
|
// first category while every lookup is under "import/", so the second
|
|
// placement could not find the first copy, tried to import it again, and
|
|
// failed -- in a script (qet.addElement() returned "") and in the editor,
|
|
// where the drop silently placed nothing.
|
|
class tst_importcategory : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
QTemporaryDir m_dir;
|
|
|
|
// Run @p script on @p project in a sandbox of its own and return the
|
|
// JSON object it logged.
|
|
QJsonObject run(const QString &script, const QString &project)
|
|
{
|
|
const QString path = m_dir.filePath(QStringLiteral("probe.js"));
|
|
const QString home = m_dir.filePath(QStringLiteral("home"));
|
|
QFile f(path);
|
|
if (!f.open(QIODevice::WriteOnly)) return {};
|
|
f.write(script.toUtf8());
|
|
f.close();
|
|
|
|
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
|
env.insert(QStringLiteral("QT_QPA_PLATFORM"), QStringLiteral("offscreen"));
|
|
env.insert(QStringLiteral("QET_ENABLE_SCRIPTING"), QStringLiteral("1"));
|
|
env.insert(QStringLiteral("HOME"), home);
|
|
env.insert(QStringLiteral("XDG_CONFIG_HOME"), home + QStringLiteral("/config"));
|
|
env.insert(QStringLiteral("XDG_DATA_HOME"), home + QStringLiteral("/data"));
|
|
env.insert(QStringLiteral("TMPDIR"), m_dir.path());
|
|
QProcess proc;
|
|
proc.setProcessEnvironment(env);
|
|
//The shipped collection, so common:// paths resolve. Given
|
|
//on the command line: macOS does not read a settings file
|
|
//under XDG_CONFIG_HOME.
|
|
proc.start(QStringLiteral(QET_TEST_BINARY_PATH),
|
|
{QStringLiteral("--common-elements-dir=" QET_ELEMENTS_DIR "/"),
|
|
QStringLiteral("--run"), path, project});
|
|
if (!proc.waitForFinished(120000)) return {};
|
|
const QString out = QString::fromUtf8(proc.readAllStandardOutput()
|
|
+ proc.readAllStandardError());
|
|
const QString mark = QStringLiteral("PROBE ");
|
|
for (const QString &line : out.split(QLatin1Char('\n'))) {
|
|
const int i = line.indexOf(mark);
|
|
if (i >= 0)
|
|
return QJsonDocument::fromJson(line.mid(i + mark.size()).toUtf8()).object();
|
|
}
|
|
return {};
|
|
}
|
|
|
|
private slots:
|
|
void initTestCase()
|
|
{
|
|
QVERIFY(m_dir.isValid());
|
|
QVERIFY(QFile::exists(QStringLiteral(QET_TEST_BINARY_PATH)));
|
|
}
|
|
|
|
void sameSymbolTwiceInLmdg()
|
|
{
|
|
const QString project = m_dir.filePath(QStringLiteral("lmdg.qet"));
|
|
QVERIFY(QFile::copy(QStringLiteral(QET_EXAMPLES_DIR "/lmdg.qet"), project));
|
|
|
|
const QJsonObject r = run(QStringLiteral(
|
|
"var p = 'common://10_electric/10_allpole/310_relays_contactors_contacts/"
|
|
"01_coils/bobine_ka_a_remanence.elmt';\n"
|
|
"var a = qet.addElement(0, p, 100, 100);\n"
|
|
"var b = qet.addElement(0, p, 300, 100);\n"
|
|
"qet.log('PROBE ' + JSON.stringify({first: a, second: b}));\n"),
|
|
project);
|
|
QVERIFY2(!r.isEmpty(), "the script logged nothing");
|
|
QVERIFY2(!r.value(QStringLiteral("first")).toString().isEmpty(),
|
|
"the first placement failed");
|
|
QVERIFY2(!r.value(QStringLiteral("second")).toString().isEmpty(),
|
|
"the second placement of the same symbol failed");
|
|
}
|
|
};
|
|
|
|
QTEST_GUILESS_MAIN(tst_importcategory)
|
|
|
|
#include "tst_importcategory.moc"
|