File indexing completed on 2025-01-19 12:48:37
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2021 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #include "kterminallauncherjobtest.h" 0009 #include "kterminallauncherjob.h" 0010 0011 #include <KConfigGroup> 0012 #include <KSharedConfig> 0013 #include <QStandardPaths> 0014 #include <QTest> 0015 0016 QTEST_GUILESS_MAIN(KTerminalLauncherJobTest) 0017 0018 void KTerminalLauncherJobTest::initTestCase() 0019 { 0020 QStandardPaths::setTestModeEnabled(true); 0021 } 0022 0023 #ifndef Q_OS_WIN 0024 0025 void KTerminalLauncherJobTest::startKonsole_data() 0026 { 0027 QTest::addColumn<QString>("command"); 0028 QTest::addColumn<QString>("workdir"); 0029 QTest::addColumn<QString>("expectedCommand"); 0030 0031 QTest::newRow("no_command_no_workdir") << "" 0032 << "" 0033 << "konsole"; 0034 QTest::newRow("no_command_but_with_workdir") << "" 0035 << "/tmp" 0036 << "konsole --workdir /tmp"; 0037 QTest::newRow("with_command") << "make cheese" 0038 << "" 0039 << "konsole --noclose -e make cheese"; 0040 QTest::newRow("with_command_and_workdir") << "make cheese" 0041 << "/tmp" 0042 << "konsole --noclose --workdir /tmp -e make cheese"; 0043 } 0044 0045 void KTerminalLauncherJobTest::startKonsole() 0046 { 0047 // Given 0048 QFETCH(QString, command); 0049 QFETCH(QString, workdir); 0050 QFETCH(QString, expectedCommand); 0051 0052 KConfigGroup confGroup(KSharedConfig::openConfig(), "General"); 0053 confGroup.writeEntry("TerminalApplication", "konsole"); 0054 0055 // When 0056 auto *job = new KTerminalLauncherJob(command, this); 0057 job->setWorkingDirectory(workdir); 0058 0059 // Then 0060 job->determineFullCommand(); // internal API 0061 QCOMPARE(job->fullCommand(), expectedCommand); 0062 } 0063 0064 void KTerminalLauncherJobTest::startXterm() 0065 { 0066 // Given 0067 KConfigGroup confGroup(KSharedConfig::openConfig(), "General"); 0068 confGroup.writeEntry("TerminalApplication", "xterm"); 0069 0070 const QString command = "play golf"; 0071 0072 // When 0073 auto *job = new KTerminalLauncherJob(command, this); 0074 job->setWorkingDirectory("/tmp"); // doesn't show in the command, but actually works due to QProcess::setWorkingDirectory 0075 0076 // Then 0077 job->determineFullCommand(); // internal API 0078 QCOMPARE(job->fullCommand(), QLatin1String("xterm -hold -e play golf")); 0079 } 0080 0081 void KTerminalLauncherJobTest::startFallbackToPath() 0082 { 0083 // Given 0084 KConfigGroup confGroup(KSharedConfig::openConfig(), "General"); 0085 confGroup.writeEntry("TerminalApplication", ""); 0086 confGroup.writeEntry("TerminalService", ""); 0087 0088 const QString command = "play golf"; 0089 0090 // When 0091 // Mock binaries so we know konsole is available in PATH. Otherwise the expectations may not be true. 0092 const QString pathEnv = QFINDTESTDATA("kterminallauncherjobtest") + QLatin1Char(':') + qEnvironmentVariable("PATH"); 0093 qputenv("PATH", pathEnv.toUtf8()); 0094 auto *job = new KTerminalLauncherJob(command, this); 0095 job->setWorkingDirectory("/tmp"); // doesn't show in the command, but actually works due to QProcess::setWorkingDirectory 0096 0097 // Then 0098 job->determineFullCommand(false); // internal API 0099 // We do not particularly care about what was produced so long as there was no crash 0100 // https://bugs.kde.org/show_bug.cgi?id=446539 0101 // and it's not empty. 0102 QVERIFY(!job->fullCommand().isEmpty()); 0103 } 0104 0105 #else 0106 0107 void KTerminalLauncherJobTest::startTerminal_data() 0108 { 0109 QTest::addColumn<bool>("useWindowsTerminal"); 0110 QTest::addColumn<QString>("command"); 0111 QTest::addColumn<QString>("workdir"); 0112 QTest::addColumn<QString>("expectedCommand"); 0113 0114 QTest::newRow("no_command") << false << "" 0115 << "not_part_of_command" 0116 << "powershell.exe"; 0117 QTest::newRow("with_command") << false << "make cheese" 0118 << "not_part_of_command" 0119 << "powershell.exe -NoExit -Command make cheese"; 0120 QTest::newRow("wt_no_command_no_workdir") << true << "" 0121 << "" 0122 << "wt.exe"; 0123 QTest::newRow("wt_no_command_with_workdir") << true << "" 0124 << "C:\\" 0125 << "wt.exe --startingDirectory 'C:\\'"; 0126 QTest::newRow("wt_with_command_no_workdir") << true << "make cheese" 0127 << "" 0128 << "wt.exe powershell.exe -NoExit -Command make cheese"; 0129 QTest::newRow("wt_with_command_with_workdir") << true << "make cheese" 0130 << "C:\\" 0131 << "wt.exe --startingDirectory 'C:\\' powershell.exe -NoExit -Command make cheese"; 0132 } 0133 0134 void KTerminalLauncherJobTest::startTerminal() 0135 { 0136 // Given 0137 QFETCH(bool, useWindowsTerminal); 0138 QFETCH(QString, command); 0139 QFETCH(QString, workdir); 0140 QFETCH(QString, expectedCommand); 0141 0142 // Control the presence of wt.exe in %PATH%, by clearing it and setting our own dir 0143 const QString binDir = m_tempDir.path(); 0144 qputenv("PATH", binDir.toLocal8Bit().constData()); 0145 QString exe = binDir + QLatin1String("/wt.exe"); 0146 if (useWindowsTerminal) { 0147 QFile fakeExe(exe); 0148 QVERIFY2(fakeExe.open(QIODevice::WriteOnly), qPrintable(fakeExe.errorString())); 0149 QVERIFY(fakeExe.setPermissions(QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ReadUser | QFile::WriteUser | QFile::ExeUser)); 0150 } else { 0151 QFile::remove(exe); 0152 } 0153 0154 // When 0155 auto *job = new KTerminalLauncherJob(command, this); 0156 job->setWorkingDirectory(workdir); 0157 0158 // Then 0159 QCOMPARE(job->fullCommand(), expectedCommand); 0160 } 0161 0162 #endif 0163 0164 #include "moc_kterminallauncherjobtest.cpp"