| 1 | import os |
| 2 | import term |
| 3 | import v.util.vtest |
| 4 | import v.util.diff |
| 5 | |
| 6 | const vexe = @VEXE |
| 7 | |
| 8 | const vroot = @VMODROOT |
| 9 | |
| 10 | const github_job = os.getenv('GITHUB_JOB') |
| 11 | |
| 12 | @[noreturn] |
| 13 | fn exit_because(msg string) { |
| 14 | eprintln('${msg}, tests will not run') |
| 15 | exit(0) |
| 16 | } |
| 17 | |
| 18 | fn 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 | |
| 36 | fn 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 | |
| 45 | fn get_main_files_in_dir(dir string) []string { |
| 46 | mut mfiles := os.walk_ext(dir, '.v') |
| 47 | mfiles.sort() |
| 48 | return mfiles |
| 49 | } |
| 50 | |
| 51 | fn 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 | |
| 91 | fn 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 | |