@iamsergio/qttest-utils 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/out/qttest.js CHANGED
@@ -1,257 +1,291 @@
1
- "use strict";
2
- // SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3
- // Author: Sergio Martins <sergio.martins@kdab.com>
4
- // SPDX-License-Identifier: MIT
5
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
6
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
7
- return new (P || (P = Promise))(function (resolve, reject) {
8
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
9
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
10
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
11
- step((generator = generator.apply(thisArg, _arguments || [])).next());
12
- });
13
- };
14
- var __importDefault = (this && this.__importDefault) || function (mod) {
15
- return (mod && mod.__esModule) ? mod : { "default": mod };
16
- };
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.QtTests = exports.QtTestSlot = exports.QtTest = void 0;
19
- const child_process_1 = require("child_process");
20
- const path_1 = __importDefault(require("path"));
21
- const cmake_1 = require("./cmake");
22
- /**
23
- * Represents a single QtTest executable.
24
- * Supports listing the individual test slots
25
- */
26
- class QtTest {
27
- constructor(filename) {
28
- this.slots = null;
29
- this.filename = filename;
30
- }
31
- get id() {
32
- return this.filename;
33
- }
34
- get label() {
35
- return path_1.default.basename(this.filename);
36
- }
37
- /**
38
- * Calls "./yourqttest -functions" and stores the results in the slots property.
39
- */
40
- parseAvailableSlots() {
41
- return __awaiter(this, void 0, void 0, function* () {
42
- let slotNames = [];
43
- let output = "";
44
- yield new Promise((resolve, reject) => {
45
- const child = (0, child_process_1.spawn)(this.filename, ["-functions"]);
46
- child.stdout.on("data", (chunk) => {
47
- output += chunk.toString();
48
- });
49
- child.on("exit", (code) => {
50
- if (code === 0) {
51
- slotNames = slotNames.concat(output.split("\n"));
52
- slotNames = slotNames.map(entry => entry.trim().replace("()", ""));
53
- slotNames = slotNames.filter(entry => entry.length > 0);
54
- if (slotNames.length > 0) {
55
- this.slots = [];
56
- for (var slotName of slotNames) {
57
- var slot = new QtTestSlot(slotName, this);
58
- this.slots.push(slot);
59
- }
60
- }
61
- resolve(slotNames);
62
- }
63
- else {
64
- reject(new Error("Failed to run -functions"));
65
- }
66
- });
67
- });
68
- });
69
- }
70
- /**
71
- * Returns whether this executable links to libQtTest.so.
72
- *
73
- * Useful for Qt autodetection, as some tests are doctest or so.
74
- *
75
- * Only implemented for Linux. Returns undefined on other platforms.
76
- */
77
- linksToQtTestLib() {
78
- let isLinux = process.platform === "linux";
79
- if (!isLinux) {
80
- return undefined;
81
- }
82
- return new Promise((resolve, reject) => {
83
- const child = (0, child_process_1.spawn)("ldd", [this.filename]);
84
- let output = "";
85
- let result = false;
86
- child.stdout.on("data", (chunk) => {
87
- if (!result) {
88
- if (chunk.toString().includes("libQt5Test.so") || chunk.toString().includes("libQt6Test.so")) {
89
- result = true;
90
- }
91
- }
92
- });
93
- child.on("exit", (code) => {
94
- if (code === 0) {
95
- resolve(result);
96
- }
97
- else {
98
- reject(new Error("Failed to run ldd"));
99
- }
100
- });
101
- });
102
- }
103
- /// Returns whether this test is a QtTest by running it with -help and checking if the help text looks familiar
104
- /// Note that if this is not a QtTest it might not run help and instead execute the test itself
105
- isQtTestViaHelp() {
106
- return __awaiter(this, void 0, void 0, function* () {
107
- return yield new Promise((resolve, reject) => {
108
- const child = (0, child_process_1.spawn)(this.filename, ["-help"]);
109
- let output = "";
110
- let result = false;
111
- child.stdout.on("data", (chunk) => {
112
- if (!result) {
113
- if (chunk.toString().includes("[testfunction[:testdata]]")) {
114
- result = true;
115
- }
116
- }
117
- });
118
- child.on("exit", (code) => {
119
- if (code === 0) {
120
- resolve(result);
121
- }
122
- else {
123
- resolve(false);
124
- }
125
- });
126
- });
127
- });
128
- }
129
- /// Runs this test
130
- runTest(slotName) {
131
- return __awaiter(this, void 0, void 0, function* () {
132
- let args = [];
133
- if (slotName) {
134
- // Runs a single Qt test instead
135
- args = args.concat(slotName);
136
- }
137
- return yield new Promise((resolve, reject) => {
138
- const child = (0, child_process_1.spawn)(this.filename, args);
139
- child.stdout.on("data", (chunk) => {
140
- // chunk.toString()
141
- });
142
- child.on("exit", (code) => {
143
- if (code === 0) {
144
- resolve(true);
145
- }
146
- else {
147
- resolve(false);
148
- }
149
- });
150
- });
151
- });
152
- }
153
- command() {
154
- return { label: this.label, executablePath: this.filename, args: [] };
155
- }
156
- }
157
- exports.QtTest = QtTest;
158
- /**
159
- * Represents a single Qt test slot
160
- */
161
- class QtTestSlot {
162
- constructor(name, parent) {
163
- this.name = name;
164
- this.parentQTest = parent;
165
- }
166
- get id() {
167
- return this.parentQTest.filename + this.name;
168
- }
169
- get absoluteFilePath() {
170
- return this.parentQTest.filename;
171
- }
172
- runTest() {
173
- return __awaiter(this, void 0, void 0, function* () {
174
- return this.parentQTest.runTest(this.name);
175
- });
176
- }
177
- command() {
178
- return { label: this.name, executablePath: this.absoluteFilePath, args: [this.name] };
179
- }
180
- }
181
- exports.QtTestSlot = QtTestSlot;
182
- /**
183
- * Represents the list of all QtTest executables in your project
184
- */
185
- class QtTests {
186
- constructor() {
187
- this.qtTestExecutables = [];
188
- }
189
- discoverViaCMake(buildDirPath) {
190
- return __awaiter(this, void 0, void 0, function* () {
191
- var cmake = new cmake_1.CMakeTests(buildDirPath);
192
- let ctests = yield cmake.tests();
193
- if (ctests) {
194
- for (let ctest of ctests) {
195
- let qtest = new QtTest(ctest.executablePath());
196
- this.qtTestExecutables.push(qtest);
197
- }
198
- }
199
- else {
200
- console.error("Failed to retrieve ctests!");
201
- }
202
- });
203
- }
204
- /// Removes any executable (from the list) that doesn't link to libQtTest.so
205
- /// This heuristic tries to filter-out doctest and other non-Qt tests
206
- /// Only implemented for linux for now
207
- removeNonLinking() {
208
- return __awaiter(this, void 0, void 0, function* () {
209
- let isLinux = process.platform === "linux";
210
- if (!isLinux) {
211
- return;
212
- }
213
- let acceptedExecutables = [];
214
- for (let ex of this.qtTestExecutables) {
215
- let linksToQt = yield ex.linksToQtTestLib();
216
- // undefined or true is accepted
217
- if (linksToQt !== false) {
218
- acceptedExecutables.push(ex);
219
- }
220
- this.qtTestExecutables = acceptedExecutables;
221
- }
222
- });
223
- }
224
- removeByRunningHelp() {
225
- return __awaiter(this, void 0, void 0, function* () {
226
- this.qtTestExecutables = this.qtTestExecutables.filter((ex) => __awaiter(this, void 0, void 0, function* () { return yield ex.isQtTestViaHelp(); }));
227
- });
228
- }
229
- /// Removes any executable (from the list) that matches the specified regex
230
- removeMatching(regex) {
231
- this.qtTestExecutables = this.qtTestExecutables.filter((ex) => !regex.test(ex.filename));
232
- }
233
- /// Removes any executable (from the list) that doesn't match the specified regex
234
- maintainMatching(regex) {
235
- this.qtTestExecutables = this.qtTestExecutables.filter((ex) => regex.test(ex.filename));
236
- }
237
- dumpExecutablePaths() {
238
- for (var ex of this.qtTestExecutables) {
239
- console.log(ex.filename);
240
- }
241
- }
242
- dumpTestSlots() {
243
- return __awaiter(this, void 0, void 0, function* () {
244
- for (var ex of this.qtTestExecutables) {
245
- if (!ex.slots)
246
- yield ex.parseAvailableSlots();
247
- console.log(ex.filename);
248
- if (ex.slots) {
249
- for (let slot of ex.slots) {
250
- console.log(" - " + slot.name);
251
- }
252
- }
253
- }
254
- });
255
- }
256
- }
257
- exports.QtTests = QtTests;
1
+ "use strict";
2
+ // SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
3
+ // Author: Sergio Martins <sergio.martins@kdab.com>
4
+ // SPDX-License-Identifier: MIT
5
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
9
+ desc = { enumerable: true, get: function() { return m[k]; } };
10
+ }
11
+ Object.defineProperty(o, k2, desc);
12
+ }) : (function(o, m, k, k2) {
13
+ if (k2 === undefined) k2 = k;
14
+ o[k2] = m[k];
15
+ }));
16
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
18
+ }) : function(o, v) {
19
+ o["default"] = v;
20
+ });
21
+ var __importStar = (this && this.__importStar) || function (mod) {
22
+ if (mod && mod.__esModule) return mod;
23
+ var result = {};
24
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
+ __setModuleDefault(result, mod);
26
+ return result;
27
+ };
28
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
29
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
30
+ return new (P || (P = Promise))(function (resolve, reject) {
31
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
32
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
33
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
34
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
35
+ });
36
+ };
37
+ var __importDefault = (this && this.__importDefault) || function (mod) {
38
+ return (mod && mod.__esModule) ? mod : { "default": mod };
39
+ };
40
+ Object.defineProperty(exports, "__esModule", { value: true });
41
+ exports.QtTests = exports.QtTestSlot = exports.QtTest = void 0;
42
+ const child_process_1 = require("child_process");
43
+ const path_1 = __importDefault(require("path"));
44
+ const fs = __importStar(require("fs"));
45
+ const cmake_1 = require("./cmake");
46
+ /**
47
+ * Represents a single QtTest executable.
48
+ * Supports listing the individual test slots
49
+ */
50
+ class QtTest {
51
+ constructor(filename, buildDirPath) {
52
+ this.slots = null;
53
+ this.filename = filename;
54
+ this.buildDirPath = buildDirPath;
55
+ }
56
+ get id() {
57
+ return this.filename;
58
+ }
59
+ get label() {
60
+ return path_1.default.basename(this.filename);
61
+ }
62
+ /**
63
+ * Calls "./yourqttest -functions" and stores the results in the slots property.
64
+ */
65
+ parseAvailableSlots() {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ let slotNames = [];
68
+ let output = "";
69
+ let err = "";
70
+ yield new Promise((resolve, reject) => {
71
+ if (!fs.existsSync(this.filename)) {
72
+ reject(new Error("File doesn't exit: " + this.filename));
73
+ return;
74
+ }
75
+ const child = (0, child_process_1.spawn)(this.filename, ["-functions"], { cwd: this.buildDirPath });
76
+ child.stdout.on("data", (chunk) => {
77
+ output += chunk.toString();
78
+ });
79
+ child.stderr.on("data", (chunk) => {
80
+ err += chunk.toString();
81
+ });
82
+ child.on("exit", (code) => {
83
+ if (code === 0) {
84
+ slotNames = slotNames.concat(output.split("\n"));
85
+ slotNames = slotNames.map(entry => entry.trim().replace("()", ""));
86
+ slotNames = slotNames.filter(entry => entry.length > 0);
87
+ if (slotNames.length > 0) {
88
+ this.slots = [];
89
+ for (var slotName of slotNames) {
90
+ var slot = new QtTestSlot(slotName, this);
91
+ this.slots.push(slot);
92
+ }
93
+ }
94
+ resolve(slotNames);
95
+ }
96
+ else {
97
+ reject(new Error("Failed to run -functions, stdout=" + output + "; stderr=" + err + "; code=" + code));
98
+ }
99
+ });
100
+ });
101
+ });
102
+ }
103
+ /**
104
+ * Returns whether this executable links to libQtTest.so.
105
+ *
106
+ * Useful for Qt autodetection, as some tests are doctest or so.
107
+ *
108
+ * Only implemented for Linux. Returns undefined on other platforms.
109
+ */
110
+ linksToQtTestLib() {
111
+ let isLinux = process.platform === "linux";
112
+ if (!isLinux) {
113
+ return undefined;
114
+ }
115
+ return new Promise((resolve, reject) => {
116
+ const child = (0, child_process_1.spawn)("ldd", [this.filename]);
117
+ let output = "";
118
+ let result = false;
119
+ child.stdout.on("data", (chunk) => {
120
+ if (!result) {
121
+ if (chunk.toString().includes("libQt5Test.so") || chunk.toString().includes("libQt6Test.so")) {
122
+ result = true;
123
+ }
124
+ }
125
+ });
126
+ child.on("exit", (code) => {
127
+ if (code === 0) {
128
+ resolve(result);
129
+ }
130
+ else {
131
+ reject(new Error("Failed to run ldd"));
132
+ }
133
+ });
134
+ });
135
+ }
136
+ /// Returns whether this test is a QtTest by running it with -help and checking if the help text looks familiar
137
+ /// Note that if this is not a QtTest it might not run help and instead execute the test itself
138
+ isQtTestViaHelp() {
139
+ return __awaiter(this, void 0, void 0, function* () {
140
+ return yield new Promise((resolve, reject) => {
141
+ const child = (0, child_process_1.spawn)(this.filename, ["-help"]);
142
+ let output = "";
143
+ let result = false;
144
+ child.stdout.on("data", (chunk) => {
145
+ if (!result) {
146
+ if (chunk.toString().includes("[testfunction[:testdata]]")) {
147
+ result = true;
148
+ }
149
+ }
150
+ });
151
+ child.on("exit", (code) => {
152
+ if (code === 0) {
153
+ resolve(result);
154
+ }
155
+ else {
156
+ resolve(false);
157
+ }
158
+ });
159
+ });
160
+ });
161
+ }
162
+ /// Runs this test
163
+ runTest(slotName, cwd = "") {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ let args = [];
166
+ if (slotName) {
167
+ // Runs a single Qt test instead
168
+ args = args.concat(slotName);
169
+ }
170
+ return yield new Promise((resolve, reject) => {
171
+ let opts = cwd.length > 0 ? { cwd: cwd } : { cwd: this.buildDirPath };
172
+ const child = (0, child_process_1.spawn)(this.filename, args, opts);
173
+ child.stdout.on("data", (chunk) => {
174
+ // chunk.toString()
175
+ });
176
+ child.on("exit", (code) => {
177
+ if (code === 0) {
178
+ resolve(true);
179
+ }
180
+ else {
181
+ resolve(false);
182
+ }
183
+ });
184
+ });
185
+ });
186
+ }
187
+ command() {
188
+ return { label: this.label, executablePath: this.filename, args: [] };
189
+ }
190
+ }
191
+ exports.QtTest = QtTest;
192
+ /**
193
+ * Represents a single Qt test slot
194
+ */
195
+ class QtTestSlot {
196
+ constructor(name, parent) {
197
+ this.name = name;
198
+ this.parentQTest = parent;
199
+ }
200
+ get id() {
201
+ return this.parentQTest.filename + this.name;
202
+ }
203
+ get absoluteFilePath() {
204
+ return this.parentQTest.filename;
205
+ }
206
+ runTest() {
207
+ return __awaiter(this, void 0, void 0, function* () {
208
+ return this.parentQTest.runTest(this.name);
209
+ });
210
+ }
211
+ command() {
212
+ return { label: this.name, executablePath: this.absoluteFilePath, args: [this.name] };
213
+ }
214
+ }
215
+ exports.QtTestSlot = QtTestSlot;
216
+ /**
217
+ * Represents the list of all QtTest executables in your project
218
+ */
219
+ class QtTests {
220
+ constructor() {
221
+ this.qtTestExecutables = [];
222
+ }
223
+ discoverViaCMake(buildDirPath) {
224
+ return __awaiter(this, void 0, void 0, function* () {
225
+ var cmake = new cmake_1.CMakeTests(buildDirPath);
226
+ let ctests = yield cmake.tests();
227
+ if (ctests) {
228
+ for (let ctest of ctests) {
229
+ let qtest = new QtTest(ctest.executablePath(), buildDirPath);
230
+ this.qtTestExecutables.push(qtest);
231
+ }
232
+ }
233
+ else {
234
+ console.error("Failed to retrieve ctests!");
235
+ }
236
+ });
237
+ }
238
+ /// Removes any executable (from the list) that doesn't link to libQtTest.so
239
+ /// This heuristic tries to filter-out doctest and other non-Qt tests
240
+ /// Only implemented for linux for now
241
+ removeNonLinking() {
242
+ return __awaiter(this, void 0, void 0, function* () {
243
+ let isLinux = process.platform === "linux";
244
+ if (!isLinux) {
245
+ return;
246
+ }
247
+ let acceptedExecutables = [];
248
+ for (let ex of this.qtTestExecutables) {
249
+ let linksToQt = yield ex.linksToQtTestLib();
250
+ // undefined or true is accepted
251
+ if (linksToQt !== false) {
252
+ acceptedExecutables.push(ex);
253
+ }
254
+ this.qtTestExecutables = acceptedExecutables;
255
+ }
256
+ });
257
+ }
258
+ removeByRunningHelp() {
259
+ return __awaiter(this, void 0, void 0, function* () {
260
+ this.qtTestExecutables = this.qtTestExecutables.filter((ex) => __awaiter(this, void 0, void 0, function* () { return yield ex.isQtTestViaHelp(); }));
261
+ });
262
+ }
263
+ /// Removes any executable (from the list) that matches the specified regex
264
+ removeMatching(regex) {
265
+ this.qtTestExecutables = this.qtTestExecutables.filter((ex) => !regex.test(ex.filename));
266
+ }
267
+ /// Removes any executable (from the list) that doesn't match the specified regex
268
+ maintainMatching(regex) {
269
+ this.qtTestExecutables = this.qtTestExecutables.filter((ex) => regex.test(ex.filename));
270
+ }
271
+ dumpExecutablePaths() {
272
+ for (var ex of this.qtTestExecutables) {
273
+ console.log(ex.filename);
274
+ }
275
+ }
276
+ dumpTestSlots() {
277
+ return __awaiter(this, void 0, void 0, function* () {
278
+ for (var ex of this.qtTestExecutables) {
279
+ if (!ex.slots)
280
+ yield ex.parseAvailableSlots();
281
+ console.log(ex.filename);
282
+ if (ex.slots) {
283
+ for (let slot of ex.slots) {
284
+ console.log(" - " + slot.name);
285
+ }
286
+ }
287
+ }
288
+ });
289
+ }
290
+ }
291
+ exports.QtTests = QtTests;
package/out/utils.d.ts CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export {};