| 1 | // vtest build: !sanitized_job? |
| 2 | import os |
| 3 | import term |
| 4 | import benchmark |
| 5 | import v.fmt |
| 6 | import v.parser |
| 7 | import v.ast |
| 8 | import v.pref |
| 9 | import v.util.diff |
| 10 | import v.util.vtest |
| 11 | |
| 12 | const vroot = @VEXEROOT |
| 13 | const fpref = &pref.Preferences{ |
| 14 | is_fmt: true |
| 15 | } |
| 16 | |
| 17 | fn run_fmt(mut input_files []string) { |
| 18 | fmt_message := 'checking that v fmt keeps already formatted files *unchanged*' |
| 19 | eprintln(term.header(fmt_message, '-')) |
| 20 | assert input_files.len > 0 |
| 21 | input_files = vtest.filter_vtest_only(input_files) |
| 22 | if input_files.len == 0 { |
| 23 | // No need to produce a failing test here. |
| 24 | eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY')) |
| 25 | exit(0) |
| 26 | } |
| 27 | input_files.sort() |
| 28 | mut fmt_bench := benchmark.new_benchmark() |
| 29 | fmt_bench.set_total_expected_steps(input_files.len + 1) |
| 30 | tmpfolder := os.temp_dir() |
| 31 | for istep, ipath in input_files { |
| 32 | fmt_bench.cstep = istep + 1 |
| 33 | fmt_bench.step() |
| 34 | expected_ocontent := os.read_file(ipath) or { |
| 35 | fmt_bench.fail() |
| 36 | eprintln(fmt_bench.step_message_fail('cannot read from ${ipath}')) |
| 37 | continue |
| 38 | } |
| 39 | mut table := ast.new_table() |
| 40 | file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref) |
| 41 | result_ocontent := fmt.fmt(file_ast, mut table, fpref, false) |
| 42 | if expected_ocontent != result_ocontent { |
| 43 | fmt_bench.fail() |
| 44 | eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) |
| 45 | vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}') |
| 46 | os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } |
| 47 | println(diff.compare_files(ipath, vfmt_result_file) or { err.msg() }) |
| 48 | continue |
| 49 | } |
| 50 | fmt_bench.ok() |
| 51 | eprintln(fmt_bench.step_message_ok(ipath)) |
| 52 | } |
| 53 | fmt_bench.stop() |
| 54 | eprintln(term.h_divider('-')) |
| 55 | eprintln(fmt_bench.total_message(fmt_message)) |
| 56 | assert fmt_bench.nfail == 0 |
| 57 | } |
| 58 | |
| 59 | fn get_test_files(path string) []string { |
| 60 | mut files := []string{} |
| 61 | mut ref := &files |
| 62 | os.walk(path, fn [mut ref] (p string) { |
| 63 | if p.ends_with('_keep.vv') || p.ends_with('_expected.vv') { |
| 64 | $if macos { |
| 65 | $if arm64 { |
| 66 | if os.file_name(p) == 'orm_keep.vv' { |
| 67 | return |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | ref << p |
| 72 | } |
| 73 | }) |
| 74 | return files |
| 75 | } |
| 76 | |
| 77 | fn test_fmt() { |
| 78 | mut input_files := get_test_files(os.join_path(vroot, 'vlib', 'v', 'fmt', 'tests')) |
| 79 | run_fmt(mut input_files) |
| 80 | } |
| 81 | |
| 82 | fn test_fmt_vmodules() { |
| 83 | vmodules_tdir := os.join_path(vroot, 'vlib', 'v', 'fmt', 'testdata', 'vmodules') |
| 84 | os.setenv('VMODULES', vmodules_tdir, true) |
| 85 | mut input_files := get_test_files(vmodules_tdir) |
| 86 | run_fmt(mut input_files) |
| 87 | } |
| 88 | |