@iamsergio/qttest-utils 2.3.0 → 2.5.0

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
@@ -18,27 +18,29 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
18
18
  }) : function(o, v) {
19
19
  o["default"] = v;
20
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
- };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
37
38
  var __importDefault = (this && this.__importDefault) || function (mod) {
38
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
39
40
  };
40
41
  Object.defineProperty(exports, "__esModule", { value: true });
41
- exports.QtTests = exports.QtTestSlot = exports.QtTest = exports.logMessage = void 0;
42
+ exports.QtTests = exports.QtTestSlot = exports.QtTest = void 0;
43
+ exports.logMessage = logMessage;
42
44
  const child_process_1 = require("child_process");
43
45
  const path_1 = __importDefault(require("path"));
44
46
  const fs = __importStar(require("fs"));
@@ -50,21 +52,24 @@ function logMessage(message) {
50
52
  gLogFunction(message);
51
53
  }
52
54
  }
53
- exports.logMessage = logMessage;
54
55
  /**
55
56
  * Represents a single QtTest executable.
56
57
  * Supports listing the individual test slots
57
58
  */
58
59
  class QtTest {
60
+ filename;
61
+ buildDirPath;
62
+ /// If true, will print more verbose output
63
+ verbose = false;
64
+ /// Allows vscode extensions to associate with a test item
65
+ vscodeTestItem;
66
+ /// The list of individual runnable test slots
67
+ slots = null;
68
+ /// Set after running
69
+ lastExitCode = 0;
70
+ /// Allows the caller to receive the output of the test process
71
+ outputFunc = undefined;
59
72
  constructor(filename, buildDirPath) {
60
- /// If true, will print more verbose output
61
- this.verbose = false;
62
- /// The list of individual runnable test slots
63
- this.slots = null;
64
- /// Set after running
65
- this.lastExitCode = 0;
66
- /// Allows the caller to receive the output of the test process
67
- this.outputFunc = undefined;
68
73
  this.filename = filename;
69
74
  this.buildDirPath = buildDirPath;
70
75
  }
@@ -93,48 +98,46 @@ class QtTest {
93
98
  /**
94
99
  * Calls "./yourqttest -functions" and stores the results in the slots property.
95
100
  */
96
- parseAvailableSlots() {
97
- return __awaiter(this, void 0, void 0, function* () {
98
- let slotNames = [];
99
- let output = "";
100
- let err = "";
101
- yield new Promise((resolve, reject) => {
102
- if (!fs.existsSync(this.filename)) {
103
- reject(new Error("qttest: File doesn't exit: " + this.filename));
104
- return;
105
- }
106
- const child = (0, child_process_1.spawn)(this.filename, ["-functions"], {
107
- cwd: this.buildDirPath,
108
- });
109
- child.stdout.on("data", (chunk) => {
110
- output += chunk.toString();
111
- });
112
- child.stderr.on("data", (chunk) => {
113
- err += chunk.toString();
114
- });
115
- child.on("exit", (code) => {
116
- if (code === 0) {
117
- slotNames = slotNames.concat(output.split("\n"));
118
- slotNames = slotNames.map((entry) => entry.trim().replace("()", ""));
119
- slotNames = slotNames.filter((entry) => entry.length > 0);
120
- if (slotNames.length > 0) {
121
- this.slots = [];
122
- for (var slotName of slotNames) {
123
- var slot = new QtTestSlot(slotName, this);
124
- this.slots.push(slot);
125
- }
101
+ async parseAvailableSlots() {
102
+ let slotNames = [];
103
+ let output = "";
104
+ let err = "";
105
+ await new Promise((resolve, reject) => {
106
+ if (!fs.existsSync(this.filename)) {
107
+ reject(new Error("qttest: File doesn't exit: " + this.filename));
108
+ return;
109
+ }
110
+ const child = (0, child_process_1.spawn)(this.filename, ["-functions"], {
111
+ cwd: this.buildDirPath,
112
+ });
113
+ child.stdout.on("data", (chunk) => {
114
+ output += chunk.toString();
115
+ });
116
+ child.stderr.on("data", (chunk) => {
117
+ err += chunk.toString();
118
+ });
119
+ child.on("exit", (code) => {
120
+ if (code === 0) {
121
+ slotNames = slotNames.concat(output.split("\n"));
122
+ slotNames = slotNames.map((entry) => entry.trim().replace("()", ""));
123
+ slotNames = slotNames.filter((entry) => entry.length > 0);
124
+ if (slotNames.length > 0) {
125
+ this.slots = [];
126
+ for (var slotName of slotNames) {
127
+ var slot = new QtTestSlot(slotName, this);
128
+ this.slots.push(slot);
126
129
  }
127
- resolve(slotNames);
128
- }
129
- else {
130
- reject(new Error("qttest: Failed to run -functions, stdout=" +
131
- output +
132
- "; stderr=" +
133
- err +
134
- "; code=" +
135
- code));
136
130
  }
137
- });
131
+ resolve(slotNames);
132
+ }
133
+ else {
134
+ reject(new Error("qttest: Failed to run -functions, stdout=" +
135
+ output +
136
+ "; stderr=" +
137
+ err +
138
+ "; code=" +
139
+ code));
140
+ }
138
141
  });
139
142
  });
140
143
  }
@@ -178,27 +181,25 @@ class QtTest {
178
181
  }
179
182
  /// Returns whether this test is a QtTest by running it with -help and checking if the help text looks familiar
180
183
  /// Note that if this is not a QtTest it might not run help and instead execute the test itself
181
- isQtTestViaHelp() {
182
- return __awaiter(this, void 0, void 0, function* () {
183
- return yield new Promise((resolve, reject) => {
184
- const child = (0, child_process_1.spawn)(this.filename, ["-help"]);
185
- let output = "";
186
- let result = false;
187
- child.stdout.on("data", (chunk) => {
188
- if (!result) {
189
- if (chunk.toString().includes("[testfunction[:testdata]]")) {
190
- result = true;
191
- }
192
- }
193
- });
194
- child.on("exit", (code) => {
195
- if (code === 0) {
196
- resolve(result);
197
- }
198
- else {
199
- resolve(false);
184
+ async isQtTestViaHelp() {
185
+ return await new Promise((resolve, reject) => {
186
+ const child = (0, child_process_1.spawn)(this.filename, ["-help"]);
187
+ let output = "";
188
+ let result = false;
189
+ child.stdout.on("data", (chunk) => {
190
+ if (!result) {
191
+ if (chunk.toString().includes("[testfunction[:testdata]]")) {
192
+ result = true;
200
193
  }
201
- });
194
+ }
195
+ });
196
+ child.on("exit", (code) => {
197
+ if (code === 0) {
198
+ resolve(result);
199
+ }
200
+ else {
201
+ resolve(false);
202
+ }
202
203
  });
203
204
  });
204
205
  }
@@ -212,64 +213,62 @@ class QtTest {
212
213
  return undefined;
213
214
  }
214
215
  /// Runs this test
215
- runTest(slot_1) {
216
- return __awaiter(this, arguments, void 0, function* (slot, cwd = "") {
217
- let args = [];
218
- if (slot) {
219
- // Runs a single Qt test instead
220
- args = args.concat(slot.name);
221
- }
222
- else {
223
- this.clearSubTestStates();
216
+ async runTest(slot, cwd = "") {
217
+ let args = [];
218
+ if (slot) {
219
+ // Runs a single Qt test instead
220
+ args = args.concat(slot.name);
221
+ }
222
+ else {
223
+ this.clearSubTestStates();
224
+ }
225
+ // log to file and to stdout
226
+ args = args.concat("-o").concat(this.tapOutputFileName(slot) + ",tap");
227
+ args = args.concat("-o").concat(this.txtOutputFileName(slot) + ",txt");
228
+ args = args.concat("-o").concat("-,txt");
229
+ return await new Promise((resolve, reject) => {
230
+ let cwdDir = cwd.length > 0 ? cwd : this.buildDirPath;
231
+ logMessage("Running " +
232
+ this.filename +
233
+ " " +
234
+ args.join(" ") +
235
+ " with cwd=" +
236
+ cwdDir);
237
+ const child = (0, child_process_1.spawn)(this.filename, args, { cwd: cwdDir });
238
+ if (this.outputFunc) {
239
+ // Callers wants the process output:
240
+ child.stdout.on("data", (chunk) => {
241
+ if (this.outputFunc)
242
+ this.outputFunc(chunk.toString());
243
+ });
244
+ child.stderr.on("data", (chunk) => {
245
+ if (this.outputFunc)
246
+ this.outputFunc(chunk.toString());
247
+ });
224
248
  }
225
- // log to file and to stdout
226
- args = args.concat("-o").concat(this.tapOutputFileName(slot) + ",tap");
227
- args = args.concat("-o").concat(this.txtOutputFileName(slot) + ",txt");
228
- args = args.concat("-o").concat("-,txt");
229
- return yield new Promise((resolve, reject) => {
230
- let cwdDir = cwd.length > 0 ? cwd : this.buildDirPath;
231
- logMessage("Running " +
232
- this.filename +
233
- " " +
234
- args.join(" ") +
235
- " with cwd=" +
236
- cwdDir);
237
- const child = (0, child_process_1.spawn)(this.filename, args, { cwd: cwdDir });
238
- if (this.outputFunc) {
239
- // Callers wants the process output:
240
- child.stdout.on("data", (chunk) => {
241
- if (this.outputFunc)
242
- this.outputFunc(chunk.toString());
243
- });
244
- child.stderr.on("data", (chunk) => {
245
- if (this.outputFunc)
246
- this.outputFunc(chunk.toString());
247
- });
249
+ child.on("exit", async (code) => {
250
+ /// Can code even be null ?
251
+ if (code == undefined)
252
+ code = -1;
253
+ if (!slot) {
254
+ this.lastExitCode = code;
248
255
  }
249
- child.on("exit", (code) => __awaiter(this, void 0, void 0, function* () {
250
- /// Can code even be null ?
251
- if (code == undefined)
252
- code = -1;
253
- if (!slot) {
254
- this.lastExitCode = code;
255
- }
256
- if (this.slots && this.slots.length > 0) {
257
- /// When running a QtTest executable, let's check which sub-tests failed
258
- /// (So VSCode can show some error icon for each fail)
259
- try {
260
- yield this.updateSubTestStates(cwdDir, slot);
261
- }
262
- catch (e) {
263
- logMessage("Failed to update sub-test states: " + e);
264
- }
265
- }
266
- if (code === 0) {
267
- resolve(true);
256
+ if (this.slots && this.slots.length > 0) {
257
+ /// When running a QtTest executable, let's check which sub-tests failed
258
+ /// (So VSCode can show some error icon for each fail)
259
+ try {
260
+ await this.updateSubTestStates(cwdDir, slot);
268
261
  }
269
- else {
270
- resolve(false);
262
+ catch (e) {
263
+ logMessage("Failed to update sub-test states: " + e);
271
264
  }
272
- }));
265
+ }
266
+ if (code === 0) {
267
+ resolve(true);
268
+ }
269
+ else {
270
+ resolve(false);
271
+ }
273
272
  });
274
273
  });
275
274
  }
@@ -293,76 +292,74 @@ class QtTest {
293
292
  }
294
293
  }
295
294
  }
296
- updateSubTestStates(cwdDir, slot) {
297
- return __awaiter(this, void 0, void 0, function* () {
298
- let tapFileName = cwdDir + "/" + this.tapOutputFileName(slot);
299
- var failures = yield new Promise((resolve, reject) => {
300
- fs.readFile(tapFileName, "utf8", (error, data) => {
301
- if (error) {
302
- logMessage("ERROR: Failed to read log file");
303
- reject(error);
304
- }
305
- else {
306
- let failedResults = [];
307
- try {
308
- const tap_events = tap_parser_1.Parser.parse(data);
309
- for (let event of tap_events) {
310
- try {
311
- if (event.length < 2)
312
- continue;
313
- if (event.at(0) != "assert")
314
- continue;
315
- var obj = event.at(1);
316
- let pass = obj["ok"] === true;
317
- let xfail = !pass && obj["todo"] !== false;
318
- if (xfail) {
319
- // This is a QEXPECT_FAIL test, all good.
320
- // QtTest outputs it as "todo"
321
- continue;
295
+ async updateSubTestStates(cwdDir, slot) {
296
+ let tapFileName = cwdDir + "/" + this.tapOutputFileName(slot);
297
+ var failures = await new Promise((resolve, reject) => {
298
+ fs.readFile(tapFileName, "utf8", (error, data) => {
299
+ if (error) {
300
+ logMessage("ERROR: Failed to read log file");
301
+ reject(error);
302
+ }
303
+ else {
304
+ let failedResults = [];
305
+ try {
306
+ const tap_events = tap_parser_1.Parser.parse(data);
307
+ for (let event of tap_events) {
308
+ try {
309
+ if (event.length < 2)
310
+ continue;
311
+ if (event.at(0) != "assert")
312
+ continue;
313
+ var obj = event.at(1);
314
+ let pass = obj["ok"] === true;
315
+ let xfail = !pass && obj["todo"] !== false;
316
+ if (xfail) {
317
+ // This is a QEXPECT_FAIL test, all good.
318
+ // QtTest outputs it as "todo"
319
+ continue;
320
+ }
321
+ // There's an QEXPECT_FAIL but test passed, not good.
322
+ let xpass = pass && obj["todo"].includes("returned TRUE unexpectedly");
323
+ if (!pass || xpass) {
324
+ // We found a failure
325
+ var name = obj["name"].replace(/\(.*\)/, "");
326
+ var filename = "";
327
+ var lineNumber = -1;
328
+ if (obj["diag"]) {
329
+ filename = obj["diag"]["file"];
330
+ lineNumber = obj["diag"]["line"];
322
331
  }
323
- // There's an QEXPECT_FAIL but test passed, not good.
324
- let xpass = pass && obj["todo"].includes("returned TRUE unexpectedly");
325
- if (!pass || xpass) {
326
- // We found a failure
327
- var name = obj["name"].replace(/\(.*\)/, "");
328
- var filename = "";
329
- var lineNumber = -1;
330
- if (obj["diag"]) {
331
- filename = obj["diag"]["file"];
332
- lineNumber = obj["diag"]["line"];
333
- }
334
- else {
335
- // A XPASS for example misses file:line info. Nothing we can do, it's a Qt bug arguably.
336
- }
337
- failedResults.push({
338
- name: name,
339
- filePath: filename,
340
- lineNumber: lineNumber,
341
- });
332
+ else {
333
+ // A XPASS for example misses file:line info. Nothing we can do, it's a Qt bug arguably.
342
334
  }
335
+ failedResults.push({
336
+ name: name,
337
+ filePath: filename,
338
+ lineNumber: lineNumber,
339
+ });
343
340
  }
344
- catch (e) { }
345
341
  }
342
+ catch (e) { }
346
343
  }
347
- catch (e) { }
348
- resolve(failedResults);
349
344
  }
350
- });
351
- });
352
- for (let failure of failures) {
353
- if (slot && slot.name != failure.name) {
354
- // We executed a single slot, ignore anything else
355
- continue;
356
- }
357
- let failedSlot = this.slotByName(failure.name);
358
- if (failedSlot) {
359
- failedSlot.lastTestFailure = failure;
345
+ catch (e) { }
346
+ resolve(failedResults);
360
347
  }
361
- else {
362
- logMessage("ERROR: Failed to find slot with name " + failure.name);
363
- }
364
- }
348
+ });
365
349
  });
350
+ for (let failure of failures) {
351
+ if (slot && slot.name != failure.name) {
352
+ // We executed a single slot, ignore anything else
353
+ continue;
354
+ }
355
+ let failedSlot = this.slotByName(failure.name);
356
+ if (failedSlot) {
357
+ failedSlot.lastTestFailure = failure;
358
+ }
359
+ else {
360
+ logMessage("ERROR: Failed to find slot with name " + failure.name);
361
+ }
362
+ }
366
363
  }
367
364
  }
368
365
  exports.QtTest = QtTest;
@@ -370,6 +367,13 @@ exports.QtTest = QtTest;
370
367
  * Represents a single Qt test slot
371
368
  */
372
369
  class QtTestSlot {
370
+ name;
371
+ // The QTest executable this slot belongs to
372
+ parentQTest;
373
+ /// Allows vscode extensions to associate with a test item
374
+ vscodeTestItem;
375
+ /// Set after running
376
+ lastTestFailure;
373
377
  constructor(name, parent) {
374
378
  this.name = name;
375
379
  this.parentQTest = parent;
@@ -380,10 +384,8 @@ class QtTestSlot {
380
384
  get absoluteFilePath() {
381
385
  return this.parentQTest.filename;
382
386
  }
383
- runTest() {
384
- return __awaiter(this, void 0, void 0, function* () {
385
- return this.parentQTest.runTest(this);
386
- });
387
+ async runTest() {
388
+ return this.parentQTest.runTest(this);
387
389
  }
388
390
  command() {
389
391
  return {
@@ -398,51 +400,43 @@ exports.QtTestSlot = QtTestSlot;
398
400
  * Represents the list of all QtTest executables in your project
399
401
  */
400
402
  class QtTests {
401
- constructor() {
402
- this.qtTestExecutables = [];
403
- }
404
- discoverViaCMake(buildDirPath) {
405
- return __awaiter(this, void 0, void 0, function* () {
406
- var cmake = new cmake_1.CMakeTests(buildDirPath);
407
- let ctests = yield cmake.tests();
408
- if (ctests) {
409
- for (let ctest of ctests) {
410
- let qtest = new QtTest(ctest.executablePath(), buildDirPath);
411
- this.qtTestExecutables.push(qtest);
412
- }
413
- }
414
- else {
415
- logMessage("ERROR: Failed to retrieve ctests!");
403
+ qtTestExecutables = [];
404
+ async discoverViaCMake(buildDirPath) {
405
+ var cmake = new cmake_1.CMakeTests(buildDirPath);
406
+ let ctests = await cmake.tests();
407
+ if (ctests) {
408
+ for (let ctest of ctests) {
409
+ let qtest = new QtTest(ctest.executablePath(), buildDirPath);
410
+ this.qtTestExecutables.push(qtest);
416
411
  }
417
- });
412
+ }
413
+ else {
414
+ logMessage("ERROR: Failed to retrieve ctests!");
415
+ }
418
416
  }
419
417
  /// Removes any executable (from the list) that doesn't link to libQtTest.so
420
418
  /// This heuristic tries to filter-out doctest and other non-Qt tests
421
419
  /// Only implemented for linux for now
422
- removeNonLinking() {
423
- return __awaiter(this, void 0, void 0, function* () {
424
- let isLinux = process.platform === "linux";
425
- if (!isLinux) {
426
- return;
427
- }
428
- let acceptedExecutables = [];
429
- for (let ex of this.qtTestExecutables) {
430
- let linksToQt = yield ex.linksToQtTestLib();
431
- // undefined or true is accepted
432
- if (linksToQt !== false) {
433
- acceptedExecutables.push(ex);
434
- }
435
- this.qtTestExecutables = acceptedExecutables;
420
+ async removeNonLinking() {
421
+ let isLinux = process.platform === "linux";
422
+ if (!isLinux) {
423
+ return;
424
+ }
425
+ let acceptedExecutables = [];
426
+ for (let ex of this.qtTestExecutables) {
427
+ let linksToQt = await ex.linksToQtTestLib();
428
+ // undefined or true is accepted
429
+ if (linksToQt !== false) {
430
+ acceptedExecutables.push(ex);
436
431
  }
437
- });
432
+ this.qtTestExecutables = acceptedExecutables;
433
+ }
438
434
  }
439
435
  setLogFunction(func) {
440
436
  gLogFunction = func;
441
437
  }
442
- removeByRunningHelp() {
443
- return __awaiter(this, void 0, void 0, function* () {
444
- this.qtTestExecutables = this.qtTestExecutables.filter((ex) => __awaiter(this, void 0, void 0, function* () { return yield ex.isQtTestViaHelp(); }));
445
- });
438
+ async removeByRunningHelp() {
439
+ this.qtTestExecutables = this.qtTestExecutables.filter(async (ex) => await ex.isQtTestViaHelp());
446
440
  }
447
441
  /// Removes any executable (from the list) that matches the specified regex
448
442
  removeMatching(regex) {
@@ -457,19 +451,17 @@ class QtTests {
457
451
  console.log(ex.filename);
458
452
  }
459
453
  }
460
- dumpTestSlots() {
461
- return __awaiter(this, void 0, void 0, function* () {
462
- for (var ex of this.qtTestExecutables) {
463
- if (!ex.slots)
464
- yield ex.parseAvailableSlots();
465
- console.log(ex.filename);
466
- if (ex.slots) {
467
- for (let slot of ex.slots) {
468
- console.log(" - " + slot.name);
469
- }
454
+ async dumpTestSlots() {
455
+ for (var ex of this.qtTestExecutables) {
456
+ if (!ex.slots)
457
+ await ex.parseAvailableSlots();
458
+ console.log(ex.filename);
459
+ if (ex.slots) {
460
+ for (let slot of ex.slots) {
461
+ console.log(" - " + slot.name);
470
462
  }
471
463
  }
472
- });
464
+ }
473
465
  }
474
466
  /// Returns all executables that contain a Qt test slot with the specified name
475
467
  executablesContainingSlot(slotName) {