diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index 7d92350c5e..ea68a86da3 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -181,11 +181,11 @@ fn (foptions &FormatOptions) post_process_file(file, formatted_file_path string) return } if foptions.is_diff { - diff_cmd := find_working_diff_command() or { + diff_cmd := util.find_working_diff_command() or { eprintln('No working "diff" CLI command found.') return } - os.system('$diff_cmd --minimal --text --unified=2 --show-function-line="fn " "$file" "$formatted_file_path" ') + println(util.color_compare_files(diff_cmd, file, formatted_file_path)) return } fc := os.read_file(file) or { @@ -224,17 +224,6 @@ fn (foptions &FormatOptions) post_process_file(file, formatted_file_path string) print(formatted_fc) } -fn find_working_diff_command() ?string { - for diffcmd in ['colordiff', 'diff', 'colordiff.exe', 'diff.exe'] { - p := os.exec('$diffcmd --version') or { - continue - } - if p.exit_code == 0 { - return diffcmd - } - } - return error('no working diff command found') -} fn (f FormatOptions) str() string { return 'FormatOptions{ is_l: $f.is_l' + ' is_w: $f.is_w' + ' is_diff: $f.is_diff' + ' is_verbose: $f.is_verbose' + diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index bdc0aecaf5..6d660d90ab 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -73,7 +73,7 @@ pub fn (mut f Fmt) write(s string) { f.out.write(tabs[f.indent]) } else { // too many indents, do it the slow way: - for i in 0 .. f.indent { + for _ in 0 .. f.indent { f.out.write('\t') } } diff --git a/vlib/v/fmt/fmt_keep_test.v b/vlib/v/fmt/fmt_keep_test.v index 9da65183ad..2ae0b3d445 100644 --- a/vlib/v/fmt/fmt_keep_test.v +++ b/vlib/v/fmt/fmt_keep_test.v @@ -6,6 +6,7 @@ import v.fmt import v.parser import v.table import v.pref +import v.util const ( error_missing_vexe = 1 @@ -22,9 +23,7 @@ fn test_fmt() { } vroot := os.dir(vexe) tmpfolder := os.temp_dir() - diff_cmd := 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') @@ -56,7 +55,7 @@ fn test_fmt() { } vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}') os.write_file(vfmt_result_file, result_ocontent) - os.system('$diff_cmd --minimal --text --unified=2 --show-function-line="fn " "$opath" "$vfmt_result_file"') + eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file)) continue } fmt_bench.ok() @@ -69,15 +68,3 @@ fn test_fmt() { exit(error_failed_tests) } } - -fn find_working_diff_command() ?string { - for diffcmd in ['colordiff', 'diff', 'colordiff.exe', 'diff.exe'] { - p := os.exec('$diffcmd --version') or { - continue - } - if p.exit_code == 0 { - return diffcmd - } - } - return error('no working diff command found') -} diff --git a/vlib/v/fmt/fmt_test.v b/vlib/v/fmt/fmt_test.v index 196e46dd53..c34c1f4fa9 100644 --- a/vlib/v/fmt/fmt_test.v +++ b/vlib/v/fmt/fmt_test.v @@ -6,6 +6,7 @@ import v.fmt import v.parser import v.table import v.pref +import v.util const ( error_missing_vexe = 1 @@ -22,9 +23,7 @@ fn test_fmt() { } vroot := os.dir(vexe) tmpfolder := os.temp_dir() - diff_cmd := find_working_diff_command() or { - '' - } + diff_cmd := util.find_working_diff_command() or { '' } 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') @@ -58,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) - os.system('$diff_cmd --minimal --text --unified=2 --show-function-line="fn " "$opath" "$vfmt_result_file"') + eprintln(util.color_compare_files(diff_cmd, opath, vfmt_result_file)) continue } fmt_bench.ok() @@ -71,15 +70,3 @@ fn test_fmt() { exit(error_failed_tests) } } - -fn find_working_diff_command() ?string { - for diffcmd in ['colordiff', 'diff', 'colordiff.exe', 'diff.exe'] { - p := os.exec('$diffcmd --version') or { - continue - } - if p.exit_code == 0 { - return diffcmd - } - } - return error('no working diff command found') -} diff --git a/vlib/v/tests/repl/runner/runner.v b/vlib/v/tests/repl/runner/runner.v index 01ae3e5f44..70499840e3 100644 --- a/vlib/v/tests/repl/runner/runner.v +++ b/vlib/v/tests/repl/runner/runner.v @@ -1,6 +1,7 @@ module runner import os +import v.util pub struct RunnerOptions { pub: @@ -33,18 +34,9 @@ pub fn full_path_to_v(dirs_in int) string { return vexec } -fn find_working_diff_command() ?string { - for diffcmd in ['colordiff', 'diff', 'colordiff.exe', 'diff.exe'] { - p := os.exec('$diffcmd --version') or { continue } - if p.exit_code == 0 { return diffcmd } - } - return error('no working diff command found') -} - fn diff_files( file_result, file_expected string ) string { - diffcmd := find_working_diff_command() or { return err } - diff := os.exec('$diffcmd --minimal --text --unified=2 ${file_result} ${file_expected}') or { return 'found diff command "$diffcmd" does not work' } - return diff.output + diffcmd := util.find_working_diff_command() or { return err } + return util.color_compare_files(diffcmd, file_result, file_expected) } pub fn run_repl_file(wd string, vexec string, file string) ?string { diff --git a/vlib/v/util/errors.v b/vlib/v/util/errors.v index f27086526b..3f436997b5 100644 --- a/vlib/v/util/errors.v +++ b/vlib/v/util/errors.v @@ -156,3 +156,27 @@ pub fn verror(kind, s string) { } exit(1) } + +pub fn find_working_diff_command() ?string { + for diffcmd in ['colordiff', 'diff', 'colordiff.exe', 'diff.exe'] { + p := os.exec('$diffcmd --version') or { + continue + } + if p.exit_code == 0 { + return diffcmd + } + } + return error('no working diff command found') +} + +pub fn color_compare_files(diff_cmd, file1, file2 string) string { + if diff_cmd != '' { + full_cmd := '$diff_cmd --minimal --text --unified=2 ' + + ' --show-function-line="fn " "$file1" "$file2" ' + x := os.exec(full_cmd) or { + return 'comparison command: `${full_cmd}` failed' + } + return x.output + } + return '' +}