File: tst_commandline.cpp

package info (click to toggle)
qtcreator 18.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 261,240 kB
  • sloc: cpp: 1,581,554; ansic: 528,781; python: 48,245; xml: 34,678; javascript: 15,495; sh: 2,670; asm: 2,049; perl: 1,039; lex: 737; java: 695; objc: 409; makefile: 111; yacc: 86; cs: 41; ruby: 33; sed: 22
file content (325 lines) | stat: -rw-r--r-- 10,627 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <app/app_version.h>

#include <utils/commandline.h>
#include <utils/environment.h>
#include <utils/hostosinfo.h>
#include <utils/macroexpander.h>
#include <utils/processreaper.h>
#include <utils/qtcprocess.h>
#include <utils/temporarydirectory.h>

#include <QObject>
#include <QTest>

#include <iostream>

using namespace Utils;

FilePath self;

class tst_CommandLine : public QObject
{
    Q_OBJECT
private:
    Environment testEnv = Environment::systemEnvironment();
    QString newLine;

    QString run(const CommandLine &cmd)
    {
        Process p;
        p.setCommand(cmd);
        p.setEnvironment(testEnv);
        p.runBlocking();
        return QString::fromUtf8(p.rawStdOut());
    }

private slots:
    void initTestCase()
    {
        TemporaryDirectory::setMasterTemporaryDirectory(
            QDir::tempPath() + "/" + Core::Constants::IDE_CASED_ID + "-XXXXXX");

        testEnv.set("TEST_ECHO", "1");

        if (HostOsInfo::isWindowsHost())
            newLine = "\r\n";
        else
            newLine = "\n";
    }

    void cleanupTestCase() { ProcessReaper::deleteAll(); }

    void testSpace()
    {
        CommandLine cmd(self, {"With Space"});
        QString expected = "With Space" + newLine;
        QCOMPARE(run(cmd), expected);
    }

    void testQuote()
    {
        QStringList args = {"\"With <\"Quote\"> % ^^ \"", "Hallo ??"};
        CommandLine cmd(self, args);
        QString expected = args.join(newLine) + newLine;
        QCOMPARE(run(cmd), expected);
    }

    void testAnd()
    {
        QStringList args = {"foo", "bar", "baz"};
        CommandLine cmd(self, {args[0]});
        CommandLine cmd2(self, args.sliced(1));

        cmd.addCommandLineWithAnd(cmd2);

        QString expected = args.join(newLine) + newLine;

        QCOMPARE(run(cmd), expected);
    }

    void testAndComplex()
    {
        QStringList args = {"foo", "long with space", "bar\"", "blizz is 'great"};
        CommandLine cmd(self, args.sliced(0, 2));
        CommandLine cmd2(self, args.sliced(2, 2));

        cmd.addCommandLineWithAnd(cmd2);
        QString expected = args.join(newLine) + newLine;
        QString actual = run(cmd);
        QCOMPARE(actual, expected);
    }

    void testAndAdd()
    {
        if (HostOsInfo::isWindowsHost())
            QSKIP("The test does not yet work on Windows.");

        QStringList args = {"foo", "long with space", "bar", "blizz is great"};

        CommandLine cmd(self, args.sliced(0, 2));
        CommandLine cmd2(self, args.sliced(2, 2));

        cmd.addCommandLineWithAnd(cmd2);

        CommandLine shell;
        if (HostOsInfo::isWindowsHost()) {
            shell.setExecutable(FilePath::fromUserInput(qEnvironmentVariable("COMSPEC")));
            shell.addArgs({"/v:off", "/s", "/c"});
        } else {
            shell.setExecutable(FilePath::fromUserInput("/bin/sh"));
            shell.addArgs({"-c"});
        }

        shell.addCommandLineAsSingleArg(cmd);

        QString expected = args.join(newLine) + newLine;
        QString actual = run(shell);
        QCOMPARE(actual, expected);
    }

    void testConstructor_data()
    {
        QTest::addColumn<CommandLine>("command");
        QTest::addColumn<FilePath>("executable");
        QTest::addColumn<QStringList>("arguments");

        const FilePath filePath("some_path");
        const QString arg("-arg");
        const QStringList args{"-a", "-b", "-c"};

        QTest::newRow("mixed-strings") << CommandLine{filePath, {"-A", arg, args}}
                                       << filePath << (QStringList{"-A"} << arg << args);
    }

    void testConstructor()
    {
        QFETCH(CommandLine, command);
        QFETCH(FilePath, executable);
        QFETCH(QStringList, arguments);

        QCOMPARE(command.executable(), executable);
        QCOMPARE(command.arguments(), arguments.join(' '));
        QCOMPARE(command.splitArguments(), arguments);
    }

