ktexteditor_wakatime v1.5.1
Kate plugin to interface with WakaTime.
Loading...
Searching...
No Matches
clienttest.cpp
1// SPDX-License-Identifier: MIT
2#include <QtCore/QLoggingCategory>
3#include <QtCore/QObject>
4#include <QtTest/QTest>
5
6#include "wakatime.h"
7
8Q_DECLARE_LOGGING_CATEGORY(gLogWakaTimeClientTest)
9Q_LOGGING_CATEGORY(gLogWakaTimeClientTest, "wakatime-config-test")
10
11class WakaTimeClientTest : public QObject {
12 Q_OBJECT
13
14public:
15 WakaTimeClientTest(QObject *parent = nullptr);
16 ~WakaTimeClientTest() override;
17
18private Q_SLOTS:
19 void testGetBinPathNotFound();
20 void testGetBinPathFound();
21 void testGetProjectDirectoryNotFound();
22 void testGetProjectDirectoryFound();
23 void testSendWakaTimeCliNotInPath();
24 void testSendEmptyFilePath();
25 void testSendSuccessful();
26 void testSendErrorSending();
27 void testSendTooSoon();
28
29private:
30 char *oldHome;
31 char *oldPath;
32};
33
34WakaTimeClientTest::WakaTimeClientTest(QObject *parent)
35 : QObject(parent), oldHome(getenv("HOME")), oldPath(getenv("PATH")) {
36 Q_UNUSED(parent);
37}
38
39WakaTimeClientTest::~WakaTimeClientTest() {
40}
41
42void WakaTimeClientTest::testGetBinPathNotFound() {
43 qputenv("PATH", QByteArrayLiteral(""));
44 WakaTime wakatime;
45 QString binPath = wakatime.getBinPath({QStringLiteral("zzzzzzzzzzzzz")});
46 QVERIFY(binPath.isEmpty());
47 qputenv("PATH", QByteArray(oldPath));
48}
49
50void WakaTimeClientTest::testGetBinPathFound() {
51 QDir tempDir(QDir::tempPath() + QDir::separator() +
52 QStringLiteral("kate-wakatime-client-test"));
53 auto newPath = tempDir.absolutePath().toUtf8();
54 qputenv("PATH", newPath);
55
56 QDir::temp().mkdir(QStringLiteral("kate-wakatime-client-test"));
57 QFile someExec(tempDir.filePath(QStringLiteral("some-executable")));
58 someExec.open(QIODevice::WriteOnly);
59 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
60 QFileDevice::ReadGroup | QFileDevice::ReadOther);
61 someExec.write("echo 'Hello World'");
62 someExec.close();
63
64 WakaTime wakatime;
65 QString binPath = wakatime.getBinPath({QStringLiteral("some-executable")});
66 QVERIFY(!binPath.isEmpty());
67 QVERIFY(binPath.endsWith(QStringLiteral("/some-executable")));
68
69 // Cache call.
70 QVERIFY(wakatime.binPathCache.contains(QStringLiteral("some-executable")));
71 wakatime.getBinPath({QStringLiteral("some-executable")});
72
73 qputenv("PATH", QByteArray(oldPath));
74}
75
76void WakaTimeClientTest::testGetProjectDirectoryNotFound() {
77 WakaTime wakatime;
78 QFileInfo fileInfo(QStringLiteral("/"));
79 QString projectDir = wakatime.getProjectDirectory(fileInfo);
80 QVERIFY(projectDir.isEmpty());
81}
82
83void WakaTimeClientTest::testGetProjectDirectoryFound() {
84 WakaTime wakatime;
85 QFileInfo fileInfo(QDir::tempPath() + QDir::separator() +
86 QStringLiteral("kate-wakatime-client-test"));
87 QDir tempDir(fileInfo.absoluteFilePath());
88 tempDir.mkdir(QStringLiteral(".git"));
89 tempDir.mkdir(QStringLiteral("src"));
90 tempDir.mkdir(QStringLiteral("src/autotests"));
91 QFileInfo autotestsDir(tempDir.filePath(QStringLiteral("src/autotests")));
92 QString projectDir = wakatime.getProjectDirectory(autotestsDir);
93 QVERIFY(!projectDir.isEmpty());
94 QVERIFY(projectDir.endsWith(QStringLiteral("kate-wakatime-client-test")));
95}
96
97void WakaTimeClientTest::testSendWakaTimeCliNotInPath() {
98 qputenv("HOME", QByteArrayLiteral("/non/existent/path"));
99 qputenv("PATH", QByteArrayLiteral(""));
100 WakaTime wakatime;
101 auto state = wakatime.send(
102 QStringLiteral("/path/to/some/file.cpp"), QStringLiteral("cpp"), 10, 5, 100, false);
103 QVERIFY(state == WakaTime::WakaTimeCliNotInPath);
104 qputenv("PATH", QByteArray(oldPath));
105 qputenv("HOME", QByteArray(oldHome));
106}
107
108void WakaTimeClientTest::testSendEmptyFilePath() {
109 qputenv("HOME", QByteArrayLiteral("/non/existent/path"));
110 QDir tempDir(QDir::tempPath() + QDir::separator() +
111 QStringLiteral("kate-wakatime-client-test"));
112 auto newPath = tempDir.absolutePath().toUtf8();
113 qputenv("PATH", newPath);
114
115 QDir::temp().mkdir(QStringLiteral("kate-wakatime-client-test"));
116 QFile someExec(tempDir.filePath(QStringLiteral("wakatime")));
117 someExec.open(QIODevice::WriteOnly);
118 someExec.write("#!/bin/sh\n");
119 someExec.write("exit 1\n");
120 someExec.close();
121 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
122 QFileDevice::ReadGroup | QFileDevice::ReadOther);
123
124 WakaTime wakatime;
125 auto state = wakatime.send(QString(), QStringLiteral("cpp"), 10, 5, 100, false);
126 QVERIFY(state == WakaTime::NothingToSend);
127
128 qputenv("PATH", QByteArray(oldPath));
129 qputenv("HOME", QByteArray(oldHome));
130}
131
132void WakaTimeClientTest::testSendSuccessful() {
133 qputenv("HOME", QByteArrayLiteral("/non/existent/path"));
134 QDir tempDir(QDir::tempPath() + QDir::separator() +
135 QStringLiteral("kate-wakatime-client-test"));
136 auto newPath = tempDir.absolutePath().toUtf8();
137 qputenv("PATH", newPath);
138
139 QDir::temp().mkdir(QStringLiteral("kate-wakatime-client-test"));
140 QFile someExec(tempDir.filePath(QStringLiteral("wakatime")));
141 someExec.open(QIODevice::WriteOnly);
142 someExec.write("#!/bin/sh\n");
143 someExec.write("echo 'Hello World'\n");
144 someExec.close();
145 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
146 QFileDevice::ReadGroup | QFileDevice::ReadOther);
147
148 WakaTime wakatime;
149 auto state = wakatime.send(
150 tempDir.filePath(QStringLiteral("some-file.cpp")), QStringLiteral("cpp"), 10, 5, 100, true);
151 QVERIFY(state == WakaTime::SentSuccessfully);
152
153 qputenv("PATH", QByteArray(oldPath));
154 qputenv("HOME", QByteArray(oldHome));
155}
156
157void WakaTimeClientTest::testSendErrorSending() {
158 qputenv("HOME", QByteArrayLiteral("/non/existent/path"));
159 QDir tempDir(QDir::tempPath() + QDir::separator() +
160 QStringLiteral("kate-wakatime-client-test"));
161 auto newPath = tempDir.absolutePath().toUtf8();
162 qputenv("PATH", newPath);
163
164 QDir::temp().mkdir(QStringLiteral("kate-wakatime-client-test"));
165 QFile someExec(tempDir.filePath(QStringLiteral("wakatime")));
166 someExec.open(QIODevice::WriteOnly);
167 someExec.write("#!/bin/sh\n");
168 someExec.write("exit 1\n");
169 someExec.close();
170 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
171 QFileDevice::ReadGroup | QFileDevice::ReadOther);
172
173 WakaTime wakatime;
174 auto state = wakatime.send(
175 tempDir.filePath(QStringLiteral("some-file.cpp")), QStringLiteral("cpp"), 10, 5, 100, true);
176 QVERIFY(state == WakaTime::ErrorSending);
177
178 qputenv("PATH", QByteArray(oldPath));
179 qputenv("HOME", QByteArray(oldHome));
180}
181
182void WakaTimeClientTest::testSendTooSoon() {
183 qputenv("HOME", QByteArrayLiteral("/non/existent/path"));
184 QDir tempDir(QDir::tempPath() + QDir::separator() +
185 QStringLiteral("kate-wakatime-client-test"));
186 auto newPath = tempDir.absolutePath().toUtf8();
187 qputenv("PATH", newPath);
188
189 QDir::temp().mkdir(QStringLiteral("kate-wakatime-client-test"));
190 QFile someExec(tempDir.filePath(QStringLiteral("wakatime")));
191 someExec.open(QIODevice::WriteOnly);
192 someExec.write("#!/bin/sh\n");
193 someExec.write("echo 'Hello World'\n");
194 someExec.close();
195 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
196 QFileDevice::ReadGroup | QFileDevice::ReadOther);
197
198 WakaTime wakatime;
199 auto state = wakatime.send(tempDir.filePath(QStringLiteral("some-file.cpp")),
200 QStringLiteral("cpp"),
201 10,
202 5,
203 100,
204 false);
205 QVERIFY(state == WakaTime::SentSuccessfully);
206
207 // Send again immediately, should be too soon.
208 state = wakatime.send(tempDir.filePath(QStringLiteral("some-file.cpp")),
209 QStringLiteral("cpp"),
210 10,
211 5,
212 100,
213 false);
214 QVERIFY(state == WakaTime::TooSoon);
215
216 qputenv("PATH", QByteArray(oldPath));
217 qputenv("HOME", QByteArray(oldHome));
218}
219
220QTEST_MAIN(WakaTimeClientTest)
221
222#include "clienttest.moc"
QString getProjectDirectory(const QFileInfo &fileInfo)
Definition wakatime.cpp:47
@ SentSuccessfully
Definition wakatime.h:25
@ ErrorSending
Definition wakatime.h:23
@ TooSoon
Definition wakatime.h:26
@ NothingToSend
Definition wakatime.h:24
@ WakaTimeCliNotInPath
Definition wakatime.h:27
QString getBinPath(const QStringList &binNames)
Definition wakatime.cpp:16
WakaTime::State send(const QString &filePath, const QString &mode, int lineNumber, int cursorPosition, int linesInFile, bool isWrite)
Definition wakatime.cpp:72