From 365bd18542f981f4418e155f42f6f9528df1a283 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Tue, 12 Mar 2024 16:11:27 +0100 Subject: [PATCH] fmt: update tests to run all fmt also in a vmodules context (#20995) --- .gitignore | 7 +- vlib/v/fmt/fmt_keep_test.v | 72 +++++++++++-------- vlib/v/fmt/fmt_test.v | 43 ++++++----- vlib/v/fmt/fmt_vmodules_test.v | 55 -------------- .../vmodules/submod_type_alias/bar/baz/baz.v | 1 + .../submod_type_alias_keep.vv | 7 ++ vlib/v/util/vtest/vtest.v | 5 +- 7 files changed, 82 insertions(+), 108 deletions(-) delete mode 100644 vlib/v/fmt/fmt_vmodules_test.v create mode 100644 vlib/v/fmt/testdata/vmodules/submod_type_alias/bar/baz/baz.v create mode 100644 vlib/v/fmt/testdata/vmodules/submod_type_alias/submod_type_alias_keep.vv diff --git a/.gitignore b/.gitignore index d56b2f8c2a..e813bf8abc 100644 --- a/.gitignore +++ b/.gitignore @@ -39,9 +39,10 @@ a.out fns.txt .noprefix.vrepl_temp.v -# ignore temp directories -/temp -/tmp +# ignore temp and cache directories +temp/ +tmp/ +cache/ # unignore special files without extension !.github/PULL_REQUEST_TEMPLATE diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index a82875e1eb..d574e34649 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -11,43 +11,32 @@ import v.pref import v.util.diff import v.util.vtest -const error_missing_vexe = 1 -const error_failed_tests = 2 +const vroot = @VEXEROOT const fpref = &pref.Preferences{ is_fmt: true } -const vexe = os.getenv('VEXE') -fn test_fmt() { +fn run_fmt(mut input_files []string) { fmt_message := 'checking that v fmt keeps already formatted files *unchanged*' eprintln(term.header(fmt_message, '-')) - if vexe.len == 0 || !os.exists(vexe) { - eprintln('VEXE must be set') - exit(error_missing_vexe) + assert input_files.len > 0 + input_files = vtest.filter_vtest_only(input_files) + if input_files.len == 0 { + // No need to produce a failing test here. + eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY')) + exit(0) } - vroot := os.dir(vexe) - os.chdir(vroot) or {} - basepath := vroot + '/' + input_files.sort() + mut fmt_bench := benchmark.new_benchmark() + fmt_bench.set_total_expected_steps(input_files.len + 1) tmpfolder := os.temp_dir() diff_cmd := diff.find_working_diff_command() or { '' } - mut fmt_bench := benchmark.new_benchmark() - keep_input_files := os.walk_ext('vlib/v/fmt/tests', '_keep.vv') - expected_input_files := os.walk_ext('vlib/v/fmt/tests', '_expected.vv') - mut input_files := []string{} - input_files << keep_input_files - input_files << expected_input_files - input_files = vtest.filter_vtest_only(input_files, basepath: vroot) - input_files.sort() - fmt_bench.set_total_expected_steps(input_files.len + 1) for istep, ipath in input_files { fmt_bench.cstep = istep + 1 fmt_bench.step() - ifilename := os.file_name(ipath) - vrelpath := ipath.replace(basepath, '') - opath := ipath - expected_ocontent := os.read_file(opath) or { + expected_ocontent := os.read_file(ipath) or { fmt_bench.fail() - eprintln(fmt_bench.step_message_fail('cannot read from ${vrelpath}')) + eprintln(fmt_bench.step_message_fail('cannot read from ${ipath}')) continue } mut table := ast.new_table() @@ -55,23 +44,44 @@ fn test_fmt() { result_ocontent := fmt.fmt(file_ast, mut table, fpref, false) if expected_ocontent != result_ocontent { fmt_bench.fail() - eprintln(fmt_bench.step_message_fail('file ${vrelpath} after formatting, does not look as expected.')) + 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}') + vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}') os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } - eprintln(diff.color_compare_files(diff_cmd, opath, vfmt_result_file)) + eprintln(diff.color_compare_files(diff_cmd, ipath, vfmt_result_file)) continue } fmt_bench.ok() - eprintln(fmt_bench.step_message_ok(vrelpath)) + 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) - } + assert fmt_bench.nfail == 0 +} + +fn get_test_files(path string) []string { + mut files := []string{} + mut ref := &files + os.walk(path, fn [mut ref] (p string) { + if p.ends_with('_keep.vv') || p.ends_with('_expected.vv') { + ref << p + } + }) + return files +} + +fn test_fmt() { + mut input_files := get_test_files(os.join_path(vroot, 'vlib', 'v', 'fmt', 'tests')) + run_fmt(mut input_files) +} + +fn test_fmt_vmodules() { + vmodules_tdir := os.join_path(vroot, 'vlib', 'v', 'fmt', 'testdata', 'vmodules') + os.setenv('VMODULES', vmodules_tdir, true) + mut input_files := get_test_files(vmodules_tdir) + run_fmt(mut input_files) } diff --git a/vlib/v/fmt/fmt_test.v b/vlib/v/fmt/fmt_test.v index dfb4a33f0d..2ecec70843 100644 --- a/vlib/v/fmt/fmt_test.v +++ b/vlib/v/fmt/fmt_test.v @@ -9,32 +9,30 @@ import v.fmt import v.parser import v.pref import v.util.diff +import v.util.vtest -const error_missing_vexe = 1 -const error_failed_tests = 2 +const vroot = @VEXEROOT const fpref = &pref.Preferences{ is_fmt: true } -fn test_fmt() { +fn run_fmt(mut input_files []string) { fmt_message := 'vfmt tests' 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 := diff.find_working_diff_command() or { '' } + assert input_files.len > 0 + input_files = vtest.filter_vtest_only(input_files) + if input_files.len == 0 { + // No need to produce a failing test here. + eprintln('no tests found with VTEST_ONLY filter set to: ' + os.getenv('VTEST_ONLY')) + exit(0) + } mut fmt_bench := benchmark.new_benchmark() - // Lookup the existing test _input.vv files: - input_files := os.walk_ext('${vroot}/vlib/v/fmt/tests', '_input.vv') 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.replace('_input.vv', '_expected.vv') if !os.exists(opath) { fmt_bench.fail() @@ -56,18 +54,29 @@ fn test_fmt() { eprintln('>> sorry, but no working "diff" CLI command can be found') continue } - vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}') + vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${os.file_name(ipath)}') os.write_file(vfmt_result_file, result_ocontent) or { panic(err) } eprintln(diff.color_compare_files(diff_cmd, opath, vfmt_result_file)) continue } fmt_bench.ok() - eprintln(fmt_bench.step_message_ok('${ipath}')) + 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) - } + assert fmt_bench.nfail == 0 } + +fn test_fmt() { + mut input_files := os.walk_ext(os.join_path(vroot, 'vlib', 'v', 'fmt', 'tests'), '_input.vv') + run_fmt(mut input_files) +} + +// NOTE: enable with upcommming `__input.vv` / `__expected.vv` files for vmodules +/* fn test_fmt_vmodules() { + vmodules_tdir := os.join_path(vroot, 'vlib', 'v', 'fmt', 'testdata', 'vmodules') + os.setenv('VMODULES', vmodules_tdir, true) + mut input_files := os.walk_ext(vmodules_tdir, '_input.vv') + run_fmt(mut input_files) +} */ diff --git a/vlib/v/fmt/fmt_vmodules_test.v b/vlib/v/fmt/fmt_vmodules_test.v deleted file mode 100644 index cc626f344f..0000000000 --- a/vlib/v/fmt/fmt_vmodules_test.v +++ /dev/null @@ -1,55 +0,0 @@ -import os - -const vexe = os.quoted_path(@VEXE) -const vmodules_tdir = os.join_path(os.vtmp_dir(), 'fmt_vmodules_test') - -fn testsuite_begin() { - os.mkdir_all(vmodules_tdir) or {} - os.setenv('VMODULES', vmodules_tdir, true) -} - -fn testsuite_end() { - os.rmdir_all(vmodules_tdir) or {} -} - -fn test_fmt_imports() { - mod_tdir := os.join_path(vmodules_tdir, @FN) - os.mkdir_all(mod_tdir)! - tfile_content := [ - 'import x.json2 as json', - 'import datatypes { Stack }', - '', - 'const foo = Stack[string]{}', - '', - ].join_lines() - os.write_file(os.join_path(mod_tdir, 'main.v'), tfile_content)! - os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() } -} - -fn test_fmt_submod_type_alias() { - mod_tdir := os.join_path(vmodules_tdir, @FN) - mod_src_tdir := os.join_path(mod_tdir, 'src') - submod_tdir := os.join_path(mod_tdir, 'bar', 'baz') - os.mkdir_all(mod_src_tdir)! - os.mkdir_all(submod_tdir)! - tfile_content := [ - 'module ${@FN}', - '', - 'import bar.baz', - '', - 'type MyAlias = baz.Baz', - '', - ].join_lines() - submod_tfile_content := [ - 'module baz', - '', - 'enum BarBaz {', - ' bar', - ' baz', - '}', - '', - ].join_lines() - os.write_file(os.join_path(mod_src_tdir, 'foo.v'), tfile_content)! - os.write_file(os.join_path(submod_tdir, 'baz.v'), submod_tfile_content)! - os.execute_opt('${vexe} fmt -c ${mod_tdir}') or { assert false, err.msg() } -} diff --git a/vlib/v/fmt/testdata/vmodules/submod_type_alias/bar/baz/baz.v b/vlib/v/fmt/testdata/vmodules/submod_type_alias/bar/baz/baz.v new file mode 100644 index 0000000000..053e98a7a7 --- /dev/null +++ b/vlib/v/fmt/testdata/vmodules/submod_type_alias/bar/baz/baz.v @@ -0,0 +1 @@ +module baz diff --git a/vlib/v/fmt/testdata/vmodules/submod_type_alias/submod_type_alias_keep.vv b/vlib/v/fmt/testdata/vmodules/submod_type_alias/submod_type_alias_keep.vv new file mode 100644 index 0000000000..2f357f3f5e --- /dev/null +++ b/vlib/v/fmt/testdata/vmodules/submod_type_alias/submod_type_alias_keep.vv @@ -0,0 +1,7 @@ +// The module name has to match the modules dir name. +module submod_type_alias + +// The submod dir structure that is imported has to be existent. +import bar.baz + +type MyAlias = baz.Baz diff --git a/vlib/v/util/vtest/vtest.v b/vlib/v/util/vtest/vtest.v index e89b57bcf3..80da335db1 100644 --- a/vlib/v/util/vtest/vtest.v +++ b/vlib/v/util/vtest/vtest.v @@ -2,6 +2,7 @@ module vtest import os +@[params] pub struct FilterVTestConfig { basepath string fix_slashes bool = true @@ -13,13 +14,13 @@ pub fn filter_vtest_only(paths []string, config FilterVTestConfig) []string { patterns := os.getenv('VTEST_ONLY').split(',') for relative_path in paths { mut file := relative_path - if config.basepath.len > 0 { + if config.basepath != '' { file = os.join_path_single(config.basepath, file) } if config.fix_slashes { file = file.replace('\\', '/') } - if patterns.len != 0 { + if patterns.len > 0 { mut found := 0 for okpat in patterns { if file.contains(okpat) {