2019-09-15 18:08:16 +03:00
|
|
|
module runner
|
|
|
|
|
2020-03-08 00:26:26 +03:00
|
|
|
import os
|
2019-09-15 18:08:16 +03:00
|
|
|
|
2020-01-20 19:06:36 +03:00
|
|
|
pub struct RunnerOptions {
|
2019-09-15 18:08:16 +03:00
|
|
|
pub:
|
|
|
|
wd string
|
|
|
|
vexec string
|
|
|
|
files []string
|
|
|
|
}
|
|
|
|
|
2019-11-09 19:35:26 +03:00
|
|
|
pub fn full_path_to_v(dirs_in int) string {
|
2019-11-28 11:46:52 +03:00
|
|
|
vexe_from_env := os.getenv('VEXE')
|
|
|
|
if vexe_from_env.len > 0 {
|
|
|
|
return vexe_from_env
|
|
|
|
}
|
2019-11-09 19:35:26 +03:00
|
|
|
vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
|
|
|
|
mut path := os.executable()
|
|
|
|
for i := 0; i < dirs_in; i++ {
|
2020-03-08 00:26:26 +03:00
|
|
|
path = os.dir(path)
|
2019-11-09 19:35:26 +03:00
|
|
|
}
|
2020-03-09 04:23:34 +03:00
|
|
|
vexec := os.join_path(path, vname)
|
2019-09-15 18:08:16 +03:00
|
|
|
/*
|
|
|
|
args := os.args
|
|
|
|
vreal := os.realpath('v')
|
|
|
|
myself := os.realpath( os.executable() )
|
2020-01-20 19:06:36 +03:00
|
|
|
wd := os.getwd()
|
2019-09-15 18:08:16 +03:00
|
|
|
println('args are: $args')
|
|
|
|
println('vreal : $vreal')
|
|
|
|
println('myself : $myself')
|
|
|
|
println('wd : $wd')
|
2020-02-17 22:31:23 +03:00
|
|
|
*/
|
2019-09-15 18:08:16 +03:00
|
|
|
return vexec
|
|
|
|
}
|
|
|
|
|
2019-09-25 22:00:56 +03:00
|
|
|
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 }
|
2020-01-21 01:04:26 +03:00
|
|
|
diff := os.exec('$diffcmd --minimal --text --unified=2 ${file_result} ${file_expected}') or { return 'found diff command "$diffcmd" does not work' }
|
2019-09-25 22:00:56 +03:00
|
|
|
return diff.output
|
|
|
|
}
|
|
|
|
|
2019-09-23 23:17:06 +03:00
|
|
|
pub fn run_repl_file(wd string, vexec string, file string) ?string {
|
2020-03-08 00:26:26 +03:00
|
|
|
vexec_folder := os.dir(vexec) + os.path_separator
|
2020-01-21 01:04:26 +03:00
|
|
|
fcontent := os.read_file(file) or { return error('Could not read file ${file}') }
|
2020-02-17 22:31:23 +03:00
|
|
|
content := fcontent.replace('\r', '')
|
2019-09-15 18:08:16 +03:00
|
|
|
input := content.all_before('===output===\n')
|
|
|
|
output := content.all_after('===output===\n')
|
2020-01-20 19:06:36 +03:00
|
|
|
|
2020-03-08 00:26:26 +03:00
|
|
|
fname := os.filename( file )
|
2020-01-21 01:04:26 +03:00
|
|
|
|
2020-03-09 04:23:34 +03:00
|
|
|
input_temporary_filename := os.realpath(os.join_path( wd, 'input_temporary_filename.txt'))
|
2019-09-15 18:08:16 +03:00
|
|
|
os.write_file(input_temporary_filename, input)
|
2020-03-09 04:23:34 +03:00
|
|
|
os.write_file( os.realpath(os.join_path( wd, 'original.txt' ) ), fcontent )
|
2020-01-20 19:06:36 +03:00
|
|
|
rcmd := '"$vexec" repl -replfolder "$wd" -replprefix "${fname}." < $input_temporary_filename'
|
2020-01-17 16:09:26 +03:00
|
|
|
r := os.exec(rcmd) or {
|
2019-09-15 18:08:16 +03:00
|
|
|
os.rm(input_temporary_filename)
|
2020-01-17 16:09:26 +03:00
|
|
|
return error('Could not execute: $rcmd')
|
2019-09-15 18:08:16 +03:00
|
|
|
}
|
|
|
|
os.rm(input_temporary_filename)
|
2020-02-17 22:31:23 +03:00
|
|
|
|
2020-01-20 19:06:36 +03:00
|
|
|
result := r.output.replace('\r','')
|
|
|
|
.replace('>>> ', '')
|
|
|
|
.replace('>>>', '')
|
|
|
|
.replace('... ', '')
|
|
|
|
.all_after('Use Ctrl-C or `exit` to exit\n')
|
2020-03-08 00:26:26 +03:00
|
|
|
.replace(wd + os.path_separator, '' )
|
2020-02-21 00:59:47 +03:00
|
|
|
.replace(vexec_folder, '')
|
|
|
|
.replace('\\', '/')
|
2019-09-15 18:08:16 +03:00
|
|
|
|
|
|
|
if result != output {
|
2019-09-25 22:00:56 +03:00
|
|
|
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 )
|
2020-01-21 01:04:26 +03:00
|
|
|
return error('Difference found in REPL file: ${file}
|
2019-09-15 18:08:16 +03:00
|
|
|
====> Got :
|
|
|
|
|$result|
|
|
|
|
====> Expected :
|
|
|
|
|$output|
|
2019-09-25 22:00:56 +03:00
|
|
|
====> Diff :
|
|
|
|
$diff
|
2019-09-15 18:08:16 +03:00
|
|
|
')
|
|
|
|
} else {
|
2020-01-21 01:04:26 +03:00
|
|
|
return 'Repl file ${file} is OK'
|
2019-09-15 18:08:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 19:35:26 +03:00
|
|
|
pub fn run_prod_file(wd string, vexec string, file string) ?string {
|
|
|
|
file_expected := '${file}.expected.txt'
|
2020-01-21 01:04:26 +03:00
|
|
|
f_expected_content := os.read_file(file_expected) or { return error('Could not read file ${file}') }
|
2020-02-17 22:31:23 +03:00
|
|
|
expected_content := f_expected_content.replace('\r', '')
|
2019-11-09 19:35:26 +03:00
|
|
|
|
2020-01-21 01:04:26 +03:00
|
|
|
cmd := '"$vexec" -prod run "${file}"'
|
2019-11-09 19:35:26 +03:00
|
|
|
r := os.exec(cmd) or {
|
|
|
|
return error('Could not execute: $cmd')
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.exit_code != 0 {
|
|
|
|
return error('$cmd return exit code: $r.exit_code')
|
|
|
|
}
|
|
|
|
|
|
|
|
result := r.output.replace('\r','')
|
|
|
|
|
|
|
|
if result != expected_content {
|
|
|
|
file_result := '${file}.result.txt'
|
|
|
|
os.write_file( file_result, result )
|
|
|
|
diff := diff_files( file_result, file_expected )
|
2020-01-21 01:04:26 +03:00
|
|
|
return error('Difference found in test: ${file}
|
2019-11-09 19:35:26 +03:00
|
|
|
====> Got :
|
|
|
|
|$result|
|
|
|
|
====> Expected :
|
|
|
|
|$expected_content|
|
|
|
|
====> Diff :
|
|
|
|
$diff
|
|
|
|
')
|
|
|
|
} else {
|
2020-01-21 01:04:26 +03:00
|
|
|
return 'Prod file ${file} is OK'
|
2019-11-09 19:35:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-15 18:08:16 +03:00
|
|
|
pub fn new_options() RunnerOptions {
|
2019-11-09 19:35:26 +03:00
|
|
|
vexec := full_path_to_v(5)
|
2020-01-21 01:04:26 +03:00
|
|
|
mut wd := os.getwd()
|
2019-09-15 18:08:16 +03:00
|
|
|
mut files := []string
|
|
|
|
if os.args.len > 1 {
|
2019-10-27 10:03:15 +03:00
|
|
|
files = os.args[1..]
|
2019-09-15 18:08:16 +03:00
|
|
|
} else {
|
2020-03-08 00:26:26 +03:00
|
|
|
os.chdir( os.dir(vexec) )
|
2020-01-21 01:04:26 +03:00
|
|
|
wd = os.getwd()
|
2019-09-15 18:08:16 +03:00
|
|
|
files = os.walk_ext('.', '.repl')
|
|
|
|
}
|
|
|
|
return RunnerOptions {
|
|
|
|
wd: wd
|
|
|
|
vexec: vexec
|
|
|
|
files: files
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 19:35:26 +03:00
|
|
|
pub fn new_prod_options() RunnerOptions {
|
2020-01-20 19:06:36 +03:00
|
|
|
wd := os.getwd()
|
2019-11-09 19:35:26 +03:00
|
|
|
vexec := full_path_to_v(4)
|
|
|
|
mut files := []string
|
|
|
|
if os.args.len > 1 {
|
|
|
|
files = os.args[1..]
|
|
|
|
} else {
|
|
|
|
files = os.walk_ext(wd, '.prod.v')
|
|
|
|
}
|
|
|
|
return RunnerOptions {
|
|
|
|
wd: wd
|
|
|
|
vexec: vexec
|
|
|
|
files: files
|
|
|
|
}
|
|
|
|
}
|