Changeset View
Changeset View
Standalone View
Standalone View
autotests/klistopenfilesjobtest_unix.cpp
Show All 14 Lines | |||||
15 | * along with this library; see the file COPYING.LIB. If not, write to | 15 | * along with this library; see the file COPYING.LIB. If not, write to | ||
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
17 | * Boston, MA 02110-1301, USA. | 17 | * Boston, MA 02110-1301, USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | #include "klistopenfilesjobtest_unix.h" | 20 | #include "klistopenfilesjobtest_unix.h" | ||
21 | #include "klistopenfilesjob.h" | 21 | #include "klistopenfilesjob.h" | ||
22 | #include <QCoreApplication> | 22 | #include <QCoreApplication> | ||
23 | #include <QDebug> | ||||
24 | #include <QProcess> | ||||
23 | #include <QStringLiteral> | 25 | #include <QStringLiteral> | ||
24 | #include <QTemporaryDir> | 26 | #include <QTemporaryDir> | ||
25 | #include <QTest> | 27 | #include <QTest> | ||
26 | #include <algorithm> | 28 | #include <algorithm> | ||
27 | 29 | | |||
28 | QTEST_MAIN(KListOpenFilesJobTest) | 30 | QTEST_MAIN(KListOpenFilesJobTest) | ||
29 | 31 | | |||
32 | namespace { | ||||
33 | | ||||
34 | bool HasLsofInstalled() | ||||
dhaumann: Can't you simply use QStandardPaths::findExecutable()?
https://doc.qt.io/qt-5/qstandardpaths. | |||||
hallas: Yes, this seems to do exactly what we need :) | |||||
dfaure: Method names start with a lowercase letter in Qt/KDE code. | |||||
35 | { | ||||
36 | QProcess lsofProcess; | ||||
37 | lsofProcess.start(QStringLiteral("lsof"), QStringList(QStringLiteral("-v"))); | ||||
A prior career in build engineering tells me that this will start failing in 10 years when lsof removes the -v argument and replaces it with --version. For robustness' sake, I would additionally check for --version (and possibly even -version too) if the initial lsof -v call fails. ngraham: A prior career in build engineering tells me that this will start failing in 10 years when… | |||||
I have changed the code to use QStandardPaths::findExecutable instead hallas: I have changed the code to use QStandardPaths::findExecutable instead | |||||
38 | lsofProcess.waitForFinished(); | ||||
39 | return lsofProcess.exitCode() == 0; | ||||
40 | } | ||||
41 | | ||||
42 | } | ||||
43 | | ||||
30 | void KListOpenFilesJobTest::testOpenFiles() | 44 | void KListOpenFilesJobTest::testOpenFiles() | ||
31 | { | 45 | { | ||
46 | if (!HasLsofInstalled()) { | ||||
47 | qDebug() << "lsof is not installed - skipping test"; | ||||
48 | return; | ||||
49 | } | ||||
32 | QDir path(QCoreApplication::applicationDirPath()); | 50 | QDir path(QCoreApplication::applicationDirPath()); | ||
33 | auto job = new KListOpenFilesJob(path.path()); | 51 | auto job = new KListOpenFilesJob(path.path()); | ||
34 | job->exec(); | 52 | job->exec(); | ||
35 | QCOMPARE(job->error(), KJob::NoError); | 53 | QCOMPARE(job->error(), KJob::NoError); | ||
36 | auto processInfoList = job->processInfoList(); | 54 | auto processInfoList = job->processInfoList(); | ||
37 | QVERIFY(!processInfoList.empty()); | 55 | QVERIFY(!processInfoList.empty()); | ||
38 | auto testProcessIterator = std::find_if(processInfoList.begin(), processInfoList.end(), | 56 | auto testProcessIterator = std::find_if(processInfoList.begin(), processInfoList.end(), | ||
39 | [](const KProcessList::KProcessInfo& info) | 57 | [](const KProcessList::KProcessInfo& info) | ||
40 | { | 58 | { | ||
41 | return info.pid() == QCoreApplication::applicationPid(); | 59 | return info.pid() == QCoreApplication::applicationPid(); | ||
42 | }); | 60 | }); | ||
43 | QVERIFY(testProcessIterator != processInfoList.end()); | 61 | QVERIFY(testProcessIterator != processInfoList.end()); | ||
44 | const auto& processInfo = *testProcessIterator; | 62 | const auto& processInfo = *testProcessIterator; | ||
45 | QVERIFY(processInfo.isValid()); | 63 | QVERIFY(processInfo.isValid()); | ||
46 | QCOMPARE(processInfo.pid(), QCoreApplication::applicationPid()); | 64 | QCOMPARE(processInfo.pid(), QCoreApplication::applicationPid()); | ||
47 | } | 65 | } | ||
48 | 66 | | |||
49 | void KListOpenFilesJobTest::testNoOpenFiles() | 67 | void KListOpenFilesJobTest::testNoOpenFiles() | ||
50 | { | 68 | { | ||
69 | if (!HasLsofInstalled()) { | ||||
70 | qDebug() << "lsof is not installed - skipping test"; | ||||
Please use QSKIP instead, otherwise the test output looks like the test passed, rather than was skipped. dfaure: Please use QSKIP instead, otherwise the test output looks like the test passed, rather than was… | |||||
hallas: Good point, I didn't know that :) | |||||
71 | return; | ||||
72 | } | ||||
51 | QTemporaryDir tempDir; | 73 | QTemporaryDir tempDir; | ||
52 | auto job = new KListOpenFilesJob(tempDir.path()); | 74 | auto job = new KListOpenFilesJob(tempDir.path()); | ||
53 | job->exec(); | 75 | job->exec(); | ||
54 | QCOMPARE(job->error(), KJob::NoError); | 76 | QCOMPARE(job->error(), KJob::NoError); | ||
55 | QVERIFY(job->processInfoList().empty()); | 77 | QVERIFY(job->processInfoList().empty()); | ||
56 | } | 78 | } | ||
57 | 79 | | |||
58 | void KListOpenFilesJobTest::testNonExistingDir() | 80 | void KListOpenFilesJobTest::testNonExistingDir() | ||
59 | { | 81 | { | ||
82 | if (!HasLsofInstalled()) { | ||||
83 | qDebug() << "lsof is not installed - skipping test"; | ||||
84 | return; | ||||
85 | } | ||||
60 | QString nonExistingDir(QStringLiteral("/does/not/exist")); | 86 | QString nonExistingDir(QStringLiteral("/does/not/exist")); | ||
61 | auto job = new KListOpenFilesJob(nonExistingDir); | 87 | auto job = new KListOpenFilesJob(nonExistingDir); | ||
62 | job->exec(); | 88 | job->exec(); | ||
63 | QCOMPARE(job->error(), static_cast<int>(KListOpenFilesJob::Error::DoesNotExist)); | 89 | QCOMPARE(job->error(), static_cast<int>(KListOpenFilesJob::Error::DoesNotExist)); | ||
64 | QCOMPARE(job->errorText(), QStringLiteral("Path %1 doesn't exist").arg(nonExistingDir)); | 90 | QCOMPARE(job->errorText(), QStringLiteral("Path %1 doesn't exist").arg(nonExistingDir)); | ||
65 | QVERIFY(job->processInfoList().empty()); | 91 | QVERIFY(job->processInfoList().empty()); | ||
66 | } | 92 | } | ||
67 | 93 | | |||
Show All 31 Lines |
Can't you simply use QStandardPaths::findExecutable()?
https://doc.qt.io/qt-5/qstandardpaths.html#findExecutable