mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-10-03 09:14:12 +02:00
ec262d1af9
XmlElementCollection::importCategory() returned the project collection's first category, taken to be "import". A project can hold other top-level categories before it: examples/lmdg.qet has "k_elem" first. Imported symbols were then filed under k_elem/, while every lookup of an imported symbol is under import/ (QETProject::importElement(), XmlElementCollection::addElement()). The first placement worked; the second could not find the copy, tried to import it again and failed. In the editor the drop silently placed nothing; a script's qet.addElement() returned "". Look the category up by name, falling back to the first one as before when a project has none called "import". Test: tst_importcategory places the same symbol twice in a copy of lmdg.qet through --run. It fails without the fix (the second placement returns "") and passes with it. ctest 45/45. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
96 lines
3.5 KiB
C++
96 lines
3.5 KiB
C++
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
#include <QtTest>
|
|
|
|
#include <QDir>
|
|
#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"));
|
|
QDir().mkpath(home + QStringLiteral("/config/QElectroTech"));
|
|
//The shipped collection, so common:// paths resolve; forward
|
|
//slashes, as Qt reads a backslash here as an escape.
|
|
QFile conf(home + QStringLiteral("/config/QElectroTech/QElectroTech.conf"));
|
|
if (!conf.open(QIODevice::WriteOnly)) return {};
|
|
conf.write("[elements-collections]\ncommon-collection-path="
|
|
QET_ELEMENTS_DIR "\n");
|
|
conf.close();
|
|
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);
|
|
proc.start(QStringLiteral(QET_TEST_BINARY_PATH),
|
|
{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"
|