-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-command.ts
More file actions
77 lines (67 loc) · 2.06 KB
/
test-command.ts
File metadata and controls
77 lines (67 loc) · 2.06 KB
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
import type { Route } from "./router.ts";
import { walk } from "https://deno.land/std@0.177.0/fs/walk.ts";
import { Operation, resolve, subscribe, Subscription } from "../deps.ts";
import { load, map, print } from "../mod.ts";
import type { TestResult } from "../builtin/testing.ts";
import type { PSValue } from "../types.ts";
export const TestCommand: Route = {
options: [],
help: {
HEAD: "Run a suite of PlatformScript tests",
USAGE: "pls test PATH",
},
*handle({ segments }) {
let path = segments.PATH ?? "test";
let pass = true;
let options = {
match: [new RegExp(`^${path}`)],
exts: [".test.yaml"],
};
let foundSomeTests = false;
yield* forEach(subscribe(walk(".", options)), function* (entry) {
foundSomeTests = true;
if (entry.isFile) {
console.log(`${entry.path}:`);
let location = new URL(`file://${resolve(entry.path)}`).toString();
let mod = yield* load({ location });
if (mod.value.type !== "external") {
throw new Error(
`test file should return a test object see https://pls.pub/docs/testing.html for details`,
);
} else {
let results: TestResult[] = mod.value.value;
for (let result of results) {
let message: PSValue;
if (result.type === "fail") {
pass = false;
message = map({
"❌": result.message,
});
} else {
message = map({
"✅": result.message,
});
}
console.log(print(message).value);
}
}
}
});
if (!foundSomeTests) {
throw new Error(`no tests found corresponding to ${path}`);
}
if (!pass) {
throw new Error("test failure");
}
},
};
function* forEach<T, R>(
subscription: Subscription<T, R>,
block: (value: T) => Operation<void>,
): Operation<R> {
let next = yield* subscription;
for (; !next.done; next = yield* subscription) {
yield* block(next.value);
}
return next.value;
}