| 1 | import os |
| 2 | import term |
| 3 | import v.util.vtest |
| 4 | import v.util.diff |
| 5 | |
| 6 | struct FileOptions { |
| 7 | mut: |
| 8 | vflags string |
| 9 | } |
| 10 | |
| 11 | fn get_file_options(file string) FileOptions { |
| 12 | mut res := FileOptions{} |
| 13 | lines := os.read_lines(file) or { [] } |
| 14 | for line in lines { |
| 15 | if line.starts_with('// vtest vflags:') { |
| 16 | res.vflags = line.all_after(':').trim_space() |
| 17 | } |
| 18 | } |
| 19 | return res |
| 20 | } |
| 21 | |
| 22 | fn test_vet() { |
| 23 | vexe := os.getenv('VEXE') |
| 24 | vroot := os.dir(vexe) |
| 25 | os.chdir(vroot)! |
| 26 | test_dir := 'cmd/tools/vvet/tests' |
| 27 | tests := get_tests_in_dir(test_dir) |
| 28 | fails := check_path(vexe, test_dir, tests) |
| 29 | assert fails == 0 |
| 30 | } |
| 31 | |
| 32 | fn get_tests_in_dir(dir string) []string { |
| 33 | files := os.ls(dir) or { panic(err) } |
| 34 | mut tests := files.filter(it.ends_with('.vv')) |
| 35 | tests.sort() |
| 36 | return tests |
| 37 | } |
| 38 | |
| 39 | fn check_path(vexe string, dir string, tests []string) int { |
| 40 | mut nb_fail := 0 |
| 41 | paths := vtest.filter_vtest_only(tests, basepath: dir) |
| 42 | for path in paths { |
| 43 | program := path |
| 44 | print(path + ' ') |
| 45 | file_options := get_file_options(path) |
| 46 | res := |
| 47 | os.execute('${os.quoted_path(vexe)} vet -nocolor ${file_options.vflags} ${os.quoted_path(program)}') |
| 48 | if res.exit_code < 0 { |
| 49 | panic(res.output) |
| 50 | } |
| 51 | mut expected := os.read_file(program.replace('.vv', '') + '.out') or { panic(err) } |
| 52 | expected = clean_line_endings(expected) |
| 53 | found := clean_line_endings(res.output) |
| 54 | if expected != found { |
| 55 | println(term.red('FAIL')) |
| 56 | println('============') |
| 57 | if diff_ := diff.compare_text(expected, found) { |
| 58 | println('diff:') |
| 59 | println(diff_) |
| 60 | } else { |
| 61 | println('expected:') |
| 62 | println(expected) |
| 63 | println('============') |
| 64 | println('found:') |
| 65 | println(found) |
| 66 | } |
| 67 | println('============\n') |
| 68 | nb_fail++ |
| 69 | } else { |
| 70 | println(term.green('OK')) |
| 71 | } |
| 72 | } |
| 73 | return nb_fail |
| 74 | } |
| 75 | |
| 76 | fn clean_line_endings(s string) string { |
| 77 | mut res := s.trim_space() |
| 78 | res = res.replace(' \n', '\n') |
| 79 | res = res.replace(' \r\n', '\n') |
| 80 | res = res.replace('\r\n', '\n') |
| 81 | res = res.trim('\n') |
| 82 | return res |
| 83 | } |
| 84 | |