1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: extract v.util.find_working_diff_command, color_compare_files

This commit is contained in:
Delyan Angelov 2020-04-29 11:50:33 +03:00
parent dd3434598c
commit 0e765e34be
6 changed files with 36 additions and 57 deletions

View File

@ -181,11 +181,11 @@ fn (foptions &FormatOptions) post_process_file(file, formatted_file_path string)
return return
} }
if foptions.is_diff { 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.') eprintln('No working "diff" CLI command found.')
return 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 return
} }
fc := os.read_file(file) or { fc := os.read_file(file) or {
@ -224,17 +224,6 @@ fn (foptions &FormatOptions) post_process_file(file, formatted_file_path string)
print(formatted_fc) 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 { 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' + return 'FormatOptions{ is_l: $f.is_l' + ' is_w: $f.is_w' + ' is_diff: $f.is_diff' + ' is_verbose: $f.is_verbose' +

View File

@ -73,7 +73,7 @@ pub fn (mut f Fmt) write(s string) {
f.out.write(tabs[f.indent]) f.out.write(tabs[f.indent])
} else { } else {
// too many indents, do it the slow way: // too many indents, do it the slow way:
for i in 0 .. f.indent { for _ in 0 .. f.indent {
f.out.write('\t') f.out.write('\t')
} }
} }

View File

@ -6,6 +6,7 @@ import v.fmt
import v.parser import v.parser
import v.table import v.table
import v.pref import v.pref
import v.util
const ( const (
error_missing_vexe = 1 error_missing_vexe = 1
@ -22,9 +23,7 @@ fn test_fmt() {
} }
vroot := os.dir(vexe) vroot := os.dir(vexe)
tmpfolder := os.temp_dir() 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() mut fmt_bench := benchmark.new_benchmark()
keep_input_files := os.walk_ext('$vroot/vlib/v/fmt/tests', '_keep.vv') 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') 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}') vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}')
os.write_file(vfmt_result_file, result_ocontent) 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 continue
} }
fmt_bench.ok() fmt_bench.ok()
@ -69,15 +68,3 @@ fn test_fmt() {
exit(error_failed_tests) 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')
}

View File

@ -6,6 +6,7 @@ import v.fmt
import v.parser import v.parser
import v.table import v.table
import v.pref import v.pref
import v.util
const ( const (
error_missing_vexe = 1 error_missing_vexe = 1
@ -22,9 +23,7 @@ fn test_fmt() {
} }
vroot := os.dir(vexe) vroot := os.dir(vexe)
tmpfolder := os.temp_dir() 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() mut fmt_bench := benchmark.new_benchmark()
// Lookup the existing test _input.vv files: // Lookup the existing test _input.vv files:
input_files := os.walk_ext('$vroot/vlib/v/fmt/tests', '_input.vv') 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}') vfmt_result_file := os.join_path(tmpfolder, 'vfmt_run_over_${ifilename}')
os.write_file(vfmt_result_file, result_ocontent) 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 continue
} }
fmt_bench.ok() fmt_bench.ok()
@ -71,15 +70,3 @@ fn test_fmt() {
exit(error_failed_tests) 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')
}

View File

@ -1,6 +1,7 @@
module runner module runner
import os import os
import v.util
pub struct RunnerOptions { pub struct RunnerOptions {
pub: pub:
@ -33,18 +34,9 @@ pub fn full_path_to_v(dirs_in int) string {
return vexec 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 { fn diff_files( file_result, file_expected string ) string {
diffcmd := find_working_diff_command() or { return err } diffcmd := util.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 util.color_compare_files(diffcmd, file_result, file_expected)
return diff.output
} }
pub fn run_repl_file(wd string, vexec string, file string) ?string { pub fn run_repl_file(wd string, vexec string, file string) ?string {

View File

@ -156,3 +156,27 @@ pub fn verror(kind, s string) {
} }
exit(1) 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 ''
}