| 1 | // vtest build: !sanitized_job? |
| 2 | import os |
| 3 | import term |
| 4 | import benchmark |
| 5 | import v.ast |
| 6 | import v.fmt |
| 7 | import v.parser |
| 8 | import v.pref |
| 9 | import v.util.diff |
| 10 | |
| 11 | const error_missing_vexe = 1 |
| 12 | const error_failed_tests = 2 |
| 13 | const fpref = &pref.Preferences{ |
| 14 | is_fmt: true |
| 15 | } |
| 16 | |
| 17 | fn test_vlib_fmt() { |
| 18 | $if !vfmt_everything ? { |
| 19 | return |
| 20 | } |
| 21 | fmt_message := "checking that all V source files are vfmt'ed" |
| 22 | eprintln(term.header(fmt_message, '-')) |
| 23 | vexe := os.getenv('VEXE') |
| 24 | if vexe.len == 0 || !os.exists(vexe) { |
| 25 | eprintln('VEXE must be set') |
| 26 | exit(error_missing_vexe) |
| 27 | } |
| 28 | vroot := os.dir(vexe) |
| 29 | tmpfolder := os.temp_dir() |
| 30 | mut fmt_bench := benchmark.new_benchmark() |
| 31 | os.chdir(vroot) or {} |
| 32 | input_files := os.walk_ext('vlib/v/', '.v').filter(!it.contains('/tests/')) |
| 33 | fmt_bench.set_total_expected_steps(input_files.len) |
| 34 | for istep, ipath in input_files { |
| 35 | fmt_bench.cstep = istep |
| 36 | fmt_bench.step() |
| 37 | ifilename := os.file_name(ipath) |
| 38 | opath := ipath |
| 39 | expected_ocontent := os.read_file(opath) or { |
| 40 | fmt_bench.fail() |
| 41 | eprintln(fmt_bench.step_message_fail('cannot read from ${opath}')) |
| 42 | continue |
| 43 | } |
| 44 | mut table := ast.new_table() |
| 45 | file_ast := parser.parse_file(ipath, mut table, .parse_comments, fpref) |
| 46 | result_ocontent := fmt.fmt(file_ast, mut table, fpref, false) |
| 47 | if expected_ocontent != result_ocontent { |
| 48 | fmt_bench.fail() |
| 49 | eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) |
| 50 | vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}') |
| 51 | os.write_file(vfmt_result_file, result_ocontent)! |
| 52 | println(diff.compare_files(opath, vfmt_result_file) or { err.msg() }) |
| 53 | continue |
| 54 | } |
| 55 | fmt_bench.ok() |
| 56 | eprintln(fmt_bench.step_message_ok('${ipath}')) |
| 57 | } |
| 58 | fmt_bench.stop() |
| 59 | eprintln(term.h_divider('-')) |
| 60 | eprintln(fmt_bench.total_message(fmt_message)) |
| 61 | if fmt_bench.nfail > 0 { |
| 62 | exit(error_failed_tests) |
| 63 | } |
| 64 | } |
| 65 | |