v2 / vlib / v / gen / wasm / tests / wasm_test.v
106 lines · 97 sloc · 2.76 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1import os
2import benchmark
3import term
4
5const is_verbose = os.getenv('VTEST_SHOW_CMD') != ''
6
7// TODO: some logic copy pasted from valgrind_test.v and compiler_test.v, move to a module
8fn test_wasm() {
9 mut runtimes := ['wasmer', 'wasmtime', 'wavm', 'wasm3', 'iwasm']
10 mut runtime_found := false
11
12 for runtime in runtimes {
13 basename := $if windows { runtime + '.exe' } $else { runtime }
14
15 if rf := os.find_abs_path_of_executable(basename) {
16 runtime_found = true
17 break
18 }
19 }
20
21 if !runtime_found {
22 eprintln('cannot find suitable wasm runtime, exiting...')
23 if os.getenv('VTEST_ONLY') == 'wasm' {
24 exit(1)
25 }
26 exit(0)
27 }
28
29 mut bench := benchmark.new_benchmark()
30 vexe := os.getenv('VEXE')
31 vroot := os.dir(vexe)
32 dir := os.join_path(vroot, 'vlib/v/gen/wasm/tests')
33 files := os.ls(dir)!
34
35 wrkdir := os.join_path(os.vtmp_dir(), 'wasm_tests')
36 os.mkdir_all(wrkdir)!
37 defer {
38 os.rmdir_all(wrkdir) or {}
39 }
40 os.chdir(wrkdir)!
41 mut tests := files.filter(it.ends_with('.vv'))
42 if tests.len == 0 {
43 println('no wasm tests found')
44 assert false
45 }
46 $if windows {
47 // FIXME:
48 if os.getenv('CI') == 'true' {
49 tests = tests.filter(it !in ['arrays.vv', 'asm.vv', 'builtin.vv'])
50 }
51 }
52 bench.set_total_expected_steps(tests.len)
53 for test in tests {
54 bench.step()
55 full_test_path := os.real_path(os.join_path(dir, test))
56 test_file_name := os.file_name(test)
57 relative_test_path := full_test_path.replace(vroot + '/', '')
58 work_test_path := '${wrkdir}/${test_file_name}'
59 tmperrfile := '${dir}/${test}.tmperr'
60 outfile := '${dir}/${test}.out'
61 // force binaryen to print without colour
62 cmd := '${os.quoted_path(vexe)} -b wasm run ${os.quoted_path(full_test_path)} 2> ${os.quoted_path(tmperrfile)}'
63 if is_verbose {
64 println(cmd)
65 }
66 res_wasm := os.execute(cmd)
67 if res_wasm.exit_code != 0 {
68 bench.fail()
69 eprintln(bench.step_message_fail(cmd))
70
71 if os.exists(tmperrfile) {
72 err := os.read_file(tmperrfile)!
73 eprintln(err)
74 }
75
76 continue
77 }
78 os.rm(tmperrfile) or {}
79 if expected_ := os.read_file(outfile) {
80 mut expected := expected_
81 expected = expected.trim_right('\r\n').replace('\r\n', '\n')
82 mut found := res_wasm.output.trim_right('\r\n').replace('\r\n', '\n')
83 found = found.trim_space()
84 if expected != found {
85 println(term.red('FAIL'))
86 println('============')
87 println('expected: "${expected}" len=${expected.len}')
88 println('============')
89 println('found:"${found}" len=${found.len}')
90 println('============\n')
91 bench.fail()
92 continue
93 }
94 } else {
95 os.write_file(outfile, res_wasm.output.trim_right('\r\n').replace('\r\n', '\n'))!
96 }
97 bench.ok()
98 eprintln(bench.step_message_ok(relative_test_path))
99 }
100 bench.stop()
101 eprintln(term.h_divider('-'))
102 eprintln(bench.total_message('wasm'))
103 if bench.nfail > 0 {
104 exit(1)
105 }
106}
107