2#include <QtCore/QElapsedTimer>
3#include <QtCore/QLoggingCategory>
4#include <QtCore/QObject>
9Q_DECLARE_LOGGING_CATEGORY(gLogWakaTimeClientTest)
10Q_LOGGING_CATEGORY(gLogWakaTimeClientTest,
"wakatime-config-test")
12class WakaTimeClientTest : public QObject {
16 WakaTimeClientTest(QObject *parent =
nullptr);
17 ~WakaTimeClientTest()
override;
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();
36WakaTimeClientTest::WakaTimeClientTest(QObject *parent)
37 : QObject(parent), oldHome(getenv(
"HOME")), oldPath(getenv(
"PATH")) {
41WakaTimeClientTest::~WakaTimeClientTest() {
44void WakaTimeClientTest::testGetBinPathNotFound() {
45 qputenv(
"PATH", QByteArrayLiteral(
""));
47 QString binPath = wakatime.
getBinPath({QStringLiteral(
"zzzzzzzzzzzzz")});
48 QVERIFY(binPath.isEmpty());
49 qputenv(
"PATH", QByteArray(oldPath));
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);
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'");
67 QString binPath = wakatime.
getBinPath({QStringLiteral(
"some-executable")});
68 QVERIFY(!binPath.isEmpty());
69 QVERIFY(binPath.endsWith(QStringLiteral(
"/some-executable")));
72 QVERIFY(wakatime.binPathCache.contains(QStringLiteral(
"some-executable")));
73 wakatime.
getBinPath({QStringLiteral(
"some-executable")});
75 qputenv(
"PATH", QByteArray(oldPath));
78void WakaTimeClientTest::testGetProjectDirectoryNotFound() {
80 QFileInfo fileInfo(QStringLiteral(
"/"));
82 QVERIFY(projectDir.isEmpty());
85void WakaTimeClientTest::testGetProjectDirectoryFound() {
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")));
95 QVERIFY(!projectDir.isEmpty());
96 QVERIFY(projectDir.endsWith(QStringLiteral(
"kate-wakatime-client-test")));
99void WakaTimeClientTest::testSendWakaTimeCliNotInPath() {
100 qputenv(
"HOME", QByteArrayLiteral(
"/non/existent/path"));
101 qputenv(
"PATH", QByteArrayLiteral(
""));
103 auto state = wakatime.
send(
104 QStringLiteral(
"/path/to/some/file.cpp"), QStringLiteral(
"cpp"), 10, 5, 100,
false);
106 qputenv(
"PATH", QByteArray(oldPath));
107 qputenv(
"HOME", QByteArray(oldHome));
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);
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");
123 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
124 QFileDevice::ReadGroup | QFileDevice::ReadOther);
127 auto state = wakatime.
send(QString(), QStringLiteral(
"cpp"), 10, 5, 100,
false);
130 qputenv(
"PATH", QByteArray(oldPath));
131 qputenv(
"HOME", QByteArray(oldHome));
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);
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");
147 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
148 QFileDevice::ReadGroup | QFileDevice::ReadOther);
151 auto state = wakatime.
send(
152 tempDir.filePath(QStringLiteral(
"some-file.cpp")), QStringLiteral(
"cpp"), 10, 5, 100,
true);
155 qputenv(
"PATH", QByteArray(oldPath));
156 qputenv(
"HOME", QByteArray(oldHome));
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);
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");
172 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
173 QFileDevice::ReadGroup | QFileDevice::ReadOther);
177 QVERIFY(!wakatime.
getBinPath({QStringLiteral(
"wakatime")}).isEmpty());
179 auto state = wakatime.
send(
180 tempDir.filePath(QStringLiteral(
"some-file.cpp")), QStringLiteral(
"cpp"), 10, 5, 100,
true);
182 QVERIFY(wakatime.binPathCache.isEmpty());
184 qputenv(
"PATH", QByteArray(oldPath));
185 qputenv(
"HOME", QByteArray(oldHome));
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);
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");
201 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
202 QFileDevice::ReadGroup | QFileDevice::ReadOther);
205 auto state = wakatime.
send(tempDir.filePath(QStringLiteral(
"some-file.cpp")),
206 QStringLiteral(
"cpp"),
214 state = wakatime.
send(tempDir.filePath(QStringLiteral(
"some-file.cpp")),
215 QStringLiteral(
"cpp"),
222 qputenv(
"PATH", QByteArray(oldPath));
223 qputenv(
"HOME", QByteArray(oldHome));
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);
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");
239 someExec.setPermissions(QFileDevice::ExeUser | QFileDevice::ReadUser | QFileDevice::WriteUser |
240 QFileDevice::ReadGroup | QFileDevice::ReadOther);
245 auto state = wakatime.
send(
246 tempDir.filePath(QStringLiteral(
"some-file.cpp")), QStringLiteral(
"cpp"), 10, 5, 100,
true);
248 QVERIFY(timer.elapsed() < 5000);
250 qputenv(
"PATH", QByteArray(oldPath));
251 qputenv(
"HOME", QByteArray(oldHome));
254QTEST_MAIN(WakaTimeClientTest)
256#include "clienttest.moc"
QString getProjectDirectory(const QFileInfo &fileInfo)
QString getBinPath(const QStringList &binNames)
WakaTime::State send(const QString &filePath, const QString &mode, int lineNumber, int cursorPosition, int linesInFile, bool isWrite)