File: shellrunnertest.cpp

package info (click to toggle)
plasma-workspace 4%3A5.27.5-2%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 102,040 kB
  • sloc: cpp: 121,800; xml: 3,238; python: 645; perl: 586; sh: 254; javascript: 113; ruby: 62; makefile: 15; ansic: 13
file content (126 lines) | stat: -rw-r--r-- 5,332 bytes parent folder | download
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
/*
    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
    SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lonau@gmx.de>
*/

// See https://phabricator.kde.org/T14499, this plugin's id should be renamed
#undef KRUNNER_TEST_RUNNER_PLUGIN_NAME
#define KRUNNER_TEST_RUNNER_PLUGIN_NAME "shell"

#include <QStandardPaths>
#include <QTemporaryFile>
#include <QTest>

#include <KPluginMetaData>
#include <KRunner/AbstractRunnerTest>
#include <KShell>
#include <QSignalSpy>
#include <QStandardPaths>

#include <clocale>

class ShellRunnerTest : public AbstractRunnerTest
{
    Q_OBJECT

private:
    QFileInfo createExecutableFile(const QString &fileName);

private Q_SLOTS:
    void initTestCase();
    void testShellrunnerQueries_data();
    void testShellrunnerQueries();
};

void ShellRunnerTest::initTestCase()
{
    initProperties();
}

void ShellRunnerTest::testShellrunnerQueries()
{
    QFETCH(int, matchCount);
    QFETCH(QString, query);
    QFETCH(QString, expectedCommand);
    QFETCH(QStringList, expectedENVs);

    launchQuery(query);
    QCOMPARE(manager->matches().count(), matchCount);
    if (matchCount == 1) {
        const QVariantList matchData = manager->matches().constFirst().data().toList();
        QCOMPARE(matchData.first().toString(), expectedCommand);
        QCOMPARE(matchData.at(1).toStringList(), expectedENVs);
    }
}

void ShellRunnerTest::testShellrunnerQueries_data()
{
    QTest::addColumn<int>("matchCount");
    QTest::addColumn<QString>("query");
    QTest::addColumn<QString>("expectedCommand");
    QTest::addColumn<QStringList>("expectedENVs");

    // On The BSDs the path can differ, this will give us the absolute path
    const QString executablePath = QStandardPaths::findExecutable("true");
    QVERIFY(!executablePath.isEmpty());
    // clang-format off
    QTest::newRow("Should show result with full executable path")
        << 1 << executablePath << executablePath << QStringList{};
    QTest::newRow("Should show result with full executable path and args")
        << 1 << executablePath + " --help" << executablePath + " --help" << QStringList{};
    QTest::newRow("Should bot show result for non-existent path")
        << 0 << "/bin/trueeeeeee" << QString() << QStringList{};
    QTest::newRow("Should show result for executable name")
        << 1 << "true" << executablePath << QStringList{};
    QTest::newRow("Should show result for executable name and args")
        << 1 << "true --help" << executablePath + " --help" << QStringList{};

    QTest::newRow("Should show result for executable and ENV variables")
        << 1 << "LC_ALL=C true" << executablePath << QStringList{"LC_ALL=C"};
    QTest::newRow("Should show result for executable + args and ENV variables")
        << 1 << "LC_ALL=C true --help" << executablePath + " --help" << QStringList{"LC_ALL=C"};
    QTest::newRow("Should show result for executable and multiple ENV variables")
        << 1 << "LC_ALL=C TEST=1 true" << executablePath << QStringList{"LC_ALL=C", "TEST=1"};
    QTest::newRow("Should show no result for non-existent executable path and ENV variable")
        << 0 << "LC_ALL=C /bin/trueeeeeeeeeeee" << "" << QStringList{};

    // Some file we can access with a ~
    const QFileInfo testFile = createExecutableFile("test.sh");
    const QString testFilePath = testFile.absoluteFilePath();
    const QString tildePath = KShell::tildeCollapse(testFilePath);

    QTest::newRow("Should show result for full path with tilde")
        << 1 << tildePath << KShell::quoteArg(testFilePath) << QStringList{};
    QTest::newRow("Should show result for full path with tilde and envs")
        << 1 << "LC_ALL=C " + tildePath << KShell::quoteArg(testFilePath) << QStringList{"LC_ALL=C"};
    QTest::newRow("Should show result for full path with tilde + args and envs")
        << 1 << "LC_ALL=C " + tildePath + " --help" << KShell::quoteArg(testFilePath) + " --help" << QStringList{"LC_ALL=C"};

    // Some file we can access with a ~ and which has a space in its filename
    const QFileInfo testSpaceFile = createExecutableFile("test space.sh");
    const QString testSpaceFilePath = testSpaceFile.absoluteFilePath();
    const QString tildeSpacePath = KShell::tildeCollapse(testSpaceFile.absoluteFilePath());

    QTest::newRow("Should show no result for full path with tilde and unquoted space")
            << 0 << tildeSpacePath << QString() << QStringList{};
    QTest::newRow("Should show result for full path with tilde and quoted space")
            << 1 << KShell::quoteArg(tildeSpacePath) << KShell::quoteArg(testSpaceFilePath) << QStringList{};
    QTest::newRow("Should show result for full path with tilde, quoted space and args")
            << 1 << KShell::quoteArg(tildeSpacePath) + " --help"
            << KShell::joinArgs({testSpaceFilePath, "--help"}) << QStringList{};
    // clang-format on
}

QFileInfo ShellRunnerTest::createExecutableFile(const QString &fileName)
{
    const QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    QDir(tmpPath).mkpath(".");
    QFile testFile(tmpPath + "/" + fileName);
    testFile.open(QIODevice::WriteOnly);
    testFile.setPermissions(QFile::ExeOwner);
    return QFileInfo(testFile);
}

QTEST_MAIN(ShellRunnerTest)

#include "shellrunnertest.moc"