@yaks/testing 0.0.0-stage → 0.2.36

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/LICENSE +202 -0
  2. package/README.md +427 -2
  3. package/_dist/assert.d.ts +40 -0
  4. package/_dist/assert.d.ts.map +1 -0
  5. package/_dist/choose.d.ts +39 -0
  6. package/_dist/choose.d.ts.map +1 -0
  7. package/_dist/clock.d.ts +8 -0
  8. package/_dist/clock.d.ts.map +1 -0
  9. package/_dist/deps.d.ts +113 -0
  10. package/_dist/deps.d.ts.map +1 -0
  11. package/_dist/examples.d.ts +69 -0
  12. package/_dist/examples.d.ts.map +1 -0
  13. package/_dist/find.d.ts +23 -0
  14. package/_dist/find.d.ts.map +1 -0
  15. package/_dist/kept.d.ts +27 -0
  16. package/_dist/kept.d.ts.map +1 -0
  17. package/_dist/lend.d.ts +15 -0
  18. package/_dist/lend.d.ts.map +1 -0
  19. package/_dist/main.d.ts +2 -0
  20. package/_dist/main.d.ts.map +1 -0
  21. package/_dist/mod.d.ts +45 -0
  22. package/_dist/mod.d.ts.map +1 -0
  23. package/_dist/names.d.ts +31 -0
  24. package/_dist/names.d.ts.map +1 -0
  25. package/_dist/own.d.ts +25 -0
  26. package/_dist/own.d.ts.map +1 -0
  27. package/_dist/reads.d.ts +8 -0
  28. package/_dist/reads.d.ts.map +1 -0
  29. package/_dist/suite.d.ts +82 -0
  30. package/_dist/suite.d.ts.map +1 -0
  31. package/_dist/typist.d.ts +24 -0
  32. package/_dist/typist.d.ts.map +1 -0
  33. package/_dist/wait.d.ts +21 -0
  34. package/_dist/wait.d.ts.map +1 -0
  35. package/assert.js +114 -0
  36. package/assert.js.map +1 -0
  37. package/assert.ts +143 -0
  38. package/choose.js +197 -0
  39. package/choose.js.map +1 -0
  40. package/choose.ts +327 -0
  41. package/clock.js +45 -0
  42. package/clock.js.map +1 -0
  43. package/clock.ts +60 -0
  44. package/deno.json +1 -0
  45. package/deps.js +868 -0
  46. package/deps.js.map +1 -0
  47. package/deps.ts +1004 -0
  48. package/drive.js +43 -0
  49. package/drive.js.map +1 -0
  50. package/drive.ts +41 -0
  51. package/examples.js +279 -0
  52. package/examples.js.map +1 -0
  53. package/examples.ts +380 -0
  54. package/find.js +86 -0
  55. package/find.js.map +1 -0
  56. package/find.ts +102 -0
  57. package/kept.js +128 -0
  58. package/kept.js.map +1 -0
  59. package/kept.ts +146 -0
  60. package/lend.js +30 -0
  61. package/lend.js.map +1 -0
  62. package/lend.ts +31 -0
  63. package/load.js +106 -0
  64. package/load.js.map +1 -0
  65. package/load.ts +139 -0
  66. package/main.js +214 -0
  67. package/main.js.map +1 -0
  68. package/main.ts +217 -0
  69. package/mod.js +45 -0
  70. package/mod.js.map +1 -0
  71. package/mod.ts +55 -0
  72. package/names.js +209 -0
  73. package/names.js.map +1 -0
  74. package/names.ts +204 -0
  75. package/own.js +109 -0
  76. package/own.js.map +1 -0
  77. package/own.ts +136 -0
  78. package/package.json +56 -4
  79. package/reads.js +88 -0
  80. package/reads.js.map +1 -0
  81. package/reads.ts +98 -0
  82. package/run.js +306 -0
  83. package/run.js.map +1 -0
  84. package/run.ts +405 -0
  85. package/suite.js +191 -0
  86. package/suite.js.map +1 -0
  87. package/suite.ts +224 -0
  88. package/typist.js +74 -0
  89. package/typist.js.map +1 -0
  90. package/typist.ts +79 -0
  91. package/wait.js +34 -0
  92. package/wait.js.map +1 -0
  93. package/wait.ts +60 -0
