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

repl: tests show difference (in color where available)

This commit is contained in:
Delyan Angelov 2019-09-25 22:00:56 +03:00 committed by Alexander Medvednikov
parent d4bae356ba
commit 3fbfc5fbd4
3 changed files with 24 additions and 0 deletions

View File

@ -1 +1,3 @@
run
*.repl.result.txt
*.repl.expected.txt

View File

@ -25,6 +25,20 @@ pub fn full_path_to_v() 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
}
pub fn run_repl_file(wd string, vexec string, file string) ?string {
fcontent := os.read_file(file) or { return error('Could not read file $file') }
content := fcontent.replace('\r', '')
@ -43,11 +57,18 @@ pub fn run_repl_file(wd string, vexec string, file string) ?string {
result := r.output.replace('\r','').replace('>>> ', '').replace('>>>', '').replace('... ', '').all_after('Use Ctrl-C or `exit` to exit\n').replace(wd, '' )
if result != output {
file_result := '${file}.result.txt'
file_expected := '${file}.expected.txt'
os.write_file( file_result, result )
os.write_file( file_expected, output )
diff := diff_files( file_result, file_expected )
return error('Difference found in REPL file: $file
====> Got :
|$result|
====> Expected :
|$output|
====> Diff :
$diff
')
} else {
return 'Repl file $file is OK'

1
examples/.gitignore vendored
View File

@ -8,3 +8,4 @@
/nbody
/spectral
/database/mysql
/hello_v_js