module main import os import scheme import time struct CliFormatCase { args []string output string status int } fn test_cli_format_detection_and_explicit_syntax_flags() { dir := os.join_path(os.temp_dir(), 'vile-cli-${os.getpid()}-${time.sys_mono_now()}') os.mkdir_all(dir) or { panic(err) } defer { os.rmdir_all(dir) or {} } binary := os.join_path(dir, 'vile') mut build := [@VEXE, '-cc', @CCOMPILER] source_path := os.getenv('VILE_TEST_VPATH') if source_path != '' { build << ['-path', source_path] } build << ['-o', binary, @VMODROOT] built := os.exec(build) assert built.exit_code == 0, built.output scheme_file := os.join_path(dir, 'program.scm') c_file := os.join_path(dir, 'code with spaces.vile') legacy_file := os.join_path(dir, 'legacy.vilec') unknown_file := os.join_path(dir, 'program.txt') forced_file := os.join_path(dir, 'forced.scm') forced_scheme_file := os.join_path(dir, 'forced-scheme.vile') inspect_file := os.join_path(dir, 'inspect.vile') load_file := os.join_path(dir, 'load.scm') settings_file := os.join_path(dir, 'settings.vile') macro_file := os.join_path(dir, 'macro.scm') os.write_file(settings_file, 'answer:=41') or { panic(err) } os.write_file(macro_file, '(define-syntax increment (syntax-rules () ((_ x) (+ x 1))))') or { panic(err) } os.write_file(load_file, '(load "settings.vile") (load "macro.scm") (display (increment answer)) (newline)') or { panic(err) } parent := os.join_path(dir, 'parent.vile') os.mkdir_all(parent) or { panic(err) } nested_file := os.join_path(parent, 'nested.scm') for file in [scheme_file, legacy_file, unknown_file, nested_file, forced_scheme_file] { os.write_file(file, '(display (+ 20 22)) (newline)') or { panic(err) } } for file in [c_file, forced_file] { os.write_file(file, 'display(20+22)\nnewline()') or { panic(err) } } os.write_file(inspect_file, 'display([1,2])') or { panic(err) } for case in [ CliFormatCase{ args: [load_file], output: '42' }, CliFormatCase{ args: [scheme_file], output: '42' }, CliFormatCase{ args: [c_file], output: '42' }, CliFormatCase{ args: [legacy_file], output: '42' }, CliFormatCase{ args: [unknown_file], output: '42' }, CliFormatCase{ args: [nested_file], output: '42' }, CliFormatCase{ args: ['-v', forced_file], output: '42' }, CliFormatCase{ args: ['--vile', forced_file], output: '42' }, CliFormatCase{ args: ['-s', forced_scheme_file], output: '42' }, CliFormatCase{ args: ['--scheme', forced_scheme_file], output: '42' }, CliFormatCase{ args: ['-v', '-e', '3+2*4'], output: '11' }, CliFormatCase{ args: ['--vile', '-e', 'x:=20', '-e', 'x+22'], output: '42' }, CliFormatCase{ args: ['-v', '-e', 'x:=1\ny:=2\nfoobar:=#f', '-e', 'x if foobar {+} else {-} y', '-e', 'foobar=#t', '-e', 'x if foobar {+} else {-} y'] output: '-1\n3' }, CliFormatCase{ args: ['-v', '-e', 'a():={display(12),newline()}\na'], output: '12' }, CliFormatCase{ args: ['-s', '-e', '(define (a) 12) a'], output: '#' }, CliFormatCase{ args: ['-s', '-e', '(+ 20 22)'], output: '42' }, CliFormatCase{ args: ['-s', '-e', "(define-class ()) (slot-set! 'print (lambda (obj out) (display 17 out))) (make )"] output: '17' }, CliFormatCase{ args: ['--scheme', '-e', '(define x 20)', '-e', '(+ x 22)'], output: '42' }, CliFormatCase{ args: ['-s', '-e', '#!no-fold-case (define MiXeD 42)', '-e', 'MiXeD'], output: '42' }, CliFormatCase{ args: ['-v', '-e', '#!NO-FOLD-CASE MiXeD:=42', '-e', 'MiXeD'], output: '42' }, CliFormatCase{ args: ['-s', '-e', '#!no-fold-case (define MiXeD 42) (define mixed 7)', '-e', '#!FOLD-CASE MiXeD'] output: '7' }, CliFormatCase{ args: ['-e', '(+ 20 22)'], output: '42' }, CliFormatCase{ args: ['--read', inspect_file], output: '(display (vector 1 2))' }, CliFormatCase{ args: ['-v', '--read', forced_file], output: '(display (+ 20 22))\n(newline)' }, CliFormatCase{ args: ['-s', '--read', forced_scheme_file], output: '(display (+ 20 22))\n(newline)' }, ] { mut command := [binary] command << case.args result := os.exec(command) assert result.exit_code == case.status, '${case.args}: ${result.output}' assert result.output.trim_space() == case.output, '${case.args}: ${result.output}' } tokens := os.exec([binary, '--tokens', inspect_file]) assert tokens.exit_code == 0, tokens.output assert tokens.output.contains('\tlbracket\t['), tokens.output assert tokens.output.contains('\trbracket\t]'), tokens.output for args in [ ['--alt-syntax', '-e', '1'], ['--vile-c', '-e', '1'], ['-c', '-e', '1'], [forced_file], [forced_scheme_file], ['-s', c_file], ['-v', '-s', '-e', '1'], ['--vile', '--scheme', '-e', '1'], ['--scheme', '-v', '-e', '1'], ] { mut command := [binary] command << args result := os.exec(command) assert result.exit_code != 0, '${args}: ${result.output}' } help := os.exec([binary, '--help']) assert help.exit_code == 0, help.output assert help.output.contains('--vile') && help.output.contains('--scheme'), help.output assert !help.output.contains('--vile-c'), help.output assert !help.output.contains('--alt-syntax'), help.output assert !help.output.contains('.vilec'), help.output cli_system_interfaces(binary, dir) cli_shared_repl_input(binary, dir) cli_recursion_limits(binary, dir) version := os.exec([binary, '--version']) assert version.exit_code == 0 && version.output.contains('0.0.0'), version.output } fn cli_shared_repl_input(binary string, dir string) { input := os.join_path(dir, 'repl-input.scm') os.write_file(input, '(read-char)\nx(+ 20\n22)\n') or { panic(err) } mut child := os.new_process(binary) defer { child.close() } child.set_args(['-s']) child.set_redirect_stdio_merged() child.set_stdin_path(input) child.run() output := child.stdout_slurp() child.wait() assert child.code == 0, output assert output.trim_space().split_into_lines() == [r'#\x', '42'], output } fn cli_recursion_limits(binary string, dir string) { // Include both sides of the former CLI ceiling and a deeper call. The five // extra arguments also exercise restoring the operand stack while unwinding. for alt in [false, true] { for depth in [9998, 9999, 10000, 20000] { source := if alt { '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()' } else { '(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)' } result := os.exec([binary, if alt { '-v' } else { '-s' }, '-e', source]) assert result.exit_code == 0, result.output assert result.output == '${depth}\n', result.output if depth == 20000 { path := os.join_path(dir, if alt { 'deep.vile' } else { 'deep.scm' }) os.write_file(path, source) or { panic(err) } file := os.exec([binary, path]) assert file.exit_code == 0 && file.output == '${depth}\n', file.output } } } limited := os.exec([binary, '--max-steps', '100', '-e', '(let loop () (loop))']) assert limited.exit_code != 0 && limited.output.contains('step limit exceeded'), limited.output } fn test_cli_vm_keeps_the_selected_step_budget_and_disables_the_frame_cap() { mut vm := cli_vm(0, ['vile']) or { panic(err) } value := vm.eval('(define (f n) (if (= n 0) 0 (+ 1 (f (- n 1))))) (f 12000)') or { panic(err) } assert value.as_integer() or { panic(err) } == 12000 mut limited := cli_vm(100, ['vile']) or { panic(err) } limited.eval('(let loop () (loop))') or { assert err.msg().contains('step limit exceeded'), err.msg() assert limited.eval('42') or { panic(err) }.as_integer() or { panic(err) } == 42 return } assert false, 'the CLI must still honor an explicit execution budget' } fn cli_system_interfaces(binary string, dir string) { args_file := os.join_path(dir, 'arguments.scm') alt_file := os.join_path(dir, 'arguments.vile') os.write_file(args_file, '(write (command-line)) (newline)') or { panic(err) } os.write_file(alt_file, 'write(command-line())\nnewline()') or { panic(err) } for path in [args_file, alt_file] { for items in [ []string{}, ['hello', 'two words', 'λ', ''], ['--help', '-e', '(exit 99)'], ['a', '--', 'b'], ] { mut cmd := [binary, path] cmd << items mut expected_items := [scheme.string_value(path)] for item in items { expected_items << scheme.string_value(item) } expected := scheme.write(scheme.list(expected_items)) or { panic(err) } result := os.exec(cmd) assert result.exit_code == 0, result.output assert result.output.trim_space() == expected, '${cmd}: ${result.output}' } } for item in ['argument', '--help', 'two words'] { result := os.exec([binary, '-e', '(command-line)', '--', item]) assert result.exit_code == 0, result.output assert result.output.trim_space() == scheme.write(scheme.list([ scheme.string_value(binary), scheme.string_value(item), ])) or { panic(err) } } for name in ['exit', 'emergency-exit'] { for item in ['', '#t', '#f', '7', '-1', '256'] { result := os.exec([binary, '-e', '(${name} ${item})', '-e', '(display "unreachable")']) expected := match item { '#f' { 1 } '7' { 7 } '-1' { 255 } else { 0 } } assert result.exit_code == expected, '${name} ${item}: ${result}' assert result.output == '', result.output } result := os.exec([binary, '-e', '(dynamic-wind (lambda () (display "before") (flush-output-port)) (lambda () (${name} 3)) (lambda () (display "after") (flush-output-port)))']) assert result.exit_code == 3, result.output assert result.output == if name == 'exit' { 'beforeafter' } else { 'before' } for controller in ['call-with-output-file', 'call-with-port', 'with-output-to-file'] { for alt in [false, true] { path := os.join_path(dir, '${name}-${controller}-${alt}.txt') quoted := scheme.write(scheme.string_value(path)) or { panic(err) } argument := if controller == 'call-with-port' { if alt { 'open-output-file(${quoted})' } else { '(open-output-file ${quoted})' } } else { quoted } managed := controller == 'with-output-to-file' parameter := if managed { '' } else { 'p' } display_arg := if managed { '' } else if alt { ',p' } else { ' p' } source := if alt { '${controller}(${argument},fn(${parameter}){display("buffered"${display_arg})\n${name}(7)})' } else { '(${controller} ${argument} (lambda (${parameter}) (display "buffered"${display_arg}) (${name} 7)))' } terminated := os.exec([binary, if alt { '-v' } else { '-s' }, '-e', source]) assert terminated.exit_code == 7 && terminated.output == '', terminated.output assert os.read_file(path) or { panic(err) } == if name == 'exit' { 'buffered' } else { '' } } } } for args in [['--read', args_file, 'extra'], ['--tokens', args_file, 'extra']] { mut command := [binary] command << args result := os.exec(command) assert result.exit_code != 0 && result.output.contains('exactly one file'), result.output } }