    void testFromUserInput_data()
    {
        QTest::addColumn<QString>("input");
        QTest::addColumn<QString>("executable");
        QTest::addColumn<QString>("arguments");

        QTest::newRow("empty") << ""
                               << ""
                               << "";
        QTest::newRow("command") << "command"
                                 << "command"
                                 << "";
        QTest::newRow("command-with-args") << "command and args"
                                           << "command"
                                           << "and args";

        if (!HostOsInfo::isWindowsHost()) {
            QTest::newRow("command-with-space-slash") << "command\\ with-space and args"
                                                      << "command with-space"
                                                      << "and args";
            QTest::newRow("command-with-space-single-quote") << "'command with-space' and args"
                                                             << "command with-space"
                                                             << "and args";
        }
        QTest::newRow("command-with-space-double-quote") << "\"command with-space\" and args"
                                                         << "command with-space"
                                                         << "and args";

        QTest::newRow("command-with-space-double-quote-in-name")
            << "\"command\\\"with-quote\" and args"
            << "command\"with-quote"
            << "and args";

        QTest::newRow("inside-space-quoted") << "command\" \"withspace args here"
                                             << "command withspace"
                                             << "args here";
    }

    void testFromUserInput()
    {
        QFETCH(QString, input);
        QFETCH(QString, executable);
        QFETCH(QString, arguments);

        CommandLine cmd = CommandLine::fromUserInput(input);
        QCOMPARE(cmd.executable(), FilePath::fromUserInput(executable));
        QCOMPARE(cmd.arguments(), arguments);
    }

    void testFromInputFails()
    {
        if (HostOsInfo::isWindowsHost())
            QSKIP("The test does not work on Windows.");

        CommandLine cmd = CommandLine::fromUserInput("command\\\\\\ with-space and args");
        QEXPECT_FAIL("",
                     "CommandLine::fromUserInput (and FilePath::fromUserInput) does not handle "
                     "backslashes correctly",
                     Continue);
        QCOMPARE(cmd.executable().fileName(), "command\\ with-space");
        QCOMPARE(cmd.arguments(), "and args");
    }

    void testFromInputWithMacro_data()
    {
        QTest::addColumn<QString>("input");
        QTest::addColumn<QString>("expectedExecutable");
        QTest::addColumn<QString>("expectedArguments");

        QTest::newRow("simple") << "command %{hello}"
                                << "command"
                                << "hello world";

        QTest::newRow("simple-quoted")
            << "command \"%{hello}\""
            << "command" << (HostOsInfo::isWindowsHost() ? "\"hello world\"" : "'hello world'");

        QTest::newRow("quoted-with-extra")
            << "command \"%{hello}, he said\""
            << "command"
            << (HostOsInfo::isWindowsHost() ? "\"hello world, he said\"" : "'hello world, he said'");

        QTest::newRow("convert-to-quote-win")
            << "command 'this is a test'"
            << "command"
            << (HostOsInfo::isWindowsHost() ? "\"this is a test\"" : "'this is a test'");
    }

    void testFromInputWithMacro()
    {
        QFETCH(QString, input);
        QFETCH(QString, expectedExecutable);
        QFETCH(QString, expectedArguments);

        MacroExpander expander;
        expander.registerVariable("hello", "world var", [] { return "hello world"; });

        CommandLine cmd = CommandLine::fromUserInput(input, &expander);
        QCOMPARE(cmd.executable().toUserOutput(), expectedExecutable);

        if (HostOsInfo::isWindowsHost()) {
            QEXPECT_FAIL("convert-to-quote-win",
                         "Windows should convert single to double quotes",
                         Continue);
        }

        QCOMPARE(cmd.arguments(), expectedArguments);
    }

    void testMultiCommand_data()
    {
        QTest::addColumn<QString>("input");
        QTest::addColumn<QString>("executable");
        QTest::addColumn<QString>("arguments");

        QTest::newRow("command-and-command") << "command1 && command2"
                                             << "command1"
                                             << "&& command2";

        QTest::newRow("command-and-command-nospace") << "command1&&command2"
                                                     << "command1"
                                                     << "&&command2";

        QTest::newRow("command-semicolon-command") << "command1 ; command2"
                                                   << "command1"
                                                   << "; command2";

        QTest::newRow("command-or-command") << "command1 || command2"
                                            << "command1"
                                            << "|| command2";
    }

    void testMultiCommand()
    {
        QFETCH(QString, input);
        QFETCH(QString, executable);
        QFETCH(QString, arguments);

        CommandLine cmdLine = CommandLine::fromUserInput(input);

        QEXPECT_FAIL(
            "command-and-command-nospace",
            "CommandLine::fromUserInput does not handle multi-command without space correctly",
            Abort);

        QCOMPARE(cmdLine.executable().path(), executable);
        QCOMPARE(cmdLine.arguments(), arguments);
    }

    void testNestedInitializer()
    {
        CommandLine cmd(self, {{"foo", "bar"}, {"baz", "blizz"}});
        QCOMPARE(cmd.arguments(), "foo bar baz blizz");
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    self = FilePath::fromString(argv[0]);

    if (qEnvironmentVariableIsSet("TEST_ECHO")) {
        for (int i = 1; i < argc; ++i) {
            std::cout << argv[i] << std::endl;
        }
        return 0;
    }

    TESTLIB_SELFCOVERAGE_START(tst_CommandLine)
    QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<tst_CommandLine>();

    tst_CommandLine tc;
    QTEST_SET_MAIN_SOURCE_PATH
    return QTest::qExec(&tc, argc, argv);
}

#include "tst_commandline.moc"