aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/utils/commandline/tst_commandline.cpp
blob: 05357d2526e7967b89167669f7ddcef4915864b1 (plain)
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
// 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/launcherinterface.h>
#include <utils/processinterface.h>
#include <utils/qtcprocess.h>
#include <utils/temporarydirectory.h>

#include <QObject>
#include <QtTest>

#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)
    {
        QtcProcess p;
        p.setCommand(cmd);
        p.setEnvironment(testEnv);
        p.runBlocking();
        return QString::fromUtf8(p.readAllRawStandardOutput());
    }

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

        const QString libExecPath(qApp->applicationDirPath() + '/'
                                  + QLatin1String(TEST_RELATIVE_LIBEXEC_PATH));
        LauncherInterface::setPathToLauncher(libExecPath);

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

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

    void cleanupTestCase() { Singleton::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);
    }
};

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"