track-cli 4.0.3 → 4.1.0-rc2

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.
Files changed (93) hide show
  1. package/esm/_dnt.shims.d.ts +1 -1
  2. package/esm/_dnt.test_shims.d.ts +20 -0
  3. package/esm/_dnt.test_shims.js +77 -0
  4. package/esm/deps/deno.land/std@0.195.0/_util/diff.d.ts +26 -0
  5. package/esm/deps/deno.land/std@0.195.0/_util/diff.js +311 -0
  6. package/esm/deps/deno.land/std@0.195.0/assert/_constants.d.ts +1 -0
  7. package/esm/deps/deno.land/std@0.195.0/assert/_constants.js +2 -0
  8. package/esm/deps/deno.land/std@0.195.0/assert/_format.d.ts +1 -0
  9. package/esm/deps/deno.land/std@0.195.0/assert/_format.js +23 -0
  10. package/esm/deps/deno.land/std@0.195.0/assert/assert_almost_equals.d.ts +18 -0
  11. package/esm/deps/deno.land/std@0.195.0/assert/assert_almost_equals.js +32 -0
  12. package/esm/deps/deno.land/std@0.195.0/assert/assert_array_includes.d.ts +14 -0
  13. package/esm/deps/deno.land/std@0.195.0/assert/assert_array_includes.js +38 -0
  14. package/esm/deps/deno.land/std@0.195.0/assert/assert_equals.d.ts +17 -0
  15. package/esm/deps/deno.land/std@0.195.0/assert/assert_equals.js +45 -0
  16. package/esm/deps/deno.land/std@0.195.0/assert/assert_exists.d.ts +5 -0
  17. package/esm/deps/deno.land/std@0.195.0/assert/assert_exists.js +14 -0
  18. package/esm/deps/deno.land/std@0.195.0/assert/assert_false.d.ts +4 -0
  19. package/esm/deps/deno.land/std@0.195.0/assert/assert_false.js +7 -0
  20. package/esm/deps/deno.land/std@0.195.0/assert/assert_instance_of.d.ts +8 -0
  21. package/esm/deps/deno.land/std@0.195.0/assert/assert_instance_of.js +38 -0
  22. package/esm/deps/deno.land/std@0.195.0/assert/assert_is_error.d.ts +7 -0
  23. package/esm/deps/deno.land/std@0.195.0/assert/assert_is_error.js +26 -0
  24. package/esm/deps/deno.land/std@0.195.0/assert/assert_match.d.ts +5 -0
  25. package/esm/deps/deno.land/std@0.195.0/assert/assert_match.js +13 -0
  26. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_equals.d.ts +14 -0
  27. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_equals.js +37 -0
  28. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_instance_of.d.ts +5 -0
  29. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_instance_of.js +14 -0
  30. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_match.d.ts +5 -0
  31. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_match.js +14 -0
  32. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_strict_equals.d.ts +11 -0
  33. package/esm/deps/deno.land/std@0.195.0/assert/assert_not_strict_equals.js +20 -0
  34. package/esm/deps/deno.land/std@0.195.0/assert/assert_object_match.d.ts +5 -0
  35. package/esm/deps/deno.land/std@0.195.0/assert/assert_object_match.js +78 -0
  36. package/esm/deps/deno.land/std@0.195.0/assert/assert_rejects.d.ts +64 -0
  37. package/esm/deps/deno.land/std@0.195.0/assert/assert_rejects.js +50 -0
  38. package/esm/deps/deno.land/std@0.195.0/assert/assert_strict_equals.d.ts +23 -0
  39. package/esm/deps/deno.land/std@0.195.0/assert/assert_strict_equals.js +60 -0
  40. package/esm/deps/deno.land/std@0.195.0/assert/assert_string_includes.d.ts +5 -0
  41. package/esm/deps/deno.land/std@0.195.0/assert/assert_string_includes.js +13 -0
  42. package/esm/deps/deno.land/std@0.195.0/assert/assert_throws.d.ts +54 -0
  43. package/esm/deps/deno.land/std@0.195.0/assert/assert_throws.js +44 -0
  44. package/esm/deps/deno.land/std@0.195.0/assert/equal.d.ts +6 -0
  45. package/esm/deps/deno.land/std@0.195.0/assert/equal.js +102 -0
  46. package/esm/deps/deno.land/std@0.195.0/assert/fail.d.ts +4 -0
  47. package/esm/deps/deno.land/std@0.195.0/assert/fail.js +9 -0
  48. package/esm/deps/deno.land/std@0.195.0/assert/mod.d.ts +32 -0
  49. package/esm/deps/deno.land/std@0.195.0/assert/mod.js +33 -0
  50. package/esm/deps/deno.land/std@0.195.0/assert/unimplemented.d.ts +2 -0
  51. package/esm/deps/deno.land/std@0.195.0/assert/unimplemented.js +7 -0
  52. package/esm/deps/deno.land/std@0.195.0/assert/unreachable.d.ts +2 -0
  53. package/esm/deps/deno.land/std@0.195.0/assert/unreachable.js +6 -0
  54. package/esm/deps/deno.land/std@0.195.0/testing/_test_suite.d.ts +70 -0
  55. package/esm/deps/deno.land/std@0.195.0/testing/_test_suite.js +321 -0
  56. package/esm/deps/deno.land/std@0.195.0/testing/asserts.d.ts +329 -0
  57. package/esm/deps/deno.land/std@0.195.0/testing/asserts.js +330 -0
  58. package/esm/deps/deno.land/std@0.195.0/testing/bdd.d.ts +440 -0
  59. package/esm/deps/deno.land/std@0.195.0/testing/bdd.js +215 -0
  60. package/esm/deps/deno.land/std@0.195.0/testing/mock.d.ts +110 -0
  61. package/esm/deps/deno.land/std@0.195.0/testing/mock.js +746 -0
  62. package/esm/src/action/clone.js +4 -3
  63. package/esm/src/action/frontend-template-switch.d.ts +1 -0
  64. package/esm/src/action/frontend-template-switch.js +53 -0
  65. package/esm/src/action/frontend-template.d.ts +2 -0
  66. package/esm/src/action/frontend-template.js +12 -0
  67. package/esm/src/action/lang.js +11 -2
  68. package/esm/src/action/run.js +6 -7
  69. package/esm/src/main.js +9 -2
  70. package/esm/src/meta.d.ts +1 -1
  71. package/esm/src/meta.js +108 -25
  72. package/esm/src/orca/client.js +1 -1
  73. package/esm/src/shared/config.d.ts +1 -0
  74. package/esm/src/shared/errors.d.ts +7 -1
  75. package/esm/src/shared/errors.js +16 -3
  76. package/esm/src/shared/file.js +1 -1
  77. package/esm/src/shared/mod.d.ts +1 -1
  78. package/esm/src/shared/mod.js +10 -2
  79. package/esm/src/shared/types.d.ts +16 -0
  80. package/esm/src/shared/types.js +2 -0
  81. package/esm/src/track/client.d.ts +4 -1
  82. package/esm/src/track/test.d.ts +6 -2
  83. package/esm/src/track/test.js +28 -5
  84. package/esm/src/track/training.d.ts +4 -1
  85. package/esm/src/track/training.js +9 -0
  86. package/esm/test/shared/config_test.d.ts +1 -0
  87. package/esm/test/shared/config_test.js +57 -0
  88. package/esm/test/shared/file_test.d.ts +1 -0
  89. package/esm/test/shared/file_test.js +265 -0
  90. package/esm/test/shared/mod_test.d.ts +1 -0
  91. package/esm/test/shared/mod_test.js +353 -0
  92. package/package.json +1 -1
  93. package/test_runner.js +186 -0
