v2 / vlib / v / slow_tests / repl / runner / runner.v
148 lines · 139 sloc · 3.89 KB · 2332ecff4811b8c97dfda8e825170e9397962519
Raw
1module runner
2
3import os
4import v.util.diff
5
6const is_vautofix = os.getenv('VAUTOFIX') != ''
7
8pub struct RunnerOptions {
9pub:
10 wd string
11 vexec string
12 files []string
13}
14
15pub fn full_path_to_v(dirs_in int) string {
16 vexe_from_env := os.getenv('VEXE')
17 if vexe_from_env.len > 0 {
18 return vexe_from_env
19 }
20 vname := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
21 mut path := os.executable()
22 for i := 0; i < dirs_in; i++ {
23 path = os.dir(path)
24 }
25 vexec := os.join_path(path, vname)
26 /*
27 args := os.args
28 vreal := os.real_path('v')
29 myself := os.real_path( os.executable() )
30 wd := os.getwd()
31 println('args are: ${args}')
32 println('vreal : ${vreal}')
33 println('myself : ${myself}')
34 println('wd : ${wd}')
35 */
36 return vexec
37}
38
39pub fn run_repl_file(wd string, vexec string, file string) !string {
40 vexec_folder := os.dir(vexec) + os.path_separator
41 fcontent := os.read_file(file) or { return error('Could not read repl file ${file}') }
42 content := fcontent.replace('\r', '')
43 input := content.all_before('===output===\n')
44 output := content.all_after('===output===\n').trim_right('\n\r')
45 fname := os.file_name(file)
46 input_temporary_filename := os.real_path(os.join_path(wd, 'input_temporary_filename.txt'))
47 os.write_file(input_temporary_filename, input) or { panic(err) }
48 os.write_file(os.real_path(os.join_path(wd, 'original.txt')), fcontent) or { panic(err) }
49 os.cp_all('vlib/v/slow_tests/repl/tmpl', os.real_path(os.join_path(wd, 'tmpl')), true) or {
50 panic(err)
51 }
52 rcmd := '${os.quoted_path(vexec)} repl -replfolder ${os.quoted_path(wd)} -replprefix "${fname}." < ${os.quoted_path(input_temporary_filename)}'
53 r := os.execute(rcmd)
54 if r.exit_code != 0 {
55 os.rm(input_temporary_filename)!
56 return error('Could not execute: ${rcmd}')
57 }
58 result := r.output.replace_each(['\r', '', '>>> ', '', '>>>', '', '... ', '',
59 wd + os.path_separator, '', vexec_folder, '']).trim_right('\n\r')
60 $if windows {
61 dump(rcmd)
62 dump(r.output)
63 dump(result)
64 }
65 os.rm(input_temporary_filename)!
66 if result != output {
67 if is_vautofix {
68 new_content := input + '===output===\n' + r.output.trim_right('\n\r') + '\n'
69 os.write_file(file, new_content)!
70 eprintln('>>> fixed file: `${file}`, new_content.len: ${new_content.len}')
71 }
72 return diff_error(file, output, result)
73 }
74 return file.replace('./', '')
75}
76
77pub fn run_prod_file(wd string, vexec string, file string) !string {
78 file_expected := '${file}.expected.txt'
79 f_expected_content := os.read_file(file_expected) or {
80 return error('Could not read expected prod file ${file_expected}')
81 }
82 expected_content := f_expected_content.replace('\r', '')
83 cmd := '${os.quoted_path(vexec)} -prod run ${os.quoted_path(file)}'
84 r := os.execute(cmd)
85 if r.exit_code < 0 {
86 return error('Could not execute: ${cmd}')
87 }
88 if r.exit_code != 0 {
89 return error('${cmd} return exit code: ${r.exit_code}')
90 }
91 result := r.output.replace('\r', '')
92 if result != expected_content {
93 return diff_error(file, expected_content, result)
94 }
95 return 'Prod file ${file} is OK'
96}
97
98pub fn new_options() RunnerOptions {
99 vexec := full_path_to_v(5)
100 mut wd := os.getwd()
101 mut files := []string{}
102 if os.args.len > 1 {
103 files = os.args[1..].clone()
104 } else {
105 os.chdir(os.dir(vexec)) or {}
106 wd = os.getwd()
107 files = os.walk_ext('.', '.repl')
108 }
109 return RunnerOptions{
110 wd: wd
111 vexec: vexec
112 files: files
113 }
114}
115
116pub fn new_prod_options() RunnerOptions {
117 wd := os.getwd()
118 vexec := full_path_to_v(4)
119 mut files := []string{}
120 if os.args.len > 1 {
121 files = os.args[1..].clone()
122 } else {
123 files = os.walk_ext(wd, '.prod.v')
124 }
125 return RunnerOptions{
126 wd: wd
127 vexec: vexec
128 files: files
129 }
130}
131
132fn diff_error(file string, expected string, found string) IError {
133 header := 'Difference found in REPL file: ${file}'
134 details := if diff_ := diff.compare_text(expected, found) {
135 '
136====> Diff :
137${diff_}
138'
139 } else {
140 '
141====> Expected :
142|${expected}|
143====> Got :
144|${found}|
145'
146 }
147 return error(header + details)
148}
149