| 1 | // vtest build: !musl? && !sanitized_job? |
| 2 | module main |
| 3 | |
| 4 | import os |
| 5 | import v.slow_tests.repl.runner |
| 6 | |
| 7 | fn test_repl_keeps_time_assignments_stable_across_reads() { |
| 8 | vexec := runner.full_path_to_v(5) |
| 9 | temp_dir := os.join_path(os.vtmp_dir(), 'v_repl_time_state_${os.getpid()}') |
| 10 | os.mkdir_all(temp_dir) or { panic(err) } |
| 11 | defer { |
| 12 | os.rmdir_all(temp_dir) or {} |
| 13 | } |
| 14 | input_file := os.join_path(temp_dir, 'input.txt') |
| 15 | input := [ |
| 16 | 'import time', |
| 17 | 'a := time.now()', |
| 18 | 'a', |
| 19 | 'time.sleep(1200 * time.millisecond)', |
| 20 | 'a', |
| 21 | 'exit', |
| 22 | ].join('\n') |
| 23 | os.write_file(input_file, input) or { panic(err) } |
| 24 | original_vexe := os.getenv_opt('VEXE') |
| 25 | os.setenv('VEXE', vexec, true) |
| 26 | defer { |
| 27 | if old_vexe := original_vexe { |
| 28 | os.setenv('VEXE', old_vexe, true) |
| 29 | } else { |
| 30 | os.unsetenv('VEXE') |
| 31 | } |
| 32 | } |
| 33 | mut repl := os.new_process(vexec) |
| 34 | repl.set_args(['repl', '-replfolder', temp_dir, '-replprefix', 'time_state.']) |
| 35 | repl.set_redirect_stdio() |
| 36 | repl.create_no_window = true |
| 37 | repl.run() |
| 38 | repl.stdin_write(input + '\n') |
| 39 | repl.wait() |
| 40 | output_raw := repl.stdout_slurp() + repl.stderr_slurp() |
| 41 | exit_code := repl.code |
| 42 | repl.close() |
| 43 | assert exit_code == 0, output_raw |
| 44 | output := output_raw.replace_each(['\r', '', '>>> ', '', '>>>', '', '... ', '', |
| 45 | temp_dir + os.path_separator, '', os.dir(vexec) + os.path_separator, '']).trim_right('\n\r') |
| 46 | lines := output.split_into_lines() |
| 47 | assert lines.len == 2, 'expected 2 repl outputs, got ${lines.len}: ${lines}' |
| 48 | assert lines[0] == lines[1], 'expected the stored time to remain stable, got ${lines}' |
| 49 | } |
| 50 | |
| 51 | fn test_repl_bypasses_local_cmd_exe_on_windows() { |
| 52 | $if !windows { |
| 53 | return |
| 54 | } |
| 55 | vexec := runner.full_path_to_v(5) |
| 56 | temp_dir := os.join_path(os.vtmp_dir(), 'v_repl_cmd_bypass_${os.getpid()}') |
| 57 | os.rmdir_all(temp_dir) or {} |
| 58 | os.mkdir_all(temp_dir) or { panic(err) } |
| 59 | defer { |
| 60 | os.rmdir_all(temp_dir) or {} |
| 61 | } |
| 62 | fake_cmd_source := os.join_path(temp_dir, 'fake_cmd.v') |
| 63 | fake_cmd_exe := os.join_path(temp_dir, 'cmd.exe') |
| 64 | os.write_file(fake_cmd_source, "fn main() {\n\tprintln('fake cmd invoked')\n}\n") or { |
| 65 | panic(err) |
| 66 | } |
| 67 | build_fake_cmd := |
| 68 | os.execute('${os.quoted_path(vexec)} -o ${os.quoted_path(fake_cmd_exe)} ${os.quoted_path(fake_cmd_source)}') |
| 69 | assert build_fake_cmd.exit_code == 0, build_fake_cmd.output |
| 70 | original_vexe := os.getenv('VEXE') |
| 71 | os.setenv('VEXE', vexec, true) |
| 72 | original_vflags := os.getenv('VFLAGS') |
| 73 | os.setenv('VFLAGS', '', true) |
| 74 | defer { |
| 75 | os.setenv('VEXE', original_vexe, true) |
| 76 | os.setenv('VFLAGS', original_vflags, true) |
| 77 | } |
| 78 | mut repl := os.new_process(vexec) |
| 79 | repl.set_args(['repl', '-replfolder', temp_dir, '-replprefix', 'cmd_bypass.']) |
| 80 | repl.set_redirect_stdio() |
| 81 | repl.create_no_window = true |
| 82 | repl.run() |
| 83 | repl.stdin_write('println(1 + 1)\nexit\n') |
| 84 | repl.wait() |
| 85 | output := repl.stdout_slurp() + repl.stderr_slurp() |
| 86 | repl.close() |
| 87 | assert repl.code == 0, output |
| 88 | assert !output.contains('fake cmd invoked'), output |
| 89 | assert output.trim_space() == '2', output |
| 90 | } |
| 91 | |