Files
qelectrotech-source-mirror/tests/qttest/tst_qetstrings.cpp
ispyisail fcd2a4e0e0 Fix bugtracker #248: opening a file while QET is running does nothing
QET::splitWithSpaces() split on QRegularExpression("[^\\]?(?:\\\\)* ").
That is not a valid pattern: "[^\\]" opens a character class whose "\\]" is
an escaped bracket, so the class is never closed. QRegularExpression
reported isValid() == false, QString::split() warned "invalid
QRegularExpression object", and the function returned an EMPTY list for
every input.

It is the receiving half of the SingleApplication handshake: a secondary
instance sends "launched-with-args: " + joinWithSpaces(args) (main.cpp) and
the running instance parses it in QETApp::receiveMessage() before calling
openFiles(). With the split always empty, the running instance received no
arguments at all -- so opening a project while QET was already running
silently did nothing.

The bug is reported against filenames containing spaces, which is how it
was noticed, but it is not limited to them: plain names failed identically.

A corrected regex is not available. The separator is a space preceded by an
even-length run of backslashes, and PCRE2 has no variable-length lookbehind,
so the run cannot be expressed in a lookbehind and anything that matches it
by consumption eats the character before the space -- which is what the
"[^\\]?" was for. Scanning the string explicitly is correct and easier to
read.

tests/qttest/tst_qetstrings.cpp asserts the round trip
splitWithSpaces(joinWithSpaces(x)) == x over plain names, embedded spaces,
embedded backslashes, a trailing backslash and a mixture, plus the specific
regression that a plain argument list does not come back empty.

Verified the test fails without the fix: 9 of 11 cases fail on the old
implementation and all 11 pass with it. Full suite 5/5, Qt 5.15.18.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 14:55:17 +12:00

55 lines
2.0 KiB
C++

#include <QtTest>
#include "qet.h"
/**
QET::joinWithSpaces() / QET::splitWithSpaces() are the wire format for the
SingleApplication message a secondary instance sends to the running one
(main.cpp: "launched-with-args: " + joinWithSpaces(...), received by
QETApp::receiveMessage()). If the round trip loses arguments, opening a
file while QET is already running silently does nothing.
splitWithSpaces() used to split on QRegularExpression("[^\\]?(?:\\\\)* "),
which is not a valid pattern: "[^\\]" opens a character class whose "\\]"
is an escaped bracket, so the class never closes. QRegularExpression
reported isValid() == false and QString::split() returned an empty list for
every input -- bugtracker #248.
*/
class tst_qetstrings : public QObject
{
Q_OBJECT
private slots:
void roundTrips_data()
{
QTest::addColumn<QStringList>("input");
QTest::newRow("single plain") << QStringList{"one.qet"};
QTest::newRow("two plain") << QStringList{"one.qet", "two.qet"};
QTest::newRow("space in name") << QStringList{"my file.qet"};
QTest::newRow("space then plain") << QStringList{"my file.qet", "other.qet"};
QTest::newRow("spaces in path") << QStringList{"/home/a b/c d.qet", "/tmp/x.qet"};
QTest::newRow("backslash in name") << QStringList{"back\\slash.qet"};
QTest::newRow("trailing backslash") << QStringList{"trailing\\"};
QTest::newRow("mixed") << QStringList{"a b", "c\\d", "e"};
}
/// What the IPC actually needs: whatever went in comes back out.
void roundTrips()
{
QFETCH(QStringList, input);
QCOMPARE(QET::splitWithSpaces(QET::joinWithSpaces(input)), input);
}
/// The specific regression: the old implementation returned an empty list
/// for every input, so this passed nothing on to openFiles().
void splitIsNotEmptyForPlainArguments()
{
QVERIFY(!QET::splitWithSpaces(QStringLiteral("one.qet")).isEmpty());
QCOMPARE(QET::splitWithSpaces(QStringLiteral("a.qet b.qet")).count(), 2);
}
};
QTEST_APPLESS_MAIN(tst_qetstrings)
#include "tst_qetstrings.moc"