From 57ea9bec3086d60066891c5828c50c409c642685 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 18 May 2020 18:43:04 +0200 Subject: [PATCH] tests: vfmt test for all vlib/v files --- vlib/v/fmt/fmt_keep_test.v | 6 ++-- vlib/v/fmt/fmt_vlib_test.v | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/fmt_vlib_test.v diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index 16eb073a03..57f6dada68 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -23,7 +23,9 @@ fn test_fmt() { } vroot := os.dir(vexe) tmpfolder := os.temp_dir() - diff_cmd := util.find_working_diff_command() or { '' } + diff_cmd := util.find_working_diff_command() or { + '' + } mut fmt_bench := benchmark.new_benchmark() keep_input_files := os.walk_ext('$vroot/vlib/v/fmt/tests', '_keep.vv') expected_input_files := os.walk_ext('$vroot/vlib/v/fmt/tests', '_expected.vv') @@ -55,7 +57,7 @@ fn test_fmt() { } vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}') os.write_file(vfmt_result_file, result_ocontent) - eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file)) + eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file)) continue } fmt_bench.ok() diff --git a/vlib/v/fmt/fmt_vlib_test.v b/vlib/v/fmt/fmt_vlib_test.v new file mode 100644 index 0000000000..5610da8faf --- /dev/null +++ b/vlib/v/fmt/fmt_vlib_test.v @@ -0,0 +1,71 @@ +import os +import term +import benchmark +import v.ast +import v.fmt +import v.parser +import v.table +import v.pref +import v.util + +const ( + error_missing_vexe = 1 + error_failed_tests = 2 +) + +fn test_vlib_fmt() { + $if !freebsd { + return + } + fmt_message := "checking that all V source files are vfmt'ed" + eprintln(term.header(fmt_message, '-')) + vexe := os.getenv('VEXE') + if vexe.len == 0 || !os.exists(vexe) { + eprintln('VEXE must be set') + exit(error_missing_vexe) + } + vroot := os.dir(vexe) + tmpfolder := os.temp_dir() + diff_cmd := util.find_working_diff_command() or { + '' + } + mut fmt_bench := benchmark.new_benchmark() + input_files := os.walk_ext('$vroot/vlib/v/', '.v') + fmt_bench.set_total_expected_steps(input_files.len) + for istep, ipath in input_files { + fmt_bench.cstep = istep + fmt_bench.step() + ifilename := os.file_name(ipath) + opath := ipath + expected_ocontent := os.read_file(opath) or { + fmt_bench.fail() + eprintln(fmt_bench.step_message_fail('cannot read from ${opath}')) + continue + } + table := table.new_table() + file_ast := parser.parse_file(ipath, table, .parse_comments, &pref.Preferences{}, &ast.Scope{ + parent: 0 + }) + result_ocontent := fmt.fmt(file_ast, table, false) + if expected_ocontent != result_ocontent { + fmt_bench.fail() + eprintln(fmt_bench.step_message_fail('file ${ipath} after formatting, does not look as expected.')) + if diff_cmd == '' { + eprintln('>> sorry, but no working "diff" CLI command can be found') + continue + } + vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}') + os.write_file(vfmt_result_file, result_ocontent) + eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file)) + continue + } + fmt_bench.ok() + eprintln(fmt_bench.step_message_ok('${ipath}')) + } + fmt_bench.stop() + eprintln(term.h_divider('-')) + eprintln(fmt_bench.total_message(fmt_message)) + if fmt_bench.nfail > 0 { + exit(error_failed_tests) + } +}