vile / cli_test.v
264 lines · 257 sloc · 10.78 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module main
2
3import os
4import scheme
5import time
6
7struct CliFormatCase {
8 args []string
9 output string
10 status int
11}
12
13fn test_cli_format_detection_and_explicit_syntax_flags() {
14 dir := os.join_path(os.temp_dir(), 'vile-cli-${os.getpid()}-${time.sys_mono_now()}')
15 os.mkdir_all(dir) or { panic(err) }
16 defer { os.rmdir_all(dir) or {} }
17 binary := os.join_path(dir, 'vile')
18 mut build := [@VEXE, '-cc', @CCOMPILER]
19 source_path := os.getenv('VILE_TEST_VPATH')
20 if source_path != '' {
21 build << ['-path', source_path]
22 }
23 build << ['-o', binary, @VMODROOT]
24 built := os.exec(build)
25 assert built.exit_code == 0, built.output
26 scheme_file := os.join_path(dir, 'program.scm')
27 c_file := os.join_path(dir, 'code with spaces.vile')
28 legacy_file := os.join_path(dir, 'legacy.vilec')
29 unknown_file := os.join_path(dir, 'program.txt')
30 forced_file := os.join_path(dir, 'forced.scm')
31 forced_scheme_file := os.join_path(dir, 'forced-scheme.vile')
32 inspect_file := os.join_path(dir, 'inspect.vile')
33 load_file := os.join_path(dir, 'load.scm')
34 settings_file := os.join_path(dir, 'settings.vile')
35 macro_file := os.join_path(dir, 'macro.scm')
36 os.write_file(settings_file, 'answer:=41') or { panic(err) }
37 os.write_file(macro_file, '(define-syntax increment (syntax-rules () ((_ x) (+ x 1))))') or { panic(err) }
38 os.write_file(load_file, '(load "settings.vile") (load "macro.scm") (display (increment answer)) (newline)') or { panic(err) }
39 parent := os.join_path(dir, 'parent.vile')
40 os.mkdir_all(parent) or { panic(err) }
41 nested_file := os.join_path(parent, 'nested.scm')
42 for file in [scheme_file, legacy_file, unknown_file, nested_file, forced_scheme_file] {
43 os.write_file(file, '(display (+ 20 22)) (newline)') or { panic(err) }
44 }
45 for file in [c_file, forced_file] {
46 os.write_file(file, 'display(20+22)\nnewline()') or { panic(err) }
47 }
48 os.write_file(inspect_file, 'display([1,2])') or { panic(err) }
49 for case in [
50 CliFormatCase{ args: [load_file], output: '42' },
51 CliFormatCase{ args: [scheme_file], output: '42' },
52 CliFormatCase{ args: [c_file], output: '42' },
53 CliFormatCase{ args: [legacy_file], output: '42' },
54 CliFormatCase{ args: [unknown_file], output: '42' },
55 CliFormatCase{ args: [nested_file], output: '42' },
56 CliFormatCase{ args: ['-v', forced_file], output: '42' },
57 CliFormatCase{ args: ['--vile', forced_file], output: '42' },
58 CliFormatCase{ args: ['-s', forced_scheme_file], output: '42' },
59 CliFormatCase{ args: ['--scheme', forced_scheme_file], output: '42' },
60 CliFormatCase{ args: ['-v', '-e', '3+2*4'], output: '11' },
61 CliFormatCase{ args: ['--vile', '-e', 'x:=20', '-e', 'x+22'], output: '42' },
62 CliFormatCase{
63 args: ['-v', '-e', 'x:=1\ny:=2\nfoobar:=#f', '-e', 'x if foobar {+} else {-} y',
64 '-e', 'foobar=#t', '-e', 'x if foobar {+} else {-} y']
65 output: '-1\n3'
66 },
67 CliFormatCase{ args: ['-v', '-e', 'a():={display(12),newline()}\na'], output: '12' },
68 CliFormatCase{ args: ['-s', '-e', '(define (a) 12) a'], output: '#<procedure>' },
69 CliFormatCase{ args: ['-s', '-e', '(+ 20 22)'], output: '42' },
70 CliFormatCase{
71 args: ['-s', '-e', "(define-class <stamp> ()) (slot-set! <stamp> 'print (lambda (obj out) (display 17 out))) (make <stamp>)"]
72 output: '17'
73 },
74 CliFormatCase{ args: ['--scheme', '-e', '(define x 20)', '-e', '(+ x 22)'], output: '42' },
75 CliFormatCase{ args: ['-s', '-e', '#!no-fold-case (define MiXeD 42)', '-e', 'MiXeD'], output: '42' },
76 CliFormatCase{ args: ['-v', '-e', '#!NO-FOLD-CASE MiXeD:=42', '-e', 'MiXeD'], output: '42' },
77 CliFormatCase{
78 args: ['-s', '-e', '#!no-fold-case (define MiXeD 42) (define mixed 7)', '-e',
79 '#!FOLD-CASE MiXeD']
80 output: '7'
81 },
82 CliFormatCase{ args: ['-e', '(+ 20 22)'], output: '42' },
83 CliFormatCase{ args: ['--read', inspect_file], output: '(display (vector 1 2))' },
84 CliFormatCase{ args: ['-v', '--read', forced_file], output: '(display (+ 20 22))\n(newline)' },
85 CliFormatCase{ args: ['-s', '--read', forced_scheme_file], output: '(display (+ 20 22))\n(newline)' },
86 ] {
87 mut command := [binary]
88 command << case.args
89 result := os.exec(command)
90 assert result.exit_code == case.status, '${case.args}: ${result.output}'
91 assert result.output.trim_space() == case.output, '${case.args}: ${result.output}'
92 }
93 tokens := os.exec([binary, '--tokens', inspect_file])
94 assert tokens.exit_code == 0, tokens.output
95 assert tokens.output.contains('\tlbracket\t['), tokens.output
96 assert tokens.output.contains('\trbracket\t]'), tokens.output
97 for args in [
98 ['--alt-syntax', '-e', '1'],
99 ['--vile-c', '-e', '1'],
100 ['-c', '-e', '1'],
101 [forced_file],
102 [forced_scheme_file],
103 ['-s', c_file],
104 ['-v', '-s', '-e', '1'],
105 ['--vile', '--scheme', '-e', '1'],
106 ['--scheme', '-v', '-e', '1'],
107 ] {
108 mut command := [binary]
109 command << args
110 result := os.exec(command)
111 assert result.exit_code != 0, '${args}: ${result.output}'
112 }
113 help := os.exec([binary, '--help'])
114 assert help.exit_code == 0, help.output
115 assert help.output.contains('--vile') && help.output.contains('--scheme'), help.output
116 assert !help.output.contains('--vile-c'), help.output
117 assert !help.output.contains('--alt-syntax'), help.output
118 assert !help.output.contains('.vilec'), help.output
119 cli_system_interfaces(binary, dir)
120 cli_shared_repl_input(binary, dir)
121 cli_recursion_limits(binary, dir)
122 version := os.exec([binary, '--version'])
123 assert version.exit_code == 0 && version.output.contains('0.0.0'), version.output
124}
125
126fn cli_shared_repl_input(binary string, dir string) {
127 input := os.join_path(dir, 'repl-input.scm')
128 os.write_file(input, '(read-char)\nx(+ 20\n22)\n') or { panic(err) }
129 mut child := os.new_process(binary)
130 defer { child.close() }
131 child.set_args(['-s'])
132 child.set_redirect_stdio_merged()
133 child.set_stdin_path(input)
134 child.run()
135 output := child.stdout_slurp()
136 child.wait()
137 assert child.code == 0, output
138 assert output.trim_space().split_into_lines() == [r'#\x', '42'], output
139}
140
141fn cli_recursion_limits(binary string, dir string) {
142 // Include both sides of the former CLI ceiling and a deeper call. The five
143 // extra arguments also exercise restoring the operand stack while unwinding.
144 for alt in [false, true] {
145 for depth in [9998, 9999, 10000, 20000] {
146 source := if alt {
147 'f(n,a,b,c,d,e):={if n==0 {0} else {1+f(n - 1,a,b,c,d,e)}}\ndisplay(f(${depth},1,2,3,4,5))\nnewline()'
148 } else {
149 '(define (f n a b c d e) (if (= n 0) 0 (+ 1 (f (- n 1) a b c d e)))) (display (f ${depth} 1 2 3 4 5)) (newline)'
150 }
151 result := os.exec([binary, if alt { '-v' } else { '-s' }, '-e', source])
152 assert result.exit_code == 0, result.output
153 assert result.output == '${depth}\n', result.output
154 if depth == 20000 {
155 path := os.join_path(dir, if alt { 'deep.vile' } else { 'deep.scm' })
156 os.write_file(path, source) or { panic(err) }
157 file := os.exec([binary, path])
158 assert file.exit_code == 0 && file.output == '${depth}\n', file.output
159 }
160 }
161 }
162 limited := os.exec([binary, '--max-steps', '100', '-e', '(let loop () (loop))'])
163 assert limited.exit_code != 0 && limited.output.contains('step limit exceeded'), limited.output
164}
165
166fn test_cli_vm_keeps_the_selected_step_budget_and_disables_the_frame_cap() {
167 mut vm := cli_vm(0, ['vile']) or { panic(err) }
168 value := vm.eval('(define (f n) (if (= n 0) 0 (+ 1 (f (- n 1))))) (f 12000)') or { panic(err) }
169 assert value.as_integer() or { panic(err) } == 12000
170 mut limited := cli_vm(100, ['vile']) or { panic(err) }
171 limited.eval('(let loop () (loop))') or {
172 assert err.msg().contains('step limit exceeded'), err.msg()
173 assert limited.eval('42') or { panic(err) }.as_integer() or { panic(err) } == 42
174 return
175 }
176 assert false, 'the CLI must still honor an explicit execution budget'
177}
178
179fn cli_system_interfaces(binary string, dir string) {
180 args_file := os.join_path(dir, 'arguments.scm')
181 alt_file := os.join_path(dir, 'arguments.vile')
182 os.write_file(args_file, '(write (command-line)) (newline)') or { panic(err) }
183 os.write_file(alt_file, 'write(command-line())\nnewline()') or { panic(err) }
184 for path in [args_file, alt_file] {
185 for items in [
186 []string{},
187 ['hello', 'two words', 'λ', ''],
188 ['--help', '-e', '(exit 99)'],
189 ['a', '--', 'b'],
190 ] {
191 mut cmd := [binary, path]
192 cmd << items
193 mut expected_items := [scheme.string_value(path)]
194 for item in items { expected_items << scheme.string_value(item) }
195 expected := scheme.write(scheme.list(expected_items)) or { panic(err) }
196 result := os.exec(cmd)
197 assert result.exit_code == 0, result.output
198 assert result.output.trim_space() == expected, '${cmd}: ${result.output}'
199 }
200 }
201 for item in ['argument', '--help', 'two words'] {
202 result := os.exec([binary, '-e', '(command-line)', '--', item])
203 assert result.exit_code == 0, result.output
204 assert result.output.trim_space() == scheme.write(scheme.list([
205 scheme.string_value(binary),
206 scheme.string_value(item),
207 ])) or { panic(err) }
208 }
209 for name in ['exit', 'emergency-exit'] {
210 for item in ['', '#t', '#f', '7', '-1', '256'] {
211 result := os.exec([binary, '-e', '(${name} ${item})', '-e', '(display "unreachable")'])
212 expected := match item {
213 '#f' { 1 }
214 '7' { 7 }
215 '-1' { 255 }
216 else { 0 }
217 }
218 assert result.exit_code == expected, '${name} ${item}: ${result}'
219 assert result.output == '', result.output
220 }
221 result := os.exec([binary, '-e', '(dynamic-wind (lambda () (display "before") (flush-output-port))
222 (lambda () (${name} 3)) (lambda () (display "after") (flush-output-port)))'])
223 assert result.exit_code == 3, result.output
224 assert result.output == if name == 'exit' { 'beforeafter' } else { 'before' }
225 for controller in ['call-with-output-file', 'call-with-port', 'with-output-to-file'] {
226 for alt in [false, true] {
227 path := os.join_path(dir, '${name}-${controller}-${alt}.txt')
228 quoted := scheme.write(scheme.string_value(path)) or { panic(err) }
229 argument := if controller == 'call-with-port' {
230 if alt { 'open-output-file(${quoted})' } else { '(open-output-file ${quoted})' }
231 } else {
232 quoted
233 }
234 managed := controller == 'with-output-to-file'
235 parameter := if managed { '' } else { 'p' }
236 display_arg := if managed {
237 ''
238 } else if alt {
239 ',p'
240 } else {
241 ' p'
242 }
243 source := if alt {
244 '${controller}(${argument},fn(${parameter}){display("buffered"${display_arg})\n${name}(7)})'
245 } else {
246 '(${controller} ${argument} (lambda (${parameter}) (display "buffered"${display_arg}) (${name} 7)))'
247 }
248 terminated := os.exec([binary, if alt { '-v' } else { '-s' }, '-e', source])
249 assert terminated.exit_code == 7 && terminated.output == '', terminated.output
250 assert os.read_file(path) or { panic(err) } == if name == 'exit' {
251 'buffered'
252 } else {
253 ''
254 }
255 }
256 }
257 }
258 for args in [['--read', args_file, 'extra'], ['--tokens', args_file, 'extra']] {
259 mut command := [binary]
260 command << args
261 result := os.exec(command)
262 assert result.exit_code != 0 && result.output.contains('exactly one file'), result.output
263 }
264}
265