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 <QStandardPaths> | ||||
23 | #include <QStringLiteral> | 24 | #include <QStringLiteral> | ||
24 | #include <QTemporaryDir> | 25 | #include <QTemporaryDir> | ||
25 | #include <QTest> | 26 | #include <QTest> | ||
26 | #include <algorithm> | 27 | #include <algorithm> | ||
27 | 28 | | |||
28 | QTEST_MAIN(KListOpenFilesJobTest) | 29 | QTEST_MAIN(KListOpenFilesJobTest) | ||
29 | 30 | | |||
31 | namespace { | ||||
32 | | ||||
33 | 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. | |||||
34 | { | ||||
35 | return !QStandardPaths::findExecutable(QStringLiteral("lsof")).isEmpty(); | ||||
36 | } | ||||
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 | |||||
37 | | ||||
38 | } | ||||
39 | | ||||
30 | void KListOpenFilesJobTest::testOpenFiles() | 40 | void KListOpenFilesJobTest::testOpenFiles() | ||
31 | { | 41 | { | ||
42 | if (!hasLsofInstalled()) { | ||||
43 | QSKIP("lsof is not installed - skipping test"); | ||||
44 | } | ||||
32 | QDir path(QCoreApplication::applicationDirPath()); | 45 | QDir path(QCoreApplication::applicationDirPath()); | ||
33 | auto job = new KListOpenFilesJob(path.path()); | 46 | auto job = new KListOpenFilesJob(path.path()); | ||
34 | job->exec(); | 47 | job->exec(); | ||
35 | QCOMPARE(job->error(), KJob::NoError); | 48 | QCOMPARE(job->error(), KJob::NoError); | ||
36 | auto processInfoList = job->processInfoList(); | 49 | auto processInfoList = job->processInfoList(); | ||
37 | QVERIFY(!processInfoList.empty()); | 50 | QVERIFY(!processInfoList.empty()); | ||
38 | auto testProcessIterator = std::find_if(processInfoList.begin(), processInfoList.end(), | 51 | auto testProcessIterator = std::find_if(processInfoList.begin(), processInfoList.end(), | ||
39 | [](const KProcessList::KProcessInfo& info) | 52 | [](const KProcessList::KProcessInfo& info) | ||
40 | { | 53 | { | ||
41 | return info.pid() == QCoreApplication::applicationPid(); | 54 | return info.pid() == QCoreApplication::applicationPid(); | ||
42 | }); | 55 | }); | ||
43 | QVERIFY(testProcessIterator != processInfoList.end()); | 56 | QVERIFY(testProcessIterator != processInfoList.end()); | ||
44 | const auto& processInfo = *testProcessIterator; | 57 | const auto& processInfo = *testProcessIterator; | ||
45 | QVERIFY(processInfo.isValid()); | 58 | QVERIFY(processInfo.isValid()); | ||
46 | QCOMPARE(processInfo.pid(), QCoreApplication::applicationPid()); | 59 | QCOMPARE(processInfo.pid(), QCoreApplication::applicationPid()); | ||
47 | } | 60 | } | ||
48 | 61 | | |||
49 | void KListOpenFilesJobTest::testNoOpenFiles() | 62 | void KListOpenFilesJobTest::testNoOpenFiles() | ||
50 | { | 63 | { | ||
64 | if (!hasLsofInstalled()) { | ||||
65 | QSKIP("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 :) | |||||
66 | } | ||||
51 | QTemporaryDir tempDir; | 67 | QTemporaryDir tempDir; | ||
52 | auto job = new KListOpenFilesJob(tempDir.path()); | 68 | auto job = new KListOpenFilesJob(tempDir.path()); | ||
53 | job->exec(); | 69 | job->exec(); | ||
54 | QCOMPARE(job->error(), KJob::NoError); | 70 | QCOMPARE(job->error(), KJob::NoError); | ||
55 | QVERIFY(job->processInfoList().empty()); | 71 | QVERIFY(job->processInfoList().empty()); | ||
56 | } | 72 | } | ||
57 | 73 | | |||
58 | void KListOpenFilesJobTest::testNonExistingDir() | 74 | void KListOpenFilesJobTest::testNonExistingDir() | ||
59 | { | 75 | { | ||
76 | if (!hasLsofInstalled()) { | ||||
77 | QSKIP("lsof is not installed - skipping test"); | ||||
78 | } | ||||
60 | QString nonExistingDir(QStringLiteral("/does/not/exist")); | 79 | QString nonExistingDir(QStringLiteral("/does/not/exist")); | ||
61 | auto job = new KListOpenFilesJob(nonExistingDir); | 80 | auto job = new KListOpenFilesJob(nonExistingDir); | ||
62 | job->exec(); | 81 | job->exec(); | ||
63 | QCOMPARE(job->error(), static_cast<int>(KListOpenFilesJob::Error::DoesNotExist)); | 82 | QCOMPARE(job->error(), static_cast<int>(KListOpenFilesJob::Error::DoesNotExist)); | ||
64 | QCOMPARE(job->errorText(), QStringLiteral("Path %1 doesn't exist").arg(nonExistingDir)); | 83 | QCOMPARE(job->errorText(), QStringLiteral("Path %1 doesn't exist").arg(nonExistingDir)); | ||
65 | QVERIFY(job->processInfoList().empty()); | 84 | QVERIFY(job->processInfoList().empty()); | ||
66 | } | 85 | } | ||
67 | 86 | | |||
Show All 31 Lines |
Can't you simply use QStandardPaths::findExecutable()?
https://doc.qt.io/qt-5/qstandardpaths.html#findExecutable