forked from intersystems-community/vscode-objectscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunitTest.ts
1132 lines (1082 loc) · 46.3 KB
/
unitTest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as vscode from "vscode";
import * as Atelier from "../api/atelier";
import { clsLangId, extensionId, filesystemSchemas, lsExtensionId } from "../extension";
import { getFileText, methodOffsetToLine, outputChannel, stripClassMemberNameQuotes, uriIsParentOf } from "../utils";
import { fileSpecFromURI } from "../utils/FileProviderUtil";
import { AtelierAPI } from "../api";
import { DocumentContentProvider } from "../providers/DocumentContentProvider";
enum TestStatus {
Failed = 0,
Passed,
Skipped,
}
interface TestAssertLocation {
offset?: number;
document: string;
label?: string;
namespace?: string;
}
/** The result of a finished test */
interface TestResult {
/** The name of the class */
class: string;
/** The status of the test */
status: TestStatus;
/** How long the test took to run, in milliseconds */
duration: number;
/** The name of the method without the "Test" prefix */
method?: string;
/**
* An array of failures. The location will only be
* defined if `method` is defined.
* Will be empty if `status` is not `0` (failed).
*/
failures: { message: string; location?: TestAssertLocation }[];
/**
* The text of the error that terminated
* execution of this test.
* Will be `undefined` if `status` is not `0` (failed).
*/
error?: string;
}
/** A cache of all test classes in a test root */
const classesForRoot: WeakMap<vscode.TestItem, Map<string, vscode.TestItem>> = new WeakMap();
/** The separator between the class URI string and method name in the method's `TestItem` id */
const methodIdSeparator = "\\\\\\";
const textDecoder = new TextDecoder();
/** Write the string represenation of `error` to `outputChannel` and show it */
function outputErrorAsString(error: any): void {
if (error && error.errorText && error.errorText !== "") {
outputChannel.appendLine(error.errorText);
} else {
outputChannel.appendLine(
typeof error == "string" ? error : error instanceof Error ? error.message : JSON.stringify(error)
);
}
outputChannel.show(true);
}
/** Find the root `TestItem` for `uri` */
function rootItemForItem(testController: vscode.TestController, uri: vscode.Uri): vscode.TestItem | undefined {
let rootItem: vscode.TestItem;
for (const [, i] of testController.items) {
if (uriIsParentOf(i.uri, uri)) {
rootItem = i;
break;
}
}
return rootItem;
}
/** Compute `TestItem`s for `Test*` methods in `parent` */
async function addTestItemsForClass(testController: vscode.TestController, parent: vscode.TestItem): Promise<void> {
// Get the symbols for the parent class
const parentSymbols = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>(
"vscode.executeDocumentSymbolProvider",
parent.uri
);
if (parentSymbols?.length == 1 && parentSymbols[0].kind == vscode.SymbolKind.Class) {
const rootItem = rootItemForItem(testController, parent.uri);
if (rootItem) {
// Add this class to our cache
// Need to do this here because we need the
// DocumentSymbols to accurately determine the class
const classes = classesForRoot.get(rootItem);
classes.set(parentSymbols[0].name, parent);
classesForRoot.set(rootItem, classes);
}
parent.range = parentSymbols[0].range;
// Add an item for each Test* method defined in this class
parentSymbols[0].children.forEach((clsMember) => {
const memberName = stripClassMemberNameQuotes(clsMember.name);
if (clsMember.detail == "Method" && memberName.startsWith("Test")) {
const displayName = memberName.slice(4);
const newItem = testController.createTestItem(
`${parent.id}${methodIdSeparator}${displayName}`,
displayName,
parent.uri
);
newItem.range = clsMember.range;
// Always show non-inherited methods at the top
newItem.sortText = `##${displayName}`;
parent.children.add(newItem);
}
});
if (filesystemSchemas.includes(parent.uri.scheme)) {
// Query the server to find inherited Test* methods
const api = new AtelierAPI(parent.uri);
const workspaceFolder = vscode.workspace.getWorkspaceFolder(parent.uri).name;
const methodsMap: Map<string, string[]> = new Map();
const inheritedMethods: { Name: string; Origin: string }[] = await api
.actionQuery(
"SELECT Name, Origin FROM %Dictionary.CompiledMethod WHERE " +
"parent->ID = ? AND Origin != parent->ID AND Name %STARTSWITH 'Test' " +
"AND ClassMethod = 0 AND ClientMethod = 0 ORDER BY Name",
[parentSymbols[0].name]
)
.then((data) => data.result.content)
.catch(() => []);
inheritedMethods.forEach((method) => {
const methodsArr = methodsMap.get(method.Origin) ?? [];
methodsArr.push(method.Name);
methodsMap.set(method.Origin, methodsArr);
});
for (const [origin, originMethods] of methodsMap) {
const uri = DocumentContentProvider.getUri(`${origin}.cls`, workspaceFolder);
const symbols = await vscode.commands.executeCommand<vscode.DocumentSymbol[]>(
"vscode.executeDocumentSymbolProvider",
uri
);
// Add an item for each Test* method defined in this class
if (symbols?.length == 1 && symbols[0].kind == vscode.SymbolKind.Class) {
originMethods.forEach((originMethod) => {
const symbol = symbols[0].children.find(
(clsMember) => clsMember.detail == "Method" && stripClassMemberNameQuotes(clsMember.name) == originMethod
);
if (symbol) {
const displayName = stripClassMemberNameQuotes(symbol.name).slice(4);
const newItem = testController.createTestItem(
`${parent.id}${methodIdSeparator}${displayName}`,
displayName,
parent.uri
);
newItem.range = symbol.range;
parent.children.add(newItem);
}
});
}
}
}
}
}
/** Get the array of `objectscript.unitTest.relativeTestRoots` for workspace folder `uri`. */
function relativeTestRootsForUri(uri: vscode.Uri): string[] {
let roots: string[] = vscode.workspace.getConfiguration("objectscript.unitTest", uri).get("relativeTestRoots");
roots = roots.map((r) => r.replaceAll("\\", "/")); // VS Code URIs always use / as a separator
if (roots.length > 1) {
// Filter out any duplicate roots, or roots that are a subdirectory of another root
roots = roots.filter((root, idx) => !roots.some((r, i) => i != idx && (root.startsWith(`${r}/`) || root == r)));
}
return roots;
}
/** Compute root `TestItem`s for `folder`. Returns `[]` if `folder` can't contain tests. */
function createRootItemsForWorkspaceFolder(
testController: vscode.TestController,
folder: vscode.WorkspaceFolder
): vscode.TestItem[] {
let newItems: vscode.TestItem[] = [];
if ([...filesystemSchemas, "file"].includes(folder.uri.scheme)) {
const api = new AtelierAPI(folder.uri);
// Must have an active server connection to a non-%SYS namespace and Atelier API version 8 or above
const errorMsg =
!api.active || api.ns == ""
? "Server connection is inactive"
: api.ns == "%SYS"
? "Connected to the %SYS namespace"
: api.config.apiVersion < 8
? "Must be connected to InterSystems IRIS version 2023.3 or above"
: folder.uri.scheme != "file" && ["", "1"].includes(new URLSearchParams(folder.uri.query).get("csp"))
? "Web application folder"
: undefined;
let itemUris: vscode.Uri[];
if (folder.uri.scheme == "file") {
const roots = relativeTestRootsForUri(folder.uri);
const baseUri = folder.uri.with({ path: `${folder.uri.path}${!folder.uri.path.endsWith("/") ? "/" : ""}` });
itemUris = roots.map((root) => baseUri.with({ path: `${baseUri.path}${root}` }));
} else {
itemUris = [folder.uri];
}
newItems = itemUris.map((uri) => {
const newItem = testController.createTestItem(uri.toString(), folder.name, uri);
if (uri.scheme == "file") {
// Add the root as the description
newItem.description = uri.path.slice(folder.uri.path.length + (!folder.uri.path.endsWith("/") ? 1 : 0));
newItem.sortText = newItem.label + newItem.description;
}
if (errorMsg != undefined) {
// Show the user why we can't run tests from this folder
newItem.canResolveChildren = false;
newItem.error = errorMsg;
} else {
newItem.canResolveChildren = true;
}
return newItem;
});
}
return newItems;
}
/** Get the `TestItem` for class `uri`. If `create` is true, create intermediate `TestItem`s. */
async function getTestItemForClass(
testController: vscode.TestController,
uri: vscode.Uri,
create = false
): Promise<vscode.TestItem | undefined> {
let item: vscode.TestItem;
const rootItem = rootItemForItem(testController, uri);
if (rootItem && !rootItem.error) {
// Walk the directory path until we reach a dead end or the TestItem for this class
let docPath = uri.path.slice(rootItem.uri.path.length);
docPath = docPath.startsWith("/") ? docPath.slice(1) : docPath;
const docPathParts = docPath.split("/");
item = rootItem;
for (const part of docPathParts) {
const currUri = item.uri.with({ path: `${item.uri.path}${!item.uri.path.endsWith("/") ? "/" : ""}${part}` });
let currItem = item.children.get(currUri.toString());
if (!currItem && create) {
// We're allowed to create non-existent directory TestItems as we walk the path
await testController.resolveHandler(item);
currItem = item.children.get(currUri.toString());
}
item = currItem;
if (!item) {
break;
}
}
}
return item;
}
/** Create a "root" item for all workspace folders that have an active server connection and MAY have tests in them. */
function replaceRootTestItems(testController: vscode.TestController): void {
testController.items.forEach((i) => classesForRoot.delete(i));
const rootItems: vscode.TestItem[] = [];
vscode.workspace.workspaceFolders?.forEach((folder) => {
const newItems = createRootItemsForWorkspaceFolder(testController, folder);
rootItems.push(...newItems);
});
rootItems.forEach((i) => classesForRoot.set(i, new Map()));
testController.items.replace(rootItems);
}
/** Create a `Promise` that resolves to a query result containing an array of children for `item`. */
function childrenForServerSideFolderItem(
item: vscode.TestItem
): Promise<Atelier.Response<Atelier.Content<{ Name: string }[]>>> {
let query: string;
let parameters: string[];
let folder = !item.uri.path.endsWith("/") ? item.uri.path + "/" : item.uri.path;
folder = folder.startsWith("/") ? folder.slice(1) : folder;
if (folder == "/") {
// Treat this the same as an empty folder
folder = "";
}
folder = folder.replace(/\//g, ".");
const folderLen = String(folder.length + 1); // Need the + 1 because SUBSTR is 1 indexed
const params = new URLSearchParams(item.uri.query);
const api = new AtelierAPI(item.uri);
if (params.has("project")) {
query =
"SELECT DISTINCT CASE " +
"WHEN $LENGTH(SUBSTR(Name,?),'.') > 1 THEN $PIECE(SUBSTR(Name,?),'.') " +
"ELSE SUBSTR(Name,?)||'.cls' END Name " +
"FROM %Studio.Project_ProjectItemsList(?) " +
"WHERE Type = 'CLS' AND Name %STARTSWITH ? AND " +
"Name IN (SELECT Name FROM %Dictionary.ClassDefinition_SubclassOf('%UnitTest.TestCase','@'))";
parameters = [folderLen, folderLen, folderLen, params.get("project"), folder];
} else {
query =
"SELECT DISTINCT CASE " +
"WHEN $LENGTH(SUBSTR(Name,?),'.') > 2 THEN $PIECE(SUBSTR(Name,?),'.') " +
"ELSE SUBSTR(Name,?) END Name " +
"FROM %Library.RoutineMgr_StudioOpenDialog(?,?,?,?,?,?,?,?,?,?) " +
"WHERE Name %STARTSWITH ? AND " +
"Name IN (SELECT Name||'.cls' FROM %Dictionary.ClassDefinition_SubclassOf('%UnitTest.TestCase','@'))";
parameters = [
folderLen,
folderLen,
folderLen,
fileSpecFromURI(item.uri),
"1",
"1",
params.has("system") && params.get("system").length ? params.get("system") : "0",
"1",
"0",
params.has("generated") && params.get("generated").length ? params.get("generated") : "0",
"",
"0",
params.has("mapped") && params.get("mapped") == "0" ? "0" : "1",
folder,
];
}
return api.actionQuery(query, parameters);
}
/** Create a child `TestItem` of `item` with label `child`. */
function addChildItem(testController: vscode.TestController, item: vscode.TestItem, child: string): void {
const newUri = item.uri.with({
path: `${item.uri.path}${!item.uri.path.endsWith("/") ? "/" : ""}${child}`,
});
if (!item.children.get(newUri.toString())) {
// Only add the item if it doesn't already exist
const newItem = testController.createTestItem(newUri.toString(), child, newUri);
newItem.canResolveChildren = true;
item.children.add(newItem);
}
}
/** Determine the class name of `item` in `root` */
function classNameForItem(item: vscode.TestItem, root: vscode.TestItem): string | undefined {
let cls: string;
const classes = classesForRoot.get(root);
if (classes) {
for (const element of classes) {
if (element[1].id == item.id) {
cls = element[0];
break;
}
}
}
return cls;
}
/** Render `line` as beautified markdown */
function markdownifyLine(line: string, bullet = false): string {
const idx = line.indexOf(":") + 1;
return `${bullet ? "- " : ""}${
idx
? `**${line.slice(0, idx)}**${line
.slice(idx)
// Need to HTML encode so rest of line is treated as raw text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)}`
: line
}`;
}
/** If `uri` is a test class without a `TestItem`, compute its `TestItem`, filling in intermediate `TestItem`s along the way */
async function addItemForClassUri(testController: vscode.TestController, uri: vscode.Uri): Promise<void> {
if (uri.path.toLowerCase().endsWith(".cls")) {
const item = await getTestItemForClass(testController, uri, true);
if (item && item.canResolveChildren && !item.children.size) {
// Resolve the methods
testController.resolveHandler(item);
}
}
}
/** The `runHandler` function for the `TestRunProfile`s. */
async function runHandler(
request: vscode.TestRunRequest,
token: vscode.CancellationToken,
testController: vscode.TestController,
debug = false
): Promise<void> {
const action = debug ? "debug" : "run";
let root: vscode.TestItem;
const asyncRequest: Atelier.AsyncUnitTestRequest = {
request: "unittest",
tests: [],
debug,
};
const clsItemsRun: vscode.TestItem[] = [];
try {
// Determine the test root for this run
let roots: vscode.TestItem[];
if (request.include?.length) {
roots = [...new Set(request.include.map((i) => rootItemForItem(testController, i.uri)))];
} else {
// Run was launched from controller's root level
// Ignore any roots that have errors
roots = [];
testController.items.forEach((i) => i.error == undefined && roots.push(i));
}
if (roots.length > 1) {
// Can't run tests from multiple roots, so ask the user to pick one
const picked = await vscode.window.showQuickPick(
roots.map((i) => {
return {
label: i.label,
detail: i.uri.toString(true),
item: i,
};
}),
{
matchOnDetail: true,
ignoreFocusOut: true,
title: `Cannot ${action} tests from multiple roots at once`,
placeHolder: `Please select a root to ${action} tests from`,
}
);
if (picked) {
root = picked.item;
}
} else if (roots.length == 1) {
root = roots[0];
}
if (!root) {
// Need a root to continue
return;
}
// Add the initial items to the queue to process
const queue: vscode.TestItem[] = [];
if (request.include?.length) {
request.include.forEach((i) => {
if (uriIsParentOf(root.uri, i.uri)) {
queue.push(i);
}
});
} else {
queue.push(root);
}
// Get the autoload configuration for the root
const autoload = vscode.workspace.getConfiguration("objectscript.unitTest.autoload", root.uri);
const autoloadFolder: string = autoload.get("folder");
const autoloadXml: boolean = autoload.get("xml");
const autoloadUdl: boolean = autoload.get("udl");
const autoloadEnabled: boolean = autoloadFolder != "" && (autoloadXml || autoloadUdl) && root.uri.scheme == "file";
const autoloadProcessed: string[] = [];
// Process every test that was queued
// Recurse down to leaves (methods) and build a map of their parents (classes)
while (queue.length > 0 && !token.isCancellationRequested) {
const test = queue.pop();
// Skip tests the user asked to exclude
if (request.exclude?.length && request.exclude.some((excludedTest) => excludedTest.id === test.id)) {
continue;
}
if (autoloadEnabled) {
// Process any autoload folders needed by this item
const basePath = root.uri.path.endsWith("/") ? root.uri.path.slice(0, -1) : root.uri.path;
const directories = ["", ...test.uri.path.slice(basePath.length + 1).split("/")];
if (directories[directories.length - 1].toLowerCase().endsWith(".cls")) {
// Remove the class name
directories.pop();
}
let testPath = "";
do {
const currentDir = directories.shift();
testPath = currentDir != "" ? `${testPath}/${currentDir}` : "";
if (!autoloadProcessed.includes(testPath)) {
// Look for XML or UDL files in the autoload folder
const files = await vscode.workspace.findFiles(
new vscode.RelativePattern(
test.uri.with({ path: `${basePath}${testPath}/${autoloadFolder}` }),
`**/*.{${autoloadXml ? "xml,XML" : ""}${autoloadXml && autoloadUdl ? "," : ""}${
autoloadUdl ? "cls,CLS,mac,MAC,int,INT,inc,INC" : ""
}}`
)
);
if (files.length) {
if (asyncRequest.load == undefined) asyncRequest.load = [];
for (const file of files) {
// Add this file to the list to load
asyncRequest.load.push({
file: file.fsPath,
content: textDecoder.decode(await vscode.workspace.fs.readFile(file)).split(/\r?\n/),
});
}
}
autoloadProcessed.push(testPath);
}
} while (directories.length);
}
// Resolve children if not already done
if (test.canResolveChildren && !test.children.size) {
await testController.resolveHandler(test);
}
if (test.uri.path.toLowerCase().endsWith(".cls")) {
if (test.id.includes(methodIdSeparator)) {
// This is a method item
// Will only reach this code if this item is in request.include
// Look up the name of this class
const cls = classNameForItem(test.parent, root);
if (cls) {
// Check if there's a test object for the parent class already
const clsObjIdx = asyncRequest.tests.findIndex((t) => t.class == cls);
if (clsObjIdx > -1) {
// Modify the existing test object if required
const clsObj = asyncRequest.tests[clsObjIdx];
if (clsObj.methods && !clsObj.methods.includes(test.label)) {
clsObj.methods.push(test.label);
asyncRequest.tests[clsObjIdx] = clsObj;
}
} else {
// Create a new test object
asyncRequest.tests.push({
class: cls,
methods: [test.label],
});
if (test.parent.uri.scheme == "file") {
// Add this class to the list to load
if (asyncRequest.load == undefined) asyncRequest.load = [];
asyncRequest.load.push({
file: test.parent.uri.fsPath,
content: textDecoder.decode(await vscode.workspace.fs.readFile(test.parent.uri)).split(/\r?\n/),
});
}
clsItemsRun.push(test.parent);
}
}
} else {
// This is a class item
// Look up the name of this class
const cls = classNameForItem(test, root);
if (cls && test.children.size) {
// It doesn't make sense to run a class with no "Test" methods
// Create the test object
const clsObj: { class: string; methods?: string[] } = { class: cls };
if (request.exclude?.length) {
// Determine the methods to run
clsObj.methods = [];
test.children.forEach((i) => {
if (!request.exclude.some((excludedTest) => excludedTest.id === i.id)) {
clsObj.methods.push(i.label);
}
});
if (clsObj.methods.length == 0) {
// No methods to run, so don't add the test object
continue;
}
if (clsObj.methods.length == test.children.size) {
// A test object with no methods means "run all methods"
delete clsObj.methods;
}
}
if (test.uri.scheme == "file") {
// Add this class to the list to load
if (asyncRequest.load == undefined) asyncRequest.load = [];
asyncRequest.load.push({
file: test.uri.fsPath,
content: textDecoder.decode(await vscode.workspace.fs.readFile(test.uri)).split(/\r?\n/),
});
}
asyncRequest.tests.push(clsObj);
clsItemsRun.push(test);
}
}
} else {
// Queue any children
test.children.forEach((i) => queue.push(i));
}
}
if (token.isCancellationRequested) {
return;
}
} catch (error) {
outputErrorAsString(error);
vscode.window.showErrorMessage(
`Error determining tests to ${action}. Check 'ObjectScript' output channel for details.`,
"Dismiss"
);
return;
}
if (!asyncRequest.tests.length) {
vscode.window.showInformationMessage(`No tests to ${action}.`, "Dismiss");
return;
}
// Ignore console output at the user's request
asyncRequest.console = vscode.workspace.getConfiguration("objectscript.unitTest", root.uri).get("showOutput");
// Send the queue request
const api = new AtelierAPI(root.uri);
const queueResp: Atelier.Response<any> = await api.queueAsync(asyncRequest, true).catch((error) => {
outputErrorAsString(error);
vscode.window.showErrorMessage(
`Error creating job to ${action} tests. Check 'ObjectScript' output channel for details.`,
"Dismiss"
);
return undefined;
});
if (!queueResp) return;
// Request was successfully queued, so get the ID
const id: string = queueResp.result.location;
if (token.isCancellationRequested) {
// The user cancelled the request, so cancel it on the server
await api.verifiedCancel(id, false);
return;
}
// Start the TestRun
const testRun = testController.createTestRun(request, undefined, true);
try {
// "Start" all of the test classes and methods that we're running
clsItemsRun.forEach((c) => {
testRun.started(c);
c.children.forEach((m) => testRun.started(m));
});
// Create a map of all TestItems that we know the status of
const knownStatuses: WeakMap<vscode.TestItem, TestStatus> = new WeakMap();
// Keep track of if/when the debug session was started
let startedDebugging = false;
// Get the map of class items for this root
const classes = classesForRoot.get(root);
// Keep track of the item that the current console output is from
let currentOutputItem: vscode.TestItem | undefined;
// The workspace folder that we're running tests in
const workspaceFolder = vscode.workspace.getWorkspaceFolder(root.uri);
// A map of all documents that we've computed symbols for
const documentSymbols: Map<string, vscode.DocumentSymbol[]> = new Map();
// A map of all documents that we've fetched the text of
const filesText: Map<string, string> = new Map();
// Poll until the tests have finished running or are cancelled by the user
const processUnitTestResults = async (): Promise<Atelier.Response<any>> => {
const pollResp = await api.pollAsync(id, true);
if (pollResp.console.length) {
// Log console output
for (const consoleLine of pollResp.console) {
const indent = consoleLine.search(/\S/);
if (indent == 4) {
if (consoleLine.endsWith("...")) {
// This is the beginning of a class
currentOutputItem = classes.get(consoleLine.trim().split(" ")[0]);
} else {
// This is the end of a class
if (currentOutputItem != undefined && currentOutputItem.id.includes(methodIdSeparator)) {
currentOutputItem = currentOutputItem.parent;
}
}
} else if (indent == 6 && consoleLine.endsWith("...")) {
// This is the beginning of a method
if (currentOutputItem != undefined) {
if (currentOutputItem.id.includes(methodIdSeparator)) {
currentOutputItem = currentOutputItem.parent.children.get(
`${currentOutputItem.parent.id}${methodIdSeparator}${consoleLine.trim().slice(4).split("(")[0]}`
);
} else {
currentOutputItem = currentOutputItem.children.get(
`${currentOutputItem.id}${methodIdSeparator}${consoleLine.trim().slice(4).split("(")[0]}`
);
}
}
} else if (indent == 2 && currentOutputItem != undefined) {
// This is the end of all test classes
currentOutputItem = undefined;
}
if (currentOutputItem != undefined) {
testRun.appendOutput(
`${consoleLine}\r\n`,
new vscode.Location(currentOutputItem.uri, currentOutputItem.range),
currentOutputItem
);
} else {
testRun.appendOutput(`${consoleLine}\r\n`);
}
}
}
if (testRun.token.isCancellationRequested) {
// The user cancelled the request, so cancel it on the server
return api.verifiedCancel(id, false);
}
if (Array.isArray(pollResp.result)) {
// Process results
for (const testResult of <TestResult[]>pollResp.result) {
const clsItem = classes.get(testResult.class);
if (clsItem) {
if (testResult.method) {
// This is a method's result
const methodItem = clsItem.children.get(`${clsItem.id}${methodIdSeparator}${testResult.method}`);
if (methodItem) {
knownStatuses.set(methodItem, testResult.status);
switch (testResult.status) {
case TestStatus.Failed: {
const messages: vscode.TestMessage[] = [];
if (testResult.error) {
// Make the error the first message
messages.push(
new vscode.TestMessage(new vscode.MarkdownString(markdownifyLine(testResult.error)))
);
}
if (testResult.failures.length) {
// Add a TestMessage for each failed assert with the correct location, if provided
for (const failure of testResult.failures) {
const message = new vscode.TestMessage(
new vscode.MarkdownString(markdownifyLine(failure.message))
);
if (failure.location) {
if (failure.location.document.toLowerCase().endsWith(".cls")) {
let locationUri: vscode.Uri;
if (classes.has(failure.location.document.slice(0, -4))) {
// This is one of the known test classes
locationUri = classes.get(failure.location.document.slice(0, -4)).uri;
} else {
// This is some other class. There's a chance that
// the class won't exist after the tests are run
// but we still want to provide the location
// because it will often be useful to the user.
locationUri = DocumentContentProvider.getUri(
failure.location.document,
workspaceFolder.name,
failure.location.namespace
);
}
if (locationUri) {
if (!documentSymbols.has(locationUri.toString())) {
const newSymbols = await vscode.commands
.executeCommand<vscode.DocumentSymbol[]>(
"vscode.executeDocumentSymbolProvider",
locationUri
)
.then(
(r) => r[0]?.children,
() => undefined
);
if (newSymbols != undefined) documentSymbols.set(locationUri.toString(), newSymbols);
}
const locationSymbols = documentSymbols.get(locationUri.toString());
if (locationSymbols != undefined) {
// Get the text of the class
if (!filesText.has(locationUri.toString())) {
const newFileText = await getFileText(locationUri).catch(() => undefined);
if (newFileText != undefined) filesText.set(locationUri.toString(), newFileText);
}
const fileText = filesText.get(locationUri.toString());
if (fileText != undefined) {
// Find the line in the text
const locationLine = methodOffsetToLine(
locationSymbols,
fileText,
failure.location.label,
failure.location.offset
);
if (locationLine != undefined) {
// We found the line, so add a location to the message
message.location = new vscode.Location(
locationUri,
// locationLine is one-indexed but Range is zero-indexed
new vscode.Range(locationLine - 1, 0, locationLine, 0)
);
}
}
}
}
} else if (failure.location.label == undefined) {
// This location doesn't contain a label, so if we can
// resolve a URI for the document then report the location.
// There's a chance that the generated URI will be for a
// document that won't exist after the tests are run
// (for example, an autoloaded document that's in an
// XML file) but we still want to provide the location
// because it will often be useful to the user.
const locationUri = DocumentContentProvider.getUri(
failure.location.document,
workspaceFolder.name,
failure.location.namespace
);
if (locationUri) {
message.location = new vscode.Location(
locationUri,
new vscode.Range(failure.location.offset ?? 0, 0, (failure.location.offset ?? 0) + 1, 0)
);
}
} else {
// This location isn't in a class and
// requires resolving a label to a line.
// We can try to resolve it but the document might
// get cleaned up when the tests finish running.
}
}
messages.push(message);
}
}
testRun.failed(methodItem, messages, testResult.duration);
break;
}
case TestStatus.Passed:
testRun.passed(methodItem, testResult.duration);
break;
default:
testRun.skipped(methodItem);
}
}
} else {
// This is a class's result
// Report any methods that don't have statuses yet as "skipped"
clsItem.children.forEach((methodItem) => {
if (!knownStatuses.has(methodItem)) {
knownStatuses.set(methodItem, TestStatus.Skipped);
testRun.skipped(methodItem);
}
});
// Report this class's status
switch (testResult.status) {
case TestStatus.Failed: {
const messages: vscode.TestMessage[] = [];
if (testResult.error) {
// Make the error the first message
messages.push(new vscode.TestMessage(new vscode.MarkdownString(markdownifyLine(testResult.error))));
}
if (testResult.failures.length) {
// Add a TestMessage showing the failures as a bulleted list
messages.push(
new vscode.TestMessage(
new vscode.MarkdownString(
`There are failed test methods:\n${testResult.failures
.map((failure) => markdownifyLine(failure.message, true))
.join("\n")}`
)
)
);
}
testRun.failed(clsItem, messages, testResult.duration);
break;
}
case TestStatus.Passed:
testRun.passed(clsItem, testResult.duration);
break;
default: {
// Only report a class as skipped if all of its methods are skipped
let allSkipped = true;
for (const [, methodItem] of clsItem.children) {
if (knownStatuses.get(methodItem) == TestStatus.Passed) {
allSkipped = false;
break;
}
}
if (allSkipped) {
testRun.skipped(clsItem);
} else {
testRun.passed(clsItem, testResult.duration);
}
}
}
}
}
}
} else if (debug && queueResp.result.content?.debugId && pollResp.result?.content?.debugReady) {
// Make sure the activeTextEditor's document is in the same workspace folder as the test
// root so the debugger connects to the correct server and runs in the correct namespace
const rootWsFolderIdx = vscode.workspace.getWorkspaceFolder(root.uri)?.index;
if (
!vscode.window.activeTextEditor?.document.uri ||
vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)?.index != rootWsFolderIdx
) {
// Make an existing editor active if one is in the correct workspace folder
let shown = false;
for (const editor of vscode.window.visibleTextEditors) {
if (vscode.workspace.getWorkspaceFolder(editor.document.uri)?.index == rootWsFolderIdx) {
await vscode.window.showTextDocument(editor.document);
shown = true;
break;
}
}
if (!shown) {
// Show the first test class. Ugly but necessary.
await vscode.window.showTextDocument(classesForRoot.get(root).get(asyncRequest.tests[0].class)?.uri);
}
}
// Start the debugging session
startedDebugging = await vscode.debug.startDebugging(undefined, {
type: "objectscript",
request: "attach",
name: "Unit tests",
cspDebugId: queueResp.result.content.debugId,
isUnitTest: true,
});
}
if (pollResp.retryafter) {
// Poll again
await new Promise((resolve) => {
// Poll less often when debugging because the tests
// will be executing much slower due to user interaction
setTimeout(resolve, startedDebugging ? 250 : 50);
});
if (testRun.token.isCancellationRequested) {
// The user cancelled the request, so cancel it on the server
return api.verifiedCancel(id, false);
}
return processUnitTestResults();
}
return pollResp;
};
await processUnitTestResults();
} catch (error) {
outputErrorAsString(error);
vscode.window.showErrorMessage(
`Error ${action}${debug ? "g" : "n"}ing tests. Check 'ObjectScript' output channel for details.`,
"Dismiss"
);
}
testRun.end();
}
/** The `configureHandler` function for the `TestRunProfile`s. */
function configureHandler(): void {
// Open the settings UI and focus on the "objectscript.unitTest" settings
vscode.commands.executeCommand(
"workbench.action.openSettings",
"@ext:intersystems-community.vscode-objectscript unitTest"
);
}
/** Set up the `TestController` and all of its dependencies. */
export function setUpTestController(): vscode.Disposable[] {
// Create and set up the test controller
const testController = vscode.tests.createTestController(extensionId, "ObjectScript");
testController.resolveHandler = async (item?: vscode.TestItem) => {
if (!item) return; // Can't resolve "undefined"
item.busy = true;
try {
if (item.uri.path.toLowerCase().endsWith(".cls")) {
// Compute items for the Test* methods in this class
await addTestItemsForClass(testController, item);
} else {
if (item.uri.scheme == "file") {
// Read the local directory for non-autoload subdirectories and classes
const autoload = vscode.workspace.getConfiguration("objectscript.unitTest.autoload", item.uri);
const autoloadFolder: string = autoload.get("folder");
const autoloadEnabled: boolean = autoloadFolder != "" && (autoload.get("xml") || autoload.get("udl"));
(await vscode.workspace.fs.readDirectory(item.uri)).forEach((element) => {
if (
(element[1] == vscode.FileType.Directory &&
!element[0].startsWith("_") && // %UnitTest.Manager skips subfolders that start with _
(!autoloadEnabled || (autoloadEnabled && element[0] != autoloadFolder))) ||
(element[1] == vscode.FileType.File && element[0].toLowerCase().endsWith(".cls"))
) {
// This element is a non-autoload directory or a .cls file
addChildItem(testController, item, element[0]);
}
});
} else {
// Query the server for subpackages and classes
(await childrenForServerSideFolderItem(item).then((data) => data.result.content)).forEach((child) =>
addChildItem(testController, item, child.Name)
);
}
}
} catch (error) {
outputErrorAsString(error);
item.error = new vscode.MarkdownString(
"Error fetching children. Check `ObjectScript` output channel for details."
);
}
item.busy = false;
};
testController.refreshHandler = () => {
replaceRootTestItems(testController);
};
// Create the run and debug profiles
const runProfile = testController.createRunProfile(
"ObjectScript Run",
vscode.TestRunProfileKind.Run,
(r, t) => runHandler(r, t, testController),
true
);
const debugProfile = testController.createRunProfile(
"ObjectScript Debug",
vscode.TestRunProfileKind.Debug,
(r, t) => runHandler(r, t, testController, true),
true
);
runProfile.configureHandler = configureHandler;
debugProfile.configureHandler = configureHandler;
// Create the initial root items
replaceRootTestItems(testController);