v2 / vlib / v / gen / js / program_test.v
98 lines · 89 sloc · 2.37 KB · bc0662064896e701a9f21d762ab9440177149fa6
Raw
1import os
2import term
3import v.util.vtest
4import v.util.diff
5
6const vexe = @VEXE
7
8const vroot = @VMODROOT
9
10const github_job = os.getenv('GITHUB_JOB')
11
12@[noreturn]
13fn exit_because(msg string) {
14 eprintln('${msg}, tests will not run')
15 exit(0)
16}
17
18fn test_node_exists() {
19 res := os.execute('node --version')
20 if res.exit_code != 0 {
21 exit_because('node does not exist')
22 }
23 if !res.output.starts_with('v') {
24 exit_because('invalid node version')
25 }
26 version := res.output.trim_left('v').int()
27 if version < 10 {
28 exit_because('node should be at least version 10, but is currently version: ${version}')
29 }
30 println('Using node version: ${version}')
31 // if github_job != '' && os.user_os() == 'macos' {
32 // exit_because('program_test.v is flaky on macos')
33 //}
34}
35
36fn test_running_programs_compiled_with_the_js_backend() {
37 os.setenv('VCOLORS', 'never', true)
38 os.chdir(vroot) or {}
39 test_dir := 'vlib/v/gen/js/tests/testdata'
40 main_files := get_main_files_in_dir(test_dir)
41 fails := check_path(test_dir, main_files)!
42 assert fails == 0
43}
44
45fn get_main_files_in_dir(dir string) []string {
46 mut mfiles := os.walk_ext(dir, '.v')
47 mfiles.sort()
48 return mfiles
49}
50
51fn check_path(dir string, tests []string) !int {
52 mut nb_fail := 0
53 paths := vtest.filter_vtest_only(tests, basepath: vroot)
54 for path in paths {
55 program := path.replace(vroot + os.path_separator, '')
56 program_out := program.replace('.v', '.out')
57 if !os.exists(program_out) {
58 os.write_file(program_out, '')!
59 }
60 print(program + ' ')
61 res := os.execute('${os.quoted_path(vexe)} -b js_node run ${os.quoted_path(program)}')
62 if res.exit_code < 0 {
63 panic(res.output)
64 }
65 mut expected := os.read_file(program_out)!
66 expected = clean_line_endings(expected)
67 found := clean_line_endings(res.output)
68 if expected != found {
69 println(term.red('FAIL'))
70 println('============')
71 if diff_ := diff.compare_text(expected, found) {
72 println('diff:')
73 println(diff_)
74 } else {
75 println('expected ${program_out} content:')
76 println(expected)
77 println('============')
78 println('found:')
79 println(found)
80 }
81 println('============\n')
82 nb_fail++
83 } else {
84 println(term.green('OK'))
85 assert true
86 }
87 }
88 return nb_fail
89}
90
91fn clean_line_endings(s string) string {
92 mut res := s.trim_space()
93 res = res.replace(' \n', '\n')
94 res = res.replace(' \r\n', '\n')
95 res = res.replace('\r\n', '\n')
96 res = res.trim('\n')
97 return res
98}
99