forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -1473,6 +1473,26 @@ void CommandLine::addCommandLineAsArgs(const CommandLine &cmd, RawType)
|
||||
addArgs(cmd.arguments(), Raw);
|
||||
}
|
||||
|
||||
void CommandLine::addCommandLineAsSingleArg(const CommandLine &cmd)
|
||||
{
|
||||
QString combined;
|
||||
ProcessArgs::addArg(&combined, cmd.executable().path());
|
||||
ProcessArgs::addArgs(&combined, cmd.arguments());
|
||||
|
||||
addArg(combined);
|
||||
}
|
||||
|
||||
void CommandLine::addCommandLineWithAnd(const CommandLine &cmd)
|
||||
{
|
||||
if (m_executable.isEmpty()) {
|
||||
*this = cmd;
|
||||
return;
|
||||
}
|
||||
|
||||
addArgs("&&", Raw);
|
||||
addCommandLineAsArgs(cmd);
|
||||
}
|
||||
|
||||
void CommandLine::addArgs(const QString &inArgs, RawType)
|
||||
{
|
||||
ProcessArgs::addArgs(&m_arguments, inArgs);
|
||||
|
@@ -132,6 +132,9 @@ public:
|
||||
void addCommandLineAsArgs(const CommandLine &cmd);
|
||||
void addCommandLineAsArgs(const CommandLine &cmd, RawType);
|
||||
|
||||
void addCommandLineAsSingleArg(const CommandLine &cmd);
|
||||
void addCommandLineWithAnd(const CommandLine &cmd);
|
||||
|
||||
QString toUserOutput() const;
|
||||
QString displayName() const;
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
add_subdirectory(ansiescapecodehandler)
|
||||
add_subdirectory(asynctask)
|
||||
add_subdirectory(commandline)
|
||||
add_subdirectory(deviceshell)
|
||||
add_subdirectory(fileutils)
|
||||
add_subdirectory(fsengine)
|
||||
@@ -10,6 +11,6 @@ add_subdirectory(persistentsettings)
|
||||
add_subdirectory(qtcprocess)
|
||||
add_subdirectory(settings)
|
||||
add_subdirectory(stringutils)
|
||||
add_subdirectory(templateengine)
|
||||
add_subdirectory(tasktree)
|
||||
add_subdirectory(templateengine)
|
||||
add_subdirectory(treemodel)
|
||||
|
4
tests/auto/utils/commandline/CMakeLists.txt
Normal file
4
tests/auto/utils/commandline/CMakeLists.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
add_qtc_test(tst_utils_commandline
|
||||
DEPENDS Utils app_version
|
||||
SOURCES tst_commandline.cpp
|
||||
)
|
12
tests/auto/utils/commandline/commandline.qbs
Normal file
12
tests/auto/utils/commandline/commandline.qbs
Normal file
@@ -0,0 +1,12 @@
|
||||
Project {
|
||||
QtcAutotest {
|
||||
name: "CommandLine autotest"
|
||||
|
||||
Depends { name: "Utils" }
|
||||
Depends { name: "app_version_header" }
|
||||
|
||||
files: [
|
||||
"tst_commandline.cpp",
|
||||
]
|
||||
}
|
||||
}
|
59
tests/auto/utils/commandline/tst_commandline.cpp
Normal file
59
tests/auto/utils/commandline/tst_commandline.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
// 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"
|
@@ -5,6 +5,7 @@ Project {
|
||||
references: [
|
||||
"ansiescapecodehandler/ansiescapecodehandler.qbs",
|
||||
"asynctask/asynctask.qbs",
|
||||
"commandline/commandline.qbs",
|
||||
"deviceshell/deviceshell.qbs",
|
||||
"fileutils/fileutils.qbs",
|
||||
"fsengine/fsengine.qbs",
|
||||
|
Reference in New Issue
Block a user