package/kept.js ADDED
@@ -0,0 +1,128 @@
1
+ import * as io from "@yaks/file/io";
2
+ import * as system from "@yaks/process/system";
3
+ // Where a run keeps what it learned for the next: stores written whole
4
+ // through a rename, and what files held, kept by each file's status.
5
+ import { createHash } from 'node:crypto';
6
+ /** The machine's cache, where every store lies. */ export let cache = system.env.get('XDG_CACHE_HOME') ?? `${system.env.get('HOME')}/.cache`;
7
+ /** Where a checkout's own stores lie, by its path. Checkouts differ: one
8
+ * store they all shared would hold whichever checkout wrote it last, and each
9
+ * run would ask deno again of every module another checkout had changed. */ export let stores = (cwd = system.cwd())=>`${cache}/yak/checkouts/${createHash('sha256').update(cwd).digest('hex').slice(0, 16)}`;
10
+ /** A store every checkout on the machine starts from, one of `name`'s by
11
+ * `key`: checkouts whose `key` differs (another revision's configuration)
12
+ * each keep their own, rather than one replacing what the other wrote. One
13
+ * no run has written for two days goes as the next is asked for. */ export let seed = (name, key)=>{
14
+ let dir = `${cache}/yak/${name}`;
15
+ try {
16
+ for (let e of io.readDirSync(dir)){
17
+ let at = `${dir}/${e.name}`;
18
+ try {
19
+ if (Date.now() - io.statSync(at).mtime.getTime() > 2 * DAY) {
20
+ io.removeSync(at);
21
+ }
22
+ } catch {}
23
+ }
24
+ } catch {}
25
+ return `${dir}/${createHash('sha256').update(key).digest('hex').slice(0, 16)}.json`;
26
+ };
27
+ let DAY = 86_400_000;
28
+ // A checkout's stores, made with its first: the stores of checkouts since
29
+ // removed go then. Each names its checkout beside them.
30
+ let room = (store)=>{
31
+ let dir = store.slice(0, store.lastIndexOf('/'));
32
+ if (gone(dir)) {
33
+ io.mkdirSync(dir, {
34
+ recursive: true
35
+ });
36
+ if (dir == stores()) {
37
+ io.writeTextFileSync(`${dir}/checkout`, system.cwd());
38
+ let all = dir.slice(0, dir.lastIndexOf('/'));
39
+ for (let e of io.readDirSync(all)){
40
+ let checkout = `${all}/${e.name}/checkout`;
41
+ if (!gone(checkout) && gone(io.readTextFileSync(checkout))) {
42
+ io.removeSync(`${all}/${e.name}`, {
43
+ recursive: true
44
+ });
45
+ }
46
+ }
47
+ }
48
+ }
49
+ };
50
+ let gone = (path)=>{
51
+ try {
52
+ io.statSync(path);
53
+ return false;
54
+ } catch {
55
+ return true;
56
+ }
57
+ };
58
+ /** What a JSON store holds; nothing, when there is none. */ export let load = (store)=>{
59
+ try {
60
+ return JSON.parse(io.readTextFileSync(store));
61
+ } catch {
62
+ return {};
63
+ }
64
+ };
65
+ /** Adds `text` to the end of a store. Writes from runs at once each land
66
+ * whole. */ export let append = (text, store)=>{
67
+ room(store);
68
+ io.writeTextFileSync(store, text, {
69
+ append: true
70
+ });
71
+ };
72
+ /** A store written whole, through a rename, so a reader never reads half; a
73
+ * `copy` holding the same is the same file, linked. */ export let put = (text, store, copy)=>{
74
+ room(store);
75
+ let draft = `${store}.${system.pid}`;
76
+ io.writeTextFileSync(draft, text);
77
+ if (copy) {
78
+ room(copy);
79
+ let also = `${copy}.${system.pid}`;
80
+ io.linkSync(draft, also);
81
+ io.renameSync(also, copy);
82
+ }
83
+ io.renameSync(draft, store);
84
+ };
85
+ /** A JSON store written whole. */ export let write = (all, store)=>put(JSON.stringify(all), store);
86
+ /**
87
+ * What files held, by `read`, kept in `store` beside each file's status: a
88
+ * file whose status still matches is not read again. A file changed within
89
+ * the second before it was read could change again within its times' grain,
90
+ * so it is not kept. `save` writes the store when what it holds changed: a
91
+ * file read anew, or one it held and no longer does. A file not asked of
92
+ * stays held when it lies `beyond` what the caller looked at.
93
+ */ export let byStatus = (store)=>{
94
+ let before = store ? load(store) : {};
95
+ let after = {};
96
+ let fresh = false;
97
+ return {
98
+ of: (path, s, read)=>{
99
+ let status = [
100
+ s.ino,
101
+ s.size,
102
+ s.mtime.getTime(),
103
+ s.ctime.getTime()
104
+ ];
105
+ let was = before[path];
106
+ if (was && status.every((n, i)=>was[i] == n)) {
107
+ after[path] = was;
108
+ return was[4];
109
+ }
110
+ let got = read();
111
+ if (status[3] < Date.now() - 1000) {
112
+ after[path] = [
113
+ ...status,
114
+ got
115
+ ];
116
+ fresh = true;
117
+ }
118
+ return got;
119
+ },
120
+ save: (beyond = ()=>false)=>{
121
+ for (let [path, was] of Object.entries(before)){
122
+ if (!(path in after) && beyond(path)) after[path] = was;
123
+ }
124
+ if (store && (fresh || Object.keys(after).length != Object.keys(before).length)) write(after, store);
125
+ }
126
+ };
127
+ };
128
+ //# sourceMappingURL=kept.js.map
package/kept.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kept.js","s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AAS,CAAC,MAAM,OAAM;AAKjC;;;;;;;CAOC,GACD,OAAO,IAAI,WAAW,CAAI;EAIxB,IAAI,SAAyC,QAAQ,KAAK,SAAS,CAAC;EACpE,IAAI,QAAwC,CAAC;EAC7C,IAAI,QAAQ;EACZ,OAAO;IACL,IAAI,CAAC,MAAc,GAAgB;MACjC,IAAI,SAAiB;QAAC,EAAE,GAAG;QAAE,EAAE,IAAI;QAAE,EAAE,KAAK,CAAC,OAAO;QAAI,EAAE,KAAK,CAAC,OAAO;OAAG;MAC1E,IAAI,MAAM,MAAM,CAAC,KAAK;MACtB,IAAI,OAAO,OAAO,KAAK,CAAC,CAAC,GAAG,IAAM,GAAG,CAAC,EAAE,IAAI,IAAI;QAC9C,KAAK,CAAC,KAAK,GAAG;QACd,OAAO,GAAG,CAAC,EAAE;MACf;MACA,IAAI,MAAM;MACV,IAAI,MAAM,CAAC,EAAE,GAAG,KAAK,GAAG,KAAK,MAAM;QACjC,KAAK,CAAC,KAAK,GAAG;aAAI;UAAQ;SAAI;QAC9B,QAAQ;MACV;MACA,OAAO;IACT;IACA,MAAM,CAAC,SAAoC,IAAM,KAAK;MACpD,KAAK,IAAI,CAAC,MAAM,IAAI,IAAI,OAAO,OAAO,CAAC,QAAS;QAC9C,IAAI,CAAC,CAAC,QAAQ,KAAK,KAAK,OAAO,OAAO,KAAK,CAAC,KAAK,GAAG;MACtD;MACA,IACE,SACA,CAAC,SAAS,OAAO,IAAI,CAAC,OAAO,MAAM,IAAI,OAAO,IAAI,CAAC,QAAQ,MAAM,GACjE,MAAM,OAAO;IACjB;EACF;AACF,EAAC"}
package/kept.ts ADDED
@@ -0,0 +1,146 @@
1
+ import * as io from '@yaks/file/io'
2
+ import * as system from '@yaks/process/system'
3
+ // Where a run keeps what it learned for the next: stores written whole
4
+ // through a rename, and what files held, kept by each file's status.
5
+ import { createHash } from 'node:crypto'
6
+
7
+ /** The machine's cache, where every store lies. */
8
+ export let cache: string = system.env.get('XDG_CACHE_HOME') ??
9
+ `${system.env.get('HOME')}/.cache`
10
+
11
+ /** Where a checkout's own stores lie, by its path. Checkouts differ: one
12
+ * store they all shared would hold whichever checkout wrote it last, and each
13
+ * run would ask deno again of every module another checkout had changed. */
14
+ export let stores = (cwd: string = system.cwd()): string =>
15
+ `${cache}/yak/checkouts/${
16
+ createHash('sha256').update(cwd).digest('hex').slice(0, 16)
17
+ }`
18
+
19
+ /** A store every checkout on the machine starts from, one of `name`'s by
20
+ * `key`: checkouts whose `key` differs (another revision's configuration)
21
+ * each keep their own, rather than one replacing what the other wrote. One
22
+ * no run has written for two days goes as the next is asked for. */
23
+ export let seed = (name: string, key: string): string => {
24
+ let dir = `${cache}/yak/${name}`
25
+ try {
26
+ for (let e of io.readDirSync(dir)) {
27
+ let at = `${dir}/${e.name}`
28
+ try {
29
+ if (Date.now() - io.statSync(at).mtime.getTime() > 2 * DAY) {
30
+ io.removeSync(at)
31
+ }
32
+ } catch { /* another run removed it */ }
33
+ }
34
+ } catch { /* none kept yet */ }
35
+ return `${dir}/${
36
+ createHash('sha256').update(key).digest('hex').slice(0, 16)
37
+ }.json`
38
+ }
39
+ let DAY = 86_400_000
40
+
41
+ // A checkout's stores, made with its first: the stores of checkouts since
42
+ // removed go then. Each names its checkout beside them.
43
+ let room = (store: string) => {
44
+ let dir = store.slice(0, store.lastIndexOf('/'))
45
+ if (gone(dir)) {
46
+ io.mkdirSync(dir, { recursive: true })
47
+ if (dir == stores()) {
48
+ io.writeTextFileSync(`${dir}/checkout`, system.cwd())
49
+ let all = dir.slice(0, dir.lastIndexOf('/'))
50
+ for (let e of io.readDirSync(all)) {
51
+ let checkout = `${all}/${e.name}/checkout`
52
+ if (!gone(checkout) && gone(io.readTextFileSync(checkout))) {
53
+ io.removeSync(`${all}/${e.name}`, { recursive: true })
54
+ }
55
+ }
56
+ }
57
+ }
58
+ }
59
+ let gone = (path: string) => {
60
+ try {
61
+ io.statSync(path)
62
+ return false
63
+ } catch {
64
+ return true
65
+ }
66
+ }
67
+
68
+ /** What a JSON store holds; nothing, when there is none. */
69
+ export let load = <T>(store: string): Record<string, T> => {
70
+ try {
71
+ return JSON.parse(io.readTextFileSync(store))
72
+ } catch {
73
+ return {}
74
+ }
75
+ }
76
+
77
+ /** Adds `text` to the end of a store. Writes from runs at once each land
78
+ * whole. */
79
+ export let append = (text: string, store: string): void => {
80
+ room(store)
81
+ io.writeTextFileSync(store, text, { append: true })
82
+ }
83
+
84
+ /** A store written whole, through a rename, so a reader never reads half; a
85
+ * `copy` holding the same is the same file, linked. */
86
+ export let put = (text: string, store: string, copy?: string): void => {
87
+ room(store)
88
+ let draft = `${store}.${system.pid}`
89
+ io.writeTextFileSync(draft, text)
90
+ if (copy) {
91
+ room(copy)
92
+ let also = `${copy}.${system.pid}`
93
+ io.linkSync(draft, also)
94
+ io.renameSync(also, copy)
95
+ }
96
+ io.renameSync(draft, store)
97
+ }
98
+
99
+ /** A JSON store written whole. */
100
+ export let write = (all: unknown, store: string): void =>
101
+ put(JSON.stringify(all), store)
102
+
103
+ // A file's status: its inode, size, and modified and changed times.
104
+ type Status = [number, number, number, number]
105
+
106
+ /**
107
+ * What files held, by `read`, kept in `store` beside each file's status: a
108
+ * file whose status still matches is not read again. A file changed within
109
+ * the second before it was read could change again within its times' grain,
110
+ * so it is not kept. `save` writes the store when what it holds changed: a
111
+ * file read anew, or one it held and no longer does. A file not asked of
112
+ * stays held when it lies `beyond` what the caller looked at.
113
+ */
114
+ export let byStatus = <T>(store?: string): {
115
+ of: (path: string, s: io.FileInfo, read: () => T) => T
116
+ save: (beyond?: (path: string) => boolean) => void
117
+ } => {
118
+ let before: Record<string, [...Status, T]> = store ? load(store) : {}
119
+ let after: Record<string, [...Status, T]> = {}
120
+ let fresh = false
121
+ return {
122
+ of: (path: string, s: io.FileInfo, read: () => T): T => {
123
+ let status: Status = [s.ino, s.size, s.mtime.getTime(), s.ctime.getTime()]
124
+ let was = before[path]
125
+ if (was && status.every((n, i) => was[i] == n)) {
126
+ after[path] = was
127
+ return was[4]
128
+ }
129
+ let got = read()
130
+ if (status[3] < Date.now() - 1000) {
131
+ after[path] = [...status, got]
132
+ fresh = true
133
+ }
134
+ return got
135
+ },
136
+ save: (beyond: (path: string) => boolean = () => false) => {
137
+ for (let [path, was] of Object.entries(before)) {
138
+ if (!(path in after) && beyond(path)) after[path] = was
139
+ }
140
+ if (
141
+ store &&
142
+ (fresh || Object.keys(after).length != Object.keys(before).length)
143
+ ) write(after, store)
144
+ },
145
+ }
146
+ }
package/lend.js ADDED
@@ -0,0 +1,30 @@
1
+ // A global a test sets for a while, such as `document` for a page it draws,
2
+ // and puts back. Files run in one runtime, so whatever is there may be
3
+ // another module's: the terminal's document is installed read-only as its
4
+ // module loads (@yaks/browse/tui/doc), and a plain assignment over it throws.
5
+ /**
6
+ * Lend `globalThis[name]` the value `value`, over whatever is there, read-only
7
+ * or not; the call it returns puts back what was there, or removes the name
8
+ * where nothing was.
9
+ *
10
+ * ```ts
11
+ * import { equal, lend } from '@yaks/testing'
12
+ *
13
+ * let back = lend('lent', 1)
14
+ * equal((globalThis as { lent?: number }).lent, 1)
15
+ * back()
16
+ * equal('lent' in globalThis, false)
17
+ * ```
18
+ */ export let lend = (name, value)=>{
19
+ let prior = Object.getOwnPropertyDescriptor(globalThis, name);
20
+ Object.defineProperty(globalThis, name, {
21
+ value,
22
+ configurable: true,
23
+ writable: true
24
+ });
25
+ return ()=>{
26
+ if (prior) Object.defineProperty(globalThis, name, prior);
27
+ else Reflect.deleteProperty(globalThis, name);
28
+ };
29
+ };
30
+ //# sourceMappingURL=lend.js.map
package/lend.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lend.js","sources":["./lend.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,uEAAuE;AACvE,0EAA0E;AAC1E,8EAA8E;AAE9E;;;;;;;;;;;;;CAaC,GACD,OAAO,IAAI,OAAO,CAAC,MAAc;EAC/B,IAAI,QAAQ,OAAO,wBAAwB,CAAC,YAAY;EACxD,OAAO,cAAc,CAAC,YAAY,MAAM;IACtC;IACA,cAAc;IACd,UAAU;EACZ;EACA,OAAO;IACL,IAAI,OAAO,OAAO,cAAc,CAAC,YAAY,MAAM;SAC9C,QAAQ,cAAc,CAAC,YAAY;EAC1C;AACF,EAAC"}
package/lend.ts ADDED
@@ -0,0 +1,31 @@
1
+ // A global a test sets for a while, such as `document` for a page it draws,
2
+ // and puts back. Files run in one runtime, so whatever is there may be
3
+ // another module's: the terminal's document is installed read-only as its
4
+ // module loads (@yaks/browse/tui/doc), and a plain assignment over it throws.
5
+
6
+ /**
7
+ * Lend `globalThis[name]` the value `value`, over whatever is there, read-only
8
+ * or not; the call it returns puts back what was there, or removes the name
9
+ * where nothing was.
10
+ *
11
+ * ```ts
12
+ * import { equal, lend } from '@yaks/testing'
13
+ *
14
+ * let back = lend('lent', 1)
15
+ * equal((globalThis as { lent?: number }).lent, 1)
16
+ * back()
17
+ * equal('lent' in globalThis, false)
18
+ * ```
19
+ */
20
+ export let lend = (name: string, value: unknown): () => void => {
21
+ let prior = Object.getOwnPropertyDescriptor(globalThis, name)
22
+ Object.defineProperty(globalThis, name, {
23
+ value,
24
+ configurable: true,
25
+ writable: true,
26
+ })
27
+ return () => {
28
+ if (prior) Object.defineProperty(globalThis, name, prior)
29
+ else Reflect.deleteProperty(globalThis, name)
30
+ }
31
+ }
package/load.js ADDED
@@ -0,0 +1,106 @@
1
+ import * as io from "@yaks/file/io";
2
+ import * as system from "@yaks/process/system";
3
+ // A page's examples, loaded as the tests they declare. Each compiled module
4
+ // is written beside its page, so its imports resolve as the page's do, as a
5
+ // hidden file named for the page, the example and this process
6
+ // (`.README.md.26.4242.example.ts`), and removed once loaded. One left by a
7
+ // process that has since ended is swept by the next run.
8
+ import { compileDoctests, compileFence, fenceName, module, scan, specifiers } from "./examples.js";
9
+ import { written } from "./find.js";
10
+ import { test } from "./suite.js";
11
+ let url = (path)=>new URL(path, `file://${system.cwd()}/`).href;
12
+ let EXT = {
13
+ tsx: 'tsx',
14
+ jsx: 'jsx',
15
+ js: 'js',
16
+ javascript: 'js',
17
+ mjs: 'js'
18
+ };
19
+ /** Where an example of `page` is written while it loads. */ export let beside = (page, tag, lang = 'ts')=>{
20
+ let i = page.lastIndexOf('/');
21
+ return `${page.slice(0, i + 1)}.${page.slice(i + 1)}.${tag}.${system.pid}.example.${EXT[lang] ?? 'ts'}`;
22
+ };
23
+ let alive = (pid)=>{
24
+ try {
25
+ return io.statSync(`/proc/${pid}`).isDirectory;
26
+ } catch {
27
+ return !io.statSync('/proc').isDirectory;
28
+ }
29
+ };
30
+ /** Removes the examples an ended run left beside `pages`. */ export let sweep = async (pages)=>{
31
+ let dirs = new Set(pages.map((p)=>p.slice(0, p.lastIndexOf('/') + 1) || './'));
32
+ for (let dir of dirs){
33
+ for (let e of io.readDirSync(dir)){
34
+ let pid = written(e.name);
35
+ if (pid && !alive(pid)) {
36
+ await io.remove(`${dir}${e.name}`).catch(()=>{});
37
+ }
38
+ }
39
+ }
40
+ };
41
+ /**
42
+ * For each page, a module importing what its examples import, fenced or
43
+ * doctest, statically or by a literal `import(…)` (the page itself too, when
44
+ * it is a module), written beside it: what the page's examples depend on, as
45
+ * a module graph can say. The caller removes them.
46
+ */ export let plan = (pages, parsed)=>Promise.try(()=>pages.map((page)=>{
47
+ let { fenced, lines } = parsed?.get(page) ?? scan(io.readTextFileSync(page), page);
48
+ let base = page.slice(page.lastIndexOf('/') + 1);
49
+ let named = new Set([
50
+ ...module(page) ? [
51
+ `./${base}`
52
+ ] : [],
53
+ ...[
54
+ ...fenced,
55
+ ...lines.filter((l)=>!l.skip)
56
+ ].flatMap((e)=>specifiers(e.code))
57
+ ]);
58
+ let path = beside(page, 'plan');
59
+ io.writeTextFileSync(path, [
60
+ ...named
61
+ ].map((s)=>`import ${JSON.stringify(s)}\n`).join(''));
62
+ return [
63
+ page,
64
+ path
65
+ ];
66
+ }));
67
+ // Imports one compiled example; one that cannot load fails as a test.
68
+ let load = async (page, tag, path)=>{
69
+ try {
70
+ await import(url(path));
71
+ } catch (error) {
72
+ test(`${page}:${tag} loads`, ()=>{
73
+ throw error;
74
+ });
75
+ }
76
+ };
77
+ /** The names a module exports. */ let exported = async (page)=>Object.keys(await import(url(page)));
78
+ /** Loads a page's examples, each declaring its test. */ export let examples = async (page, parsed)=>{
79
+ let { fenced, lines } = parsed ?? scan(io.readTextFileSync(page), page);
80
+ let exports = module(page) ? await exported(page) : [];
81
+ let files = fenced.map((f)=>({
82
+ tag: String(f.line),
83
+ path: beside(page, String(f.line), f.lang),
84
+ code: fenceName(page, f).then((name)=>compileFence(name, page, f, exports))
85
+ }));
86
+ if (lines.length) {
87
+ files.push({
88
+ tag: 'doctests',
89
+ path: beside(page, 'doctests'),
90
+ code: Promise.resolve(compileDoctests(page, lines, exports))
91
+ });
92
+ }
93
+ try {
94
+ // The sources can be prepared together. Import in page order, since
95
+ // evaluation can register tests or initialize shared modules.
96
+ let writes = await Promise.allSettled(files.map(async ({ path, code })=>await io.writeTextFile(path, await code)));
97
+ for (let [i, { tag, path }] of files.entries()){
98
+ let wrote = writes[i];
99
+ if (wrote.status == 'rejected') throw wrote.reason;
100
+ await load(page, tag, path);
101
+ }
102
+ } finally{
103
+ await Promise.allSettled(files.map(({ path })=>io.remove(path)));
104
+ }
105
+ };
106
+ //# sourceMappingURL=load.js.map
package/load.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"load.js","sources":["./load.ts"],"names":[],"mappings":"AAAA,YAAY,6BAAkC;AAC9C,YAAY,wCAA6C;AACzD,4EAA4E;AAC5E,4EAA4E;AAC5E,+DAA+D;AAC/D,4EAA4E;AAC5E,yDAAyD;AACzD,SACE,eAAe,EACf,YAAY,EAEZ,SAAS,EACT,MAAM,EACN,IAAI,EACJ,UAAU,wBACU;AACtB,SAAS,OAAO,oBAAmB;AACnC,SAAS,IAAI,qBAAoB;AAEjC,IAAI,MAAM,CAAC,OAAiB,IAAI,IAAI,MAAM,CAAC,OAAO,EA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C;EACD,IAAI,MAAM,MAAM,EAAE;IAChB,MAAM,IAAI,CAAC;MACT,KAAK;MACL,MAAM,OAAO,MAAM;MACnB,MAAM,QAAQ,OAAO,CAAC,gBAAgB,MAAM,OAAO;IACrD;EACF;EACA,IAAI;IACF,oEAAoE;IACpE,8DAA8D;IAC9D,IAAI,SAAS,MAAM,QAAQ,UAAU,CACnC,MAAM,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,GAC7B,MAAM,GAAG,aAAa,CAAC,MAAM,MAAM;IAGvC,KAAK,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,IAAI,MAAM,OAAO,GAAI;MAC9C,IAAI,QAAQ,MAAM,CAAC,EAAE;MACrB,IAAI,MAAM,MAAM,IAAI,YAAY,MAAM,MAAM,MAAM;MAClD,MAAM,KAAK,MAAM,KAAK;IACxB;EACF,SAAU;IACR,MAAM,QAAQ,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,GAAK,GAAG,MAAM,CAAC;EAC7D;AACF,EAAC"}
package/load.ts ADDED
@@ -0,0 +1,139 @@
1
+ import * as io from '@yaks/file/io'
2
+ import * as system from '@yaks/process/system'
3
+ // A page's examples, loaded as the tests they declare. Each compiled module
4
+ // is written beside its page, so its imports resolve as the page's do, as a
5
+ // hidden file named for the page, the example and this process
6
+ // (`.README.md.26.4242.example.ts`), and removed once loaded. One left by a
7
+ // process that has since ended is swept by the next run.
8
+ import {
9
+ compileDoctests,
10
+ compileFence,
11
+ type Examples,
12
+ fenceName,
13
+ module,
14
+ scan,
15
+ specifiers,
16
+ } from './examples.js'
17
+ import { written } from './find.js'
18
+ import { test } from './suite.js'
19
+
20
+ let url = (path: string) => new URL(path, `file://${system.cwd()}/`).href
21
+
22
+ let EXT: Record<string, string> = {
23
+ tsx: 'tsx',
24
+ jsx: 'jsx',
25
+ js: 'js',
26
+ javascript: 'js',
27
+ mjs: 'js',
28
+ }
29
+
30
+ /** Where an example of `page` is written while it loads. */
31
+ export let beside = (page: string, tag: string, lang = 'ts') => {
32
+ let i = page.lastIndexOf('/')
33
+ return `${page.slice(0, i + 1)}.${
34
+ page.slice(i + 1)
35
+ }.${tag}.${system.pid}.example.${EXT[lang] ?? 'ts'}`
36
+ }
37
+
38
+ let alive = (pid: number) => {
39
+ try {
40
+ return io.statSync(`/proc/${pid}`).isDirectory
41
+ } catch {
42
+ return !io.statSync('/proc').isDirectory
43
+ }
44
+ }
45
+
46
+ /** Removes the examples an ended run left beside `pages`. */
47
+ export let sweep = async (pages: string[]) => {
48
+ let dirs = new Set(
49
+ pages.map((p) => p.slice(0, p.lastIndexOf('/') + 1) || './'),
50
+ )
51
+ for (let dir of dirs) {
52
+ for (let e of io.readDirSync(dir)) {
53
+ let pid = written(e.name)
54
+ if (pid && !alive(pid)) {
55
+ await io.remove(`${dir}${e.name}`).catch(() => {})
56
+ }
57
+ }
58
+ }
59
+ }
60
+
61
+ /**
62
+ * For each page, a module importing what its examples import, fenced or
63
+ * doctest, statically or by a literal `import(…)` (the page itself too, when
64
+ * it is a module), written beside it: what the page's examples depend on, as
65
+ * a module graph can say. The caller removes them.
66
+ */
67
+ export let plan = (
68
+ pages: string[],
69
+ parsed?: ReadonlyMap<string, Examples>,
70
+ ) =>
71
+ Promise.try(() =>
72
+ pages.map((page) => {
73
+ let { fenced, lines } = parsed?.get(page) ??
74
+ scan(io.readTextFileSync(page), page)
75
+ let base = page.slice(page.lastIndexOf('/') + 1)
76
+ let named = new Set([
77
+ ...module(page) ? [`./${base}`] : [],
78
+ ...[...fenced, ...lines.filter((l) => !l.skip)]
79
+ .flatMap((e) => specifiers(e.code)),
80
+ ])
81
+ let path = beside(page, 'plan')
82
+ io.writeTextFileSync(
83
+ path,
84
+ [...named].map((s) => `import ${JSON.stringify(s)}\n`).join(''),
85
+ )
86
+ return [page, path] as const
87
+ })
88
+ )
89
+
90
+ // Imports one compiled example; one that cannot load fails as a test.
91
+ let load = async (page: string, tag: string, path: string) => {
92
+ try {
93
+ await import(url(path))
94
+ } catch (error) {
95
+ test(`${page}:${tag} loads`, () => {
96
+ throw error
97
+ })
98
+ }
99
+ }
100
+
101
+ /** The names a module exports. */
102
+ let exported = async (page: string): Promise<string[]> =>
103
+ Object.keys(await import(url(page)))
104
+
105
+ /** Loads a page's examples, each declaring its test. */
106
+ export let examples = async (page: string, parsed?: Examples) => {
107
+ let { fenced, lines } = parsed ?? scan(io.readTextFileSync(page), page)
108
+ let exports = module(page) ? await exported(page) : []
109
+ let files = fenced.map((f) => ({
110
+ tag: String(f.line),
111
+ path: beside(page, String(f.line), f.lang),
112
+ code: fenceName(page, f).then((name) =>
113
+ compileFence(name, page, f, exports)
114
+ ),
115
+ }))
116
+ if (lines.length) {
117
+ files.push({
118
+ tag: 'doctests',
119
+ path: beside(page, 'doctests'),
120
+ code: Promise.resolve(compileDoctests(page, lines, exports)),
121
+ })
122
+ }
123
+ try {
124
+ // The sources can be prepared together. Import in page order, since
125
+ // evaluation can register tests or initialize shared modules.
126
+ let writes = await Promise.allSettled(
127
+ files.map(async ({ path, code }) =>
128
+ await io.writeTextFile(path, await code)
129
+ ),
130
+ )
131
+ for (let [i, { tag, path }] of files.entries()) {
132
+ let wrote = writes[i]
133
+ if (wrote.status == 'rejected') throw wrote.reason
134
+ await load(page, tag, path)
135
+ }
136
+ } finally {
137
+ await Promise.allSettled(files.map(({ path }) => io.remove(path)))
138
+ }
139
+ }