-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.test.ts
191 lines (182 loc) · 4.54 KB
/
code.test.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
/**
* Simple tests for the deno_tag, for now most usage is being covered.
*
* Only the `denoTag` input function is being tested because this is also the
* only export from the `code.ts` module.
*
* These tests must be run with the `unstable` flag:
* > `deno test --unstable code.test.ts`
*
* This is because the code makes usage of the unstable `Deno.bundle` and
* `Diagnostic` code parts.
*/
import { denoTag } from "./code.ts";
import { defaultBundler } from "./defaults.ts";
import {
assert,
assertEquals,
} from "https://deno.land/std@0.83.0/testing/asserts.ts";
Deno.test(
"Calls the runner function with the value from the <deno> tag",
async () => {
const file = "test.ts";
const runner = createRunnerMock((value) => {
assert(value.cmd);
assertEquals(
value.cmd,
["deno", "run", "--allow-read", "--allow-run", file],
);
});
const htmlText = `<deno run="${file}" />`;
await denoTag(htmlText, { runner });
},
);
Deno.test(
"Calls the runner function with output piped",
async () => {
const runner = createRunnerMock((value) => {
assert(value.stdout);
assertEquals(value.stdout, "piped");
});
const htmlText = `<deno run="file.ts" />`;
await denoTag(htmlText, { runner });
},
);
Deno.test(
"Sets the runner function arguments to be the <deno> tag attributes",
async () => {
const runner = createRunnerMock((value) => {
assert(value.cmd);
assertEquals(
value.cmd,
[
"deno",
"run",
"--allow-read",
"--allow-run",
"file.ts",
'arg1="test"',
'arg2="something"',
],
);
});
const htmlText = `<deno run="file.ts" arg1="test" arg2="something" />`;
await denoTag(htmlText, { runner });
},
);
Deno.test(
'Places a value of "true" on the boolean attributes of the <deno> tag',
async () => {
const runner = createRunnerMock((value) => {
assert(value.cmd);
assertEquals(
value.cmd,
[
"deno",
"run",
"--allow-read",
"--allow-run",
"file.ts",
'boolean="true"',
'isOk="true"',
],
);
});
const htmlText = `<deno run="file.ts" boolean isOk />`;
await denoTag(htmlText, { runner });
},
);
Deno.test(
"Can handle multi-line <deno> tags",
async () => {
const runner = createRunnerMock((value) => {
assert(value.cmd);
assertEquals(
value.cmd,
[
"deno",
"run",
"--allow-read",
"--allow-run",
"file.ts",
'boolean="true"',
'isOk="true"',
],
);
});
const htmlText = `
<p>This is a simple test</p>
<deno
run="file.ts"
boolean
isOk
/>`;
await denoTag(htmlText, { runner });
},
);
Deno.test(
"Can handle <deno> tags inside other tags",
async () => {
const runner = createRunnerMock((value) => {
assert(value.cmd);
assertEquals(
value.cmd,
[
"deno",
"run",
"--allow-read",
"--allow-run",
"file.ts",
'boolean="true"',
'isOk="true"',
],
);
});
const htmlText = `
<p>This is a simple test
<code><deno run="file.ts" boolean isOk></deno></code>
</p>`;
await denoTag(htmlText, { runner });
},
);
Deno.test(
"Calls the bundler function with the value from the <deno> tag",
async () => {
const file = "test.ts";
const bundler: typeof defaultBundler = (value, opts) => {
assertEquals(value, file);
return Promise.resolve(
{ files: { "deno:///bundle.js": "" } } as unknown as Deno.EmitResult,
);
};
const htmlText = `<deno bundle="${file}" />`;
await denoTag(htmlText, { bundler });
},
);
/**
* Helper function that creates a mock of the `Deno.run` function that runs
* the function provided as argument and always returns a dummy `Deno.Process`
* object.
**/
const createRunnerMock = (run: (value: Deno.RunOptions) => void) =>
(value: Deno.RunOptions) => {
run(value);
return ({
rid: 0,
pid: 0,
stdin: null,
stdout: null,
stderr: null,
status() {
return Promise.resolve({ success: true, code: 0, signal: undefined });
},
output() {
return Promise.resolve(new Uint8Array());
},
stderrOutput() {
return Promise.resolve(new Uint8Array());
},
close() {},
kill(signo: number) {},
}) as ReturnType<typeof Deno.run>;
};