@@ -0,0 +1,353 @@
1
+ import * as dntShim from "../../_dnt.test_shims.js";
2
+ import * as mod from "../../src/shared/mod.js";
3
+ import { FileListType } from "../../src/shared/types.js";
4
+ import { assertEquals, assertStringIncludes } from "../../deps/deno.land/std@0.195.0/testing/asserts.js";
5
+ import { describe, it } from "../../deps/deno.land/std@0.195.0/testing/bdd.js";
6
+ import { assertSpyCall, restore, returnsNext, stub } from "../../deps/deno.land/std@0.195.0/testing/mock.js";
7
+ import * as colors from "../../deps/deno.land/std@0.195.0/fmt/colors.js";
8
+ describe("HttpStatus", () => {
9
+ // deno-fmt-ignore
10
+ const testcases = [
11
+ { code: 200, isSuccess: true, isClientError: false, isServerError: false },
12
+ { code: 201, isSuccess: true, isClientError: false, isServerError: false },
13
+ { code: 400, isSuccess: false, isClientError: true, isServerError: false },
14
+ { code: 403, isSuccess: false, isClientError: true, isServerError: false },
15
+ { code: 500, isSuccess: false, isClientError: false, isServerError: true },
16
+ { code: 503, isSuccess: false, isClientError: false, isServerError: true },
17
+ ];
18
+ describe("HttpStatus.isSuccess(x: number)", () => {
19
+ testcases.forEach(({ code, isSuccess }) => {
20
+ it(`should return ${isSuccess} for ${code}`, () => {
21
+ assertEquals(mod.HttpStatus.isSuccess(code), isSuccess);
22
+ });
23
+ });
24
+ });
25
+ describe("HttpStatus.isClientError(x: number)", () => {
26
+ testcases.forEach(({ code, isClientError }) => {
27
+ it(`should return ${isClientError} for ${code}`, () => {
28
+ assertEquals(mod.HttpStatus.isClientError(code), isClientError);
29
+ });
30
+ });
31
+ });
32
+ describe("HttpStatus.isServerError(x: number)", () => {
33
+ testcases.forEach(({ code, isServerError }) => {
34
+ it(`should return ${isServerError} for ${code}`, () => {
35
+ assertEquals(mod.HttpStatus.isServerError(code), isServerError);
36
+ });
37
+ });
38
+ });
39
+ it("HttpStatus for Response should work", () => {
40
+ testcases.forEach((testcase) => {
41
+ const response = new dntShim.Response("hello", { status: testcase.code });
42
+ assertEquals(mod.HttpStatus.isSuccess(response), testcase.isSuccess);
43
+ assertEquals(mod.HttpStatus.isClientError(response), testcase.isClientError);
44
+ assertEquals(mod.HttpStatus.isServerError(response), testcase.isServerError);
45
+ });
46
+ });
47
+ });
48
+ describe("Prompt.select()", () => {
49
+ describe(".select()", () => {
50
+ const items = ["a", "b", "c"];
51
+ // deno-fmt-ignore-start
52
+ const testcases = [
53
+ { input: "1", expected: 0, message: "should return selected item index (0 start) - 1" },
54
+ { input: "2", expected: 1, message: "should return selected item index (0 start) - 2" },
55
+ { input: "3", expected: 2, message: "should return selected item index (0 start) - 3" },
56
+ { input: "4", expected: undefined, message: "should return undefined if input is out of range" },
57
+ { input: null, expected: undefined, message: "should return undefined for empty input" },
58
+ { input: "x", expected: undefined, message: "should return undefined for invalid input" },
59
+ ];
60
+ testcases.forEach(({ input, expected, message }) => {
61
+ it(message, () => {
62
+ const promptSpy = stub(mod._internals, "prompt", returnsNext([input]));
63
+ const actual = mod.Prompt.select("Select one", items);
64
+ assertEquals(actual, expected);
65
+ assertSpyCall(promptSpy, 0, {
66
+ args: ["Select one\n[1] a\n[2] b\n[3] c\n> "],
67
+ });
68
+ restore();
69
+ });
70
+ });
71
+ });
72
+ });
73
+ describe("printTimeLeft()", () => {
74
+ const noColor = (x) => x;
75
+ // deno-fmt-ignore
76
+ const testcases = [
77
+ { input: 0, expected: "0s", color: colors.red },
78
+ { input: 1, expected: "1s", color: colors.red },
79
+ { input: 59, expected: "59s", color: colors.red },
80
+ { input: 60, expected: "1m", color: colors.red },
81
+ { input: 300, expected: "5m", color: colors.red },
82
+ { input: 301, expected: "5m 1s", color: colors.yellow },
83
+ { input: 3599, expected: "59m 59s", color: colors.yellow },
84
+ { input: 3600, expected: "1h", color: colors.yellow },
85
+ { input: -1, expected: "The exam deadline has not yet been started", color: noColor },
86
+ { input: -2, expected: "The exam deadline has passed", color: noColor },
87
+ ];
88
+ testcases.forEach(({ input, expected, color }) => {
89
+ it(`should print collect time for ${input} second(s)`, () => {
90
+ const consoleSpy = stub(console, "log");
91
+ mod.printTimeLeft(input);
92
+ const actual = consoleSpy.calls.flatMap((call) => call.args).join("\n");
93
+ assertStringIncludes(actual, color(expected));
94
+ restore();
95
+ });
96
+ });
97
+ });
98
+ describe("listFileNames()", () => {
99
+ const context = {
100
+ settings: {
101
+ files: {
102
+ "test": {
103
+ "settings.json": false,
104
+ "basic.js": false,
105
+ "basic_testcases.json": false,
106
+ "in": {
107
+ "basic": {
108
+ "1.in": false,
109
+ "2.in": false,
110
+ },
111
+ },
112
+ "out": {
113
+ "basic": {
114
+ "1.out": false,
115
+ "2.out": false,
116
+ },
117
+ },
118
+ },
119
+ "readme.md": true,
120
+ "Cargo.toml": true,
121
+ "src": {
122
+ "main.rs": true,
123
+ },
124
+ },
125
+ hiddenFiles: [
126
+ "test/in/secret/1.in",
127
+ "test/in/secret/2.in",
128
+ "test/out/secret/1.out",
129
+ "test/out/secret/2.out",
130
+ "test/secret_testcases.json",
131
+ "test/secret.js",
132
+ ],
133
+ testExcludes: [],
134
+ images: [],
135
+ build: ["cargo build --release"],
136
+ test: "cargo test",
137
+ mainFile: "src/main.rs",
138
+ allowNewFile: true,
139
+ languages: ["rust"],
140
+ initialize: [],
141
+ },
142
+ answers: {
143
+ build: ["cargo build --release"],
144
+ appCommand: "./target/release/main",
145
+ mainFile: "src/main.rs",
146
+ addedFiles: ["src/lib.rs"],
147
+ fileVersions: {},
148
+ },
149
+ };
150
+ const testcases = [
151
+ {
152
+ target: "public files",
153
+ type: FileListType.All,
154
+ expected: [
155
+ "Cargo.toml",
156
+ "readme.md",
157
+ "src/lib.rs",
158
+ "src/main.rs",
159
+ "test/basic.js",
160
+ "test/basic_testcases.json",
161
+ "test/in/basic/1.in",
162
+ "test/in/basic/2.in",
163
+ "test/out/basic/1.out",
164
+ "test/out/basic/2.out",
165
+ "test/settings.json",
166
+ ],
167
+ },
168
+ {
169
+ target: "readonly public files",
170
+ type: FileListType.ReadOnlyChallengeFiles,
171
+ expected: [
172
+ "test/basic.js",
173
+ "test/basic_testcases.json",
174
+ "test/in/basic/1.in",
175
+ "test/in/basic/2.in",
176
+ "test/out/basic/1.out",
177
+ "test/out/basic/2.out",
178
+ "test/settings.json",
179
+ ],
180
+ },
181
+ {
182
+ target: "editable files",
183
+ type: FileListType.EditableChallengeFiles,
184
+ expected: [
185
+ "Cargo.toml",
186
+ "readme.md",
187
+ "src/main.rs",
188
+ ],
189
+ },
190
+ {
191
+ target: "public files",
192
+ type: FileListType.UserAddedFiles,
193
+ expected: [
194
+ "src/lib.rs",
195
+ ],
196
+ },
197
+ {
198
+ target: "public files",
199
+ type: FileListType.Editable,
200
+ expected: [
201
+ "Cargo.toml",
202
+ "readme.md",
203
+ "src/lib.rs",
204
+ "src/main.rs",
205
+ ],
206
+ },
207
+ ];
208
+ testcases.forEach(({ target, type, expected }) => {
209
+ const flt = (Object.entries(FileListType).find(([_, v]) => v === type) ?? ["", 0])[0];
210
+ it(`should list all ${target} with FileListType.${flt}`, () => {
211
+ const actual = mod.listFileNames(context, type);
212
+ actual.sort();
213
+ assertEquals(actual, expected);
214
+ });
215
+ });
216
+ });
217
+ describe("SnakeCase", () => {
218
+ describe(".to()", () => {
219
+ it("should convert string correctly", () => {
220
+ assertEquals(mod.SnakeCase.to("helloWorld"), "hello_world");
221
+ });
222
+ it("should keep string as is if it is already snake case", () => {
223
+ assertEquals(mod.SnakeCase.to("hello_world"), "hello_world");
224
+ });
225
+ it("should accept empty object", () => {
226
+ assertEquals(mod.SnakeCase.to({}), {});
227
+ });
228
+ it("should convert object correctly", () => {
229
+ assertEquals(mod.SnakeCase.to({
230
+ helloWorld: "foo",
231
+ }), {
232
+ hello_world: "foo",
233
+ });
234
+ });
235
+ it("should keep object as is if it is already snake case", () => {
236
+ // @ts-ignore key of the object may change
237
+ assertEquals(mod.SnakeCase.to({
238
+ hello_world: "foo",
239
+ }), {
240
+ hello_world: "foo",
241
+ });
242
+ });
243
+ it("should keep object values as is", () => {
244
+ // @ts-ignore key of the object may change
245
+ assertEquals(mod.SnakeCase.to({
246
+ helloWorld: "helloWorld",
247
+ another_world: "anotherWorld",
248
+ }), {
249
+ hello_world: "helloWorld",
250
+ another_world: "anotherWorld",
251
+ });
252
+ });
253
+ it("should keep array as is", () => {
254
+ assertEquals(mod.SnakeCase.to(["helloWorld", "another_world"]), ["helloWorld", "another_world"]);
255
+ assertEquals(mod.SnakeCase.to({
256
+ helloWorld: ["helloWorld", "another_world"],
257
+ }), {
258
+ hello_world: ["helloWorld", "another_world"],
259
+ });
260
+ });
261
+ it("should treat nested object correctly", () => {
262
+ assertEquals(mod.SnakeCase.to({
263
+ helloWorld: {
264
+ anotherWorld: "fooBar",
265
+ },
266
+ }), {
267
+ hello_world: {
268
+ another_world: "fooBar",
269
+ },
270
+ });
271
+ });
272
+ it("should ignore primitive types other than strings", () => {
273
+ assertEquals(mod.SnakeCase.to(1), 1);
274
+ assertEquals(mod.SnakeCase.to(true), true);
275
+ assertEquals(mod.SnakeCase.to(undefined), undefined);
276
+ assertEquals(mod.SnakeCase.to(null), null);
277
+ });
278
+ });
279
+ describe(".from()", () => {
280
+ it("should convert string correctly", () => {
281
+ assertEquals(mod.SnakeCase.from("hello_world"), "helloWorld");
282
+ });
283
+ it("should keep string as is if it is already camelCase", () => {
284
+ assertEquals(mod.SnakeCase.from("helloWorld"), "helloWorld");
285
+ });
286
+ it("should accept empty object", () => {
287
+ assertEquals(mod.SnakeCase.to({}), {});
288
+ });
289
+ it("should convert object correctly", () => {
290
+ assertEquals(mod.SnakeCase.from({
291
+ hello_world: "foo",
292
+ }), {
293
+ helloWorld: "foo",
294
+ });
295
+ });
296
+ it("should keep object as is if it is already camelCase", () => {
297
+ // @ts-ignore key of the object may change
298
+ assertEquals(mod.SnakeCase.from({
299
+ helloWorld: "foo",
300
+ }), {
301
+ helloWorld: "foo",
302
+ });
303
+ });
304
+ it("should keep object values as is", () => {
305
+ // @ts-ignore key of the object may change
306
+ assertEquals(mod.SnakeCase.from({
307
+ helloWorld: "hello_world",
308
+ another_world: "another_world",
309
+ }), {
310
+ helloWorld: "hello_world",
311
+ anotherWorld: "another_world",
312
+ });
313
+ });
314
+ it("should keep array as is", () => {
315
+ assertEquals(mod.SnakeCase.from(["helloWorld", "another_world"]), ["helloWorld", "another_world"]);
316
+ assertEquals(mod.SnakeCase.from({
317
+ hello_world: ["helloWorld", "another_world"],
318
+ }), {
319
+ helloWorld: ["helloWorld", "another_world"],
320
+ });
321
+ });
322
+ it("should treat nested object correctly", () => {
323
+ assertEquals(mod.SnakeCase.from({
324
+ hello_world: {
325
+ another_world: "foo_bar",
326
+ },
327
+ }), {
328
+ helloWorld: {
329
+ anotherWorld: "foo_bar",
330
+ },
331
+ });
332
+ });
333
+ it("should ignore primitive types other than strings", () => {
334
+ assertEquals(mod.SnakeCase.from(1), 1);
335
+ assertEquals(mod.SnakeCase.from(true), true);
336
+ assertEquals(mod.SnakeCase.from(undefined), undefined);
337
+ assertEquals(mod.SnakeCase.from(null), null);
338
+ });
339
+ });
340
+ });
341
+ describe("scoreRatio() (Inner method)", () => {
342
+ it("should return `N/A` if `track run` is never executed", () => {
343
+ assertStringIncludes(mod._internals.scoreRatio(undefined, 10), "N/A");
344
+ });
345
+ it("should return `N/A` for challenges without open testcase", () => {
346
+ assertStringIncludes(mod._internals.scoreRatio(undefined, undefined), "N/A");
347
+ });
348
+ it("should return score if `track run` is once after executed", () => {
349
+ assertStringIncludes(mod._internals.scoreRatio(0, 10), "0/10");
350
+ assertStringIncludes(mod._internals.scoreRatio(5, 10), "5/10");
351
+ assertStringIncludes(mod._internals.scoreRatio(10, 10), "10/10");
352
+ });
353
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "track-cli",
3
- "version": "4.0.3",
3
+ "version": "4.1.0-rc2",
4
4
  "description": "A CLI for interacting with tracks.run and running code tests on track's servers",
5
5
  "repository": {
6
6
  "type": "git",
package/test_runner.js ADDED
@@ -0,0 +1,186 @@
1
+ const pc = require("picocolors");
2
+ const process = require("process");
3
+ const { pathToFileURL } = require("url");
4
+ const { testDefinitions } = require("@deno/shim-deno/test-internals");
5
+ const filePaths = [
6
+ "src/track/test.js",
7
+ "test/shared/config_test.js",
8
+ "test/shared/file_test.js",
9
+ "test/shared/mod_test.js",
10
+ ];
11
+ async function main() {
12
+ const testContext = {
13
+ process,
14
+ pc,
15
+ };
16
+ for (const [i, filePath] of filePaths.entries()) {
17
+ if (i > 0) {
18
+ console.log("");
19
+ }
20
+ const esmPath = "./esm/" + filePath;
21
+ console.log("\nRunning tests in " + pc.underline(esmPath) + "...\n");
22
+ process.chdir(__dirname + "/esm");
23
+ const esmTestContext = {
24
+ origin: pathToFileURL(filePath).toString(),
25
+ ...testContext,
26
+ };
27
+ await import(esmPath);
28
+ await runTestDefinitions(testDefinitions.splice(0, testDefinitions.length), esmTestContext);
29
+ }
30
+ }
31
+ async function runTestDefinitions(testDefinitions, options) {
32
+ const testFailures = [];
33
+ for (const definition of testDefinitions) {
34
+ options.process.stdout.write("test " + definition.name + " ...");
35
+ if (definition.ignore) {
36
+ options.process.stdout.write(` ${options.pc.gray("ignored")}\n`);
37
+ continue;
38
+ }
39
+ const context = getTestContext(definition, undefined);
40
+ let pass = false;
41
+ try {
42
+ await definition.fn(context);
43
+ if (context.hasFailingChild) {
44
+ testFailures.push({
45
+ name: definition.name,
46
+ err: new Error("Had failing test step.")
47
+ });
48
+ }
49
+ else {
50
+ pass = true;
51
+ }
52
+ }
53
+ catch (err) {
54
+ testFailures.push({
55
+ name: definition.name,
56
+ err
57
+ });
58
+ }
59
+ const testStepOutput = context.getOutput();
60
+ if (testStepOutput.length > 0) {
61
+ options.process.stdout.write(testStepOutput);
62
+ }
63
+ else {
64
+ options.process.stdout.write(" ");
65
+ }
66
+ options.process.stdout.write(getStatusText(pass ? "ok" : "fail"));
67
+ options.process.stdout.write("\n");
68
+ }
69
+ if (testFailures.length > 0) {
70
+ options.process.stdout.write("\nFAILURES");
71
+ for (const failure of testFailures) {
72
+ options.process.stdout.write("\n\n");
73
+ options.process.stdout.write(failure.name + "\n");
74
+ options.process.stdout.write(indentText((failure.err?.stack ?? failure.err).toString(), 1));
75
+ }
76
+ options.process.exit(1);
77
+ }
78
+ function getTestContext(definition, parent) {
79
+ return {
80
+ name: definition.name,
81
+ parent,
82
+ origin: options.origin,
83
+ /** @type {any} */ err: undefined,
84
+ status: "ok",
85
+ children: [],
86
+ get hasFailingChild() {
87
+ return this.children.some((c) => c.status === "fail" || c.status === "pending");
88
+ },
89
+ getOutput() {
90
+ let output = "";
91
+ if (this.parent) {
92
+ output += "test " + this.name + " ...";
93
+ }
94
+ if (this.children.length > 0) {
95
+ output += "\n" + this.children.map((c) => indentText(c.getOutput(), 1)).join("\n") + "\n";
96
+ }
97
+ else if (!this.err) {
98
+ output += " ";
99
+ }
100
+ if (this.parent && this.err) {
101
+ output += "\n";
102
+ }
103
+ if (this.err) {
104
+ output += indentText((this.err.stack ?? this.err).toString(), 1);
105
+ if (this.parent) {
106
+ output += "\n";
107
+ }
108
+ }
109
+ if (this.parent) {
110
+ output += getStatusText(this.status);
111
+ }
112
+ return output;
113
+ },
114
+ async step(nameOrTestDefinition, fn) {
115
+ const definition = getDefinition();
116
+ const context = getTestContext(definition, this);
117
+ context.status = "pending";
118
+ this.children.push(context);
119
+ if (definition.ignore) {
120
+ context.status = "ignored";
121
+ return false;
122
+ }
123
+ try {
124
+ await definition.fn(context);
125
+ context.status = "ok";
126
+ if (context.hasFailingChild) {
127
+ context.status = "fail";
128
+ return false;
129
+ }
130
+ return true;
131
+ }
132
+ catch (err) {
133
+ context.status = "fail";
134
+ context.err = err;
135
+ return false;
136
+ }
137
+ /** @returns {TestDefinition} */ function getDefinition() {
138
+ if (typeof nameOrTestDefinition === "string") {
139
+ if (!(fn instanceof Function)) {
140
+ throw new TypeError("Expected function for second argument.");
141
+ }
142
+ return {
143
+ name: nameOrTestDefinition,
144
+ fn
145
+ };
146
+ }
147
+ else if (typeof nameOrTestDefinition === "object") {
148
+ return nameOrTestDefinition;
149
+ }
150
+ else {
151
+ throw new TypeError("Expected a test definition or name and function.");
152
+ }
153
+ }
154
+ }
155
+ };
156
+ }
157
+ function getStatusText(status) {
158
+ switch (status) {
159
+ case "ok":
160
+ return options.pc.green(status);
161
+ case "fail":
162
+ case "pending":
163
+ return options.pc.red(status);
164
+ case "ignored":
165
+ return options.pc.gray(status);
166
+ default:
167
+ {
168
+ const _assertNever = status;
169
+ return status;
170
+ }
171
+ }
172
+ }
173
+ function indentText(text, indentLevel) {
174
+ if (text === undefined) {
175
+ text = "[undefined]";
176
+ }
177
+ else if (text === null) {
178
+ text = "[null]";
179
+ }
180
+ else {
181
+ text = text.toString();
182
+ }
183
+ return text.split(/\r?\n/).map((line) => " ".repeat(indentLevel) + line).join("\n");
184
+ }
185
+ }
186
+ main();