From dbdbfe24a51e307b635a416ac144c27439a6a708 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Fri, 22 Mar 2024 07:50:57 +0100 Subject: [PATCH] vdoc,tests: detect expected files from input instead of hardcoding names (#21074) --- .../{main.comments.out => basic.comments.out} | 2 +- .../testdata/basic/{main.out => basic.out} | 2 +- .../tests/testdata/basic/{main.v => basic.v} | 2 + cmd/tools/vdoc/tests/vdoc_file_test.v | 78 ++++++------------- 4 files changed, 28 insertions(+), 56 deletions(-) rename cmd/tools/vdoc/tests/testdata/basic/{main.comments.out => basic.comments.out} (96%) rename cmd/tools/vdoc/tests/testdata/basic/{main.out => basic.out} (94%) rename cmd/tools/vdoc/tests/testdata/basic/{main.v => basic.v} (96%) diff --git a/cmd/tools/vdoc/tests/testdata/basic/main.comments.out b/cmd/tools/vdoc/tests/testdata/basic/basic.comments.out similarity index 96% rename from cmd/tools/vdoc/tests/testdata/basic/main.comments.out rename to cmd/tools/vdoc/tests/testdata/basic/basic.comments.out index cde033ffab..a87edf2794 100644 --- a/cmd/tools/vdoc/tests/testdata/basic/main.comments.out +++ b/cmd/tools/vdoc/tests/testdata/basic/basic.comments.out @@ -1,4 +1,4 @@ -module main +module basic const source_root = 'temp' // some const const another = int(5) diff --git a/cmd/tools/vdoc/tests/testdata/basic/main.out b/cmd/tools/vdoc/tests/testdata/basic/basic.out similarity index 94% rename from cmd/tools/vdoc/tests/testdata/basic/main.out rename to cmd/tools/vdoc/tests/testdata/basic/basic.out index dd6e4f4515..536c668246 100644 --- a/cmd/tools/vdoc/tests/testdata/basic/main.out +++ b/cmd/tools/vdoc/tests/testdata/basic/basic.out @@ -1,4 +1,4 @@ -module main +module basic const source_root = 'temp' // some const const another = int(5) diff --git a/cmd/tools/vdoc/tests/testdata/basic/main.v b/cmd/tools/vdoc/tests/testdata/basic/basic.v similarity index 96% rename from cmd/tools/vdoc/tests/testdata/basic/main.v rename to cmd/tools/vdoc/tests/testdata/basic/basic.v index 49b04f2d6d..26719ae6bf 100644 --- a/cmd/tools/vdoc/tests/testdata/basic/main.v +++ b/cmd/tools/vdoc/tests/testdata/basic/basic.v @@ -1,3 +1,5 @@ +module basic + pub const source_root = 'temp' // some const pub const another = int(5) diff --git a/cmd/tools/vdoc/tests/vdoc_file_test.v b/cmd/tools/vdoc/tests/vdoc_file_test.v index 23442fc2c0..c9093148ba 100644 --- a/cmd/tools/vdoc/tests/vdoc_file_test.v +++ b/cmd/tools/vdoc/tests/vdoc_file_test.v @@ -4,12 +4,10 @@ import term import v.util.vtest import v.util.diff -const vexe = os.quoted_path(@VEXE) - -const vroot = @VMODROOT - +const vexe_path = @VEXE +const vexe = os.quoted_path(vexe_path) +const vroot = os.dir(vexe_path) const diff_cmd = find_diff_cmd() - const should_autofix = os.getenv('VAUTOFIX') != '' fn find_diff_cmd() string { @@ -62,45 +60,22 @@ fn check_path(dir string, tests []string) int { for path in paths { mut fails := 0 qpath := os.quoted_path(path) + path_no_ext := path.all_before_last('.') print(path + ' ') - fails += check_output( - program: path - cmd: '${vexe} doc ${qpath}' - out_filename: 'main.out' - ) - fails += check_output( - program: path - cmd: '${vexe} doc -comments ${qpath}' - out_filename: 'main.unsorted.out' + fails += check_output('${vexe} doc ${qpath}', path_no_ext + '.out') + fails += check_output('${vexe} doc -comments ${qpath}', '${path_no_ext}.unsorted.out', should_sort: false ) - fails += check_output( - program: path - cmd: '${vexe} doc -comments ${qpath}' - out_filename: 'main.comments.out' - ) - fails += check_output( - program: path - cmd: '${vexe} doc -readme -comments ${qpath}' - out_filename: 'main.readme.comments.out' - ) + fails += check_output('${vexe} doc -comments ${qpath}', '${path_no_ext}.comments.out') + fails += check_output('${vexe} doc -readme -comments ${qpath}', '${path_no_ext}.readme.comments.out') // test the main 3 different formats: program_dir := os.quoted_path(if os.is_file(path) { os.dir(path) } else { path }) - fails += check_output( - program: path - cmd: '${vexe} doc -f html -o - -html-only-contents -readme -comments ${program_dir}' - out_filename: 'main.html' - ) - fails += check_output( - program: path - cmd: '${vexe} doc -f ansi -o - -html-only-contents -readme -comments ${program_dir}' - out_filename: 'main.ansi' - ) - fails += check_output( - program: path - cmd: '${vexe} doc -f text -o - -html-only-contents -readme -comments ${program_dir}' - out_filename: 'main.text' - ) + fails += check_output('${vexe} doc -f html -o - -html-only-contents -readme -comments ${program_dir}', + '${path_no_ext}.html') + fails += check_output('${vexe} doc -f ansi -o - -html-only-contents -readme -comments ${program_dir}', + '${path_no_ext}.ansi') + fails += check_output('${vexe} doc -f text -o - -html-only-contents -readme -comments ${program_dir}', + '${path_no_ext}.text') // total_fails += fails if fails == 0 { @@ -136,24 +111,19 @@ fn clean_line_endings(s string) string { @[params] struct CheckOutputParams { - program string = 'some/dir/main.v' - cmd string = 'v doc' - main_filename string = 'main.v' - out_filename string = 'main.out' - should_sort bool = true + should_sort bool = true } -fn check_output(params CheckOutputParams) int { - out_file_path := params.program.replace(params.main_filename, params.out_filename) - if !os.exists(out_file_path) { +fn check_output(cmd string, out_path string, opts CheckOutputParams) int { + if !os.exists(out_path) { return 0 } mut fails := 0 - mut expected := os.read_file(out_file_path) or { panic(err) } + mut expected := os.read_file(out_path) or { panic(err) } expected = clean_line_endings(expected) - os.setenv('VDOC_SORT', params.should_sort.str(), true) - res := os.execute(params.cmd) + os.setenv('VDOC_SORT', opts.should_sort.str(), true) + res := os.execute(cmd) if res.exit_code < 0 { panic(res.output) @@ -161,13 +131,13 @@ fn check_output(params CheckOutputParams) int { found := clean_line_endings(res.output) if expected != found { print_compare(expected, found) - eprintln('>>> cmd: VDOC_SORT=${params.should_sort} ${params.cmd}') - eprintln('>>> out_file_path: `${out_file_path}`') - eprintln('>>> fix: VDOC_SORT=${params.should_sort} ${params.cmd} > ${out_file_path}') + eprintln('>>> cmd: VDOC_SORT=${opts.should_sort} ${cmd}') + eprintln('>>> out_file_path: `${out_path}`') + eprintln('>>> fix: VDOC_SORT=${opts.should_sort} ${cmd} > ${out_path}') fails++ } if should_autofix { - os.write_file(out_file_path, res.output) or {} + os.write_file(out_path, res.output) or {} } return fails }