deno.land / std@0.173.0 / wasi / snapshot_preview1_test.ts

snapshot_preview1_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
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import Context from "./snapshot_preview1.ts";import { assertEquals, assertThrows } from "../testing/asserts.ts";import { copy } from "../fs/copy.ts";import * as path from "../path/mod.ts";import { writeAll } from "../streams/write_all.ts";import { isWindows } from "../_util/os.ts";
const tests = [ "testdata/std_env_args.wasm", "testdata/std_env_vars.wasm", "testdata/std_fs_create_dir.wasm", "testdata/std_fs_file_create.wasm", "testdata/std_fs_file_metadata.wasm", "testdata/std_fs_file_seek.wasm", "testdata/std_fs_file_set_len.wasm", "testdata/std_fs_file_sync_all.wasm", "testdata/std_fs_file_sync_data.wasm", "testdata/std_fs_hard_link.wasm", "testdata/std_fs_metadata.wasm", "testdata/std_fs_read.wasm", "testdata/std_fs_read_dir.wasm", "testdata/std_fs_remove_dir_all.wasm", "testdata/std_fs_rename.wasm", "testdata/std_fs_symlink_metadata.wasm", "testdata/std_fs_write.wasm", "testdata/std_io_stderr.wasm", "testdata/std_io_stdin.wasm", "testdata/std_io_stdout.wasm", "testdata/std_process_exit.wasm", "testdata/wasi_clock_res_get.wasm", "testdata/wasi_clock_time_get.wasm", "testdata/wasi_fd_fdstat_get.wasm", "testdata/wasi_fd_fdstat_get.wasm", "testdata/wasi_fd_fdstat_get.wasm", "testdata/wasi_fd_renumber.wasm", "testdata/wasi_fd_tell_file.wasm", "testdata/wasi_fd_write_file.wasm", "testdata/wasi_fd_write_stderr.wasm", "testdata/wasi_fd_write_stdout.wasm", "testdata/wasi_path_open.wasm", "testdata/wasi_proc_exit.wasm", "testdata/wasi_random_get.wasm", "testdata/wasi_sched_yield.wasm",];
const ignore = [];
// TODO(caspervonb) investigate why these tests are failing on windows and fix// them.// The failing tests all involve symlinks in some way, my best guess so far is// that there's something going wrong with copying the symlinks over to the// temporary working directory, but only in some cases.if (isWindows) { ignore.push("testdata/std_fs_metadata.wasm"); ignore.push("testdata/std_fs_read_dir.wasm"); ignore.push("testdata/wasi_path_open.wasm");}
const rootdir = path.dirname(path.fromFileUrl(import.meta.url));const testdir = path.join(rootdir, "testdata");
for (const pathname of tests) { Deno.test({ name: path.basename(pathname), ignore: ignore.includes(pathname), fn: async function () { const prelude = await Deno.readTextFile( path.resolve(rootdir, pathname.replace(/\.wasm$/, ".json")), ); const options = JSON.parse(prelude);
// TODO(caspervonb) investigate more. // On Windows creating a tempdir in the default directory breaks nearly // all the tests, possibly due to symlinks pointing to the original file // which crosses drive boundaries. const workdir = await Deno.makeTempDir({ dir: testdir, });
await copy( path.join(testdir, "fixtures"), path.join(workdir, "fixtures"), );
try { const process = new Deno.Command(Deno.execPath(), { cwd: workdir, args: [ "run", "--quiet", "--unstable", "--allow-all", "--no-check", path.resolve(rootdir, "snapshot_preview1_test_runner.ts"), prelude, path.resolve(rootdir, pathname), ], stdin: "piped", stdout: "piped", stderr: "piped", }); const child = process.spawn();
if (options.stdin) { const writer = child.stdin.getWriter(); await writer.write(new TextEncoder().encode(options.stdin)); writer.releaseLock(); }
child.stdin.close();
const { code, stdout, stderr } = await child.output();
if (options.stdout) { assertEquals(new TextDecoder().decode(stdout), options.stdout); } else { await writeAll(Deno.stdout, stdout); }
if (options.stderr) { assertEquals(new TextDecoder().decode(stderr), options.stderr); } else { await writeAll(Deno.stderr, stderr); }
assertEquals(code, options.exitCode ? +options.exitCode : 0); } catch (err) { throw err; } finally { await Deno.remove(workdir, { recursive: true }); } }, });}
Deno.test("context_start", function () { assertThrows( () => { const context = new Context({}); context.start({ exports: { _start() {}, }, }); }, TypeError, "must provide a memory export", );
assertThrows( () => { const context = new Context({}); context.start({ exports: { _initialize() {}, memory: new WebAssembly.Memory({ initial: 1 }), }, }); }, TypeError, "export _initialize must not be a function", );
assertThrows( () => { const context = new Context({}); context.start({ exports: { memory: new WebAssembly.Memory({ initial: 1 }), }, }); }, TypeError, "export _start must be a function", );
{ const context = new Context({ exitOnReturn: false, }); const exitCode = context.start({ exports: { _start() { }, memory: new WebAssembly.Memory({ initial: 1 }), }, }); assertEquals(exitCode, null); }
{ const context = new Context({ exitOnReturn: false, }); const exitCode = context.start({ exports: { _start() { const exit = context.exports["proc_exit"] as CallableFunction; exit(0); }, memory: new WebAssembly.Memory({ initial: 1 }), }, }); assertEquals(exitCode, 0); }
assertThrows( () => { const context = new Context({}); context.start({ exports: { memory: new WebAssembly.Memory({ initial: 1 }), _start() {}, }, }); context.start({ exports: {}, }); }, Error, "WebAssembly.Instance has already started", );});
Deno.test("context_initialize", function () { assertThrows( () => { const context = new Context({}); context.initialize({ exports: { _initialize() {}, }, }); }, TypeError, "must provide a memory export", );
assertThrows( () => { const context = new Context({}); context.initialize({ exports: { _start() {}, memory: new WebAssembly.Memory({ initial: 1 }), }, }); }, TypeError, "export _start must not be a function", );
assertThrows( () => { const context = new Context({}); context.initialize({ exports: { memory: new WebAssembly.Memory({ initial: 1 }), _initialize() {}, }, }); context.initialize({ exports: {}, }); }, Error, "WebAssembly.Instance has already started", );
{ let wasCalled = false; const context = new Context({}); context.initialize({ exports: { _initialize() { wasCalled = true; }, memory: new WebAssembly.Memory({ initial: 1 }), }, }); assertEquals(wasCalled, true); }});
Deno.test("std_io_stdin.wasm with stdin as file", function () { const stdinPath = Deno.makeTempFileSync(); Deno.writeTextFileSync(stdinPath, "Hello, stdin!");
const stdinFile = Deno.openSync(stdinPath);
const context = new Context({ exitOnReturn: false, stdin: stdinFile.rid, });
const binary = Deno.readFileSync(path.join(testdir, "std_io_stdin.wasm")); const module = new WebAssembly.Module(binary); const instance = new WebAssembly.Instance(module, { wasi_snapshot_preview1: context.exports, });
context.start(instance);
stdinFile.close();});
Deno.test("std_io_stdout.wasm with stdout as file", function () { const stdoutPath = Deno.makeTempFileSync(); const stdoutFile = Deno.openSync(stdoutPath, { create: true, write: true });
const context = new Context({ exitOnReturn: false, stdout: stdoutFile.rid, });
const binary = Deno.readFileSync(path.join(testdir, "std_io_stdout.wasm")); const module = new WebAssembly.Module(binary); const instance = new WebAssembly.Instance(module, { wasi_snapshot_preview1: context.exports, });
context.start(instance);
stdoutFile.close();
assertEquals(Deno.readTextFileSync(stdoutPath), "Hello, stdout!");});
Deno.test("std_io_stderr.wasm with stderr as file", function () { const stderrPath = Deno.makeTempFileSync(); const stderrFile = Deno.openSync(stderrPath, { create: true, write: true });
const context = new Context({ exitOnReturn: false, stderr: stderrFile.rid, });
const binary = Deno.readFileSync(path.join(testdir, "std_io_stderr.wasm")); const module = new WebAssembly.Module(binary); const instance = new WebAssembly.Instance(module, { wasi_snapshot_preview1: context.exports, });
context.start(instance);
stderrFile.close();
assertEquals(Deno.readTextFileSync(stderrPath), "Hello, stderr!");});
std

Version Info

Tagged at
a year ago