Files
qt-creator/tests/auto/utils/commandline/tst_commandline.cpp
Marcus Tillmanns 3e6c3d9fe7 Utils: Add "addCommandLine..." functions
addCommandLineAsSingleArg allows to reliably create commandlines
like "bash -c 'echo ...'"

addCommandLineWithAnd combines two command lines
by adding '&&' in between

Change-Id: Ic5af34c90fd5271dced40ba1341a3df019ededb8
Reviewed-by: hjk <hjk@qt.io>
2022-11-22 13:17:00 +00:00

60 lines
1.7 KiB
C++

// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include <utils/commandline.h>
#include <QObject>
#include <QtTest>
using namespace Utils;
class tst_CommandLine : public QObject
{
Q_OBJECT
private:
private slots:
void initTestCase() {}
void cleanupTestCase() {}
void testAnd()
{
CommandLine cmd("echo", {"foo"});
CommandLine cmd2("echo", {"bar", "blizz"});
cmd.addCommandLineWithAnd(cmd2);
QCOMPARE(cmd.toUserOutput(), QString("echo foo && echo bar blizz"));
}
void testAndComplex()
{
CommandLine cmd("/tmp/space path/\"echo", {"foo", "long with space"});
CommandLine cmd2("/tmp/space \"path/echo", {"bar\"", "blizz is 'great"});
cmd.addCommandLineWithAnd(cmd2);
QCOMPARE(cmd.toUserOutput(),
QString("/tmp/space path/\"echo foo 'long with space' && '/tmp/space \"path/echo' "
"'bar\"' 'blizz is '\\''great'"));
}
void testAndAdd()
{
CommandLine cmd("/tmp/space path/\"echo", {"foo", "long with space"});
CommandLine cmd2("/tmp/space \"path/echo", {"bar\"", "blizz is 'great"});
cmd.addCommandLineWithAnd(cmd2);
CommandLine shell("bash", {"-c"});
shell.addCommandLineAsSingleArg(cmd);
QCOMPARE(shell.toUserOutput(),
"bash -c ''\\''/tmp/space path/\"echo'\\'' foo '\\''long with space'\\'' && "
"'\\''/tmp/space \"path/echo'\\'' '\\''bar\"'\\'' '\\''blizz is "
"'\\''\\'\\'''\\''great'\\'''");
}
};
QTEST_GUILESS_MAIN(tst_CommandLine)
#include "tst_commandline.moc"