diff --git a/runners/shell/autotests/shellrunnertest.cpp b/runners/shell/autotests/shellrunnertest.cpp index 51e470bd6..b7ccfb598 100644 --- a/runners/shell/autotests/shellrunnertest.cpp +++ b/runners/shell/autotests/shellrunnertest.cpp @@ -20,6 +20,8 @@ Q_OBJECT private: RunnerManager *manager = nullptr; + QFileInfo createExecutableFile(const QString &fileName); + private Q_SLOTS: void initTestCase(); void testShellrunnerQueries_data(); @@ -87,20 +89,37 @@ void ShellRunnerTest::testShellrunnerQueries_data() << 0 << "LC_ALL=C /bin/trueeeeeeeeeeee" << "" << QStringList{}; // Some file we can access with a ~ - const QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - QDir(tmpPath).mkpath("."); - QFile testFile(tmpPath + "/test.sh"); - testFile.open(QIODevice::WriteOnly); - testFile.setPermissions(QFile::ExeOwner); - const QString absoluteFilePath = testFile.fileName(); - const QString tildePath = KShell::tildeCollapse(absoluteFilePath); + const QFileInfo testFile = createExecutableFile("test.sh"); + const QString tildePath = KShell::tildeCollapse(testFile.absoluteFilePath()); QTest::newRow("Should show result for full path with tilde") - << 1 << tildePath << tildePath << QStringList{}; + << 1 << tildePath << KShell::quoteArg(tildePath) << QStringList{}; QTest::newRow("Should show result for full path with tilde and envs") << 1 << "LC_ALL=C " + tildePath << KShell::quoteArg(tildePath) << 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(tildePath) + " --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 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(tildeSpacePath) << QStringList{}; + QTest::newRow("Should show result for full path with tilde, quoted space and args") + << 1 << KShell::quoteArg(tildeSpacePath) + " --help" + << KShell::joinArgs({tildeSpacePath, "--help"}) << QStringList{}; +} + +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) diff --git a/runners/shell/shellrunner.cpp b/runners/shell/shellrunner.cpp index bb88c8925..9a2c8dbb1 100644 --- a/runners/shell/shellrunner.cpp +++ b/runners/shell/shellrunner.cpp @@ -53,16 +53,9 @@ ShellRunner::~ShellRunner() void ShellRunner::match(Plasma::RunnerContext &context) { - bool isShellCommand = context.type() == Plasma::RunnerContext::ShellCommand || context.type() == Plasma::RunnerContext::Executable; QStringList envs; QString command = context.query(); - // If it is not a shell command we check if we use ENV variables, FEATURE: 409107 - // This is not recognized when setting the context type and we can't change it, because - // other runners depend on the current pattern - if (!isShellCommand) { - isShellCommand = parseENVVariables(context.query(), envs, command); - } - if (isShellCommand) { + if (parseShellCommand(context.query(), envs, command)) { const QString term = context.query(); Plasma::QueryMatch match(this); match.setId(term); @@ -89,7 +82,7 @@ void ShellRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryM job->start(); } -bool ShellRunner::parseENVVariables(const QString &query, QStringList &envs, QString &command) +bool ShellRunner::parseShellCommand(const QString &query, QStringList &envs, QString &command) { const static QRegularExpression envRegex = QRegularExpression(QStringLiteral("^.+=.+$")); const QStringList split = KShell::splitArgs(query); diff --git a/runners/shell/shellrunner.h b/runners/shell/shellrunner.h index 3846f584b..e19191165 100644 --- a/runners/shell/shellrunner.h +++ b/runners/shell/shellrunner.h @@ -37,7 +37,7 @@ class ShellRunner : public Plasma::AbstractRunner void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &action) override; private: - bool parseENVVariables(const QString &query, QStringList &envs, QString &command); + bool parseShellCommand(const QString &query, QStringList &envs, QString &command); QList m_actionList; QIcon m_matchIcon; };