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