| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | import rand |
| 5 | |
| 6 | const current_vexe = @VEXE |
| 7 | |
| 8 | const delegated_marker = 'VVM-DELEGATED' |
| 9 | |
| 10 | const requested_vvm_test_version = '99.99.99' |
| 11 | |
| 12 | struct CmdResult { |
| 13 | exit_code int |
| 14 | output string |
| 15 | } |
| 16 | |
| 17 | fn test_parse_vvmrc_version() { |
| 18 | assert parse_vvmrc_version('') == '' |
| 19 | assert parse_vvmrc_version(' ') == '' |
| 20 | assert parse_vvmrc_version('# only comments') == '' |
| 21 | assert parse_vvmrc_version('\n# comment\n0.4.6\n') == '0.4.6' |
| 22 | assert parse_vvmrc_version(' latest # comment ') == 'latest' |
| 23 | } |
| 24 | |
| 25 | fn test_find_project_vvmrc_stops_at_stop_paths() { |
| 26 | tmp_root := os.join_path(os.vtmp_dir(), 'vvmrc_stop_test_${rand.ulid()}') |
| 27 | os.mkdir_all(tmp_root) or { panic(err) } |
| 28 | defer { |
| 29 | os.rmdir_all(tmp_root) or {} |
| 30 | } |
| 31 | project_dir := os.join_path(tmp_root, 'project') |
| 32 | source_dir := os.join_path(project_dir, 'src') |
| 33 | os.mkdir_all(source_dir) or { panic(err) } |
| 34 | os.mkdir_all(os.join_path(project_dir, '.git')) or { panic(err) } |
| 35 | os.write_file(os.join_path(tmp_root, '.vvmrc'), requested_vvm_test_version) or { panic(err) } |
| 36 | main_v := os.join_path(source_dir, 'main.v') |
| 37 | os.write_file(main_v, 'fn main() {}') or { panic(err) } |
| 38 | assert find_project_vvmrc(main_v) == '' |
| 39 | local_vvmrc := os.join_path(project_dir, '.vvmrc') |
| 40 | os.write_file(local_vvmrc, requested_vvm_test_version) or { panic(err) } |
| 41 | assert find_project_vvmrc(main_v) == os.real_path(local_vvmrc) |
| 42 | } |
| 43 | |
| 44 | fn test_vvmrc_delegates_to_matching_compiler_executable() { |
| 45 | tmp_root := os.join_path(os.vtmp_dir(), 'vvmrc_delegate_test_${rand.ulid()}') |
| 46 | os.mkdir_all(tmp_root) or { panic(err) } |
| 47 | defer { |
| 48 | os.rmdir_all(tmp_root) or {} |
| 49 | } |
| 50 | bin_dir := os.join_path(tmp_root, 'bin') |
| 51 | os.mkdir_all(bin_dir) or { panic(err) } |
| 52 | fake_compiler_source := os.join_path(tmp_root, 'fake_compiler.v') |
| 53 | os.write_file(fake_compiler_source, |
| 54 | "import os\nfn main() {\n\tprintln('${delegated_marker} ' + os.args[1..].join(' '))\n}\n") or { |
| 55 | panic(err) |
| 56 | } |
| 57 | fake_compiler_exe := os.join_path(bin_dir, if os.user_os() == 'windows' { |
| 58 | 'v${requested_vvm_test_version}.exe' |
| 59 | } else { |
| 60 | 'v${requested_vvm_test_version}' |
| 61 | }) |
| 62 | compile_res := |
| 63 | os.execute('${os.quoted_path(current_vexe)} -o ${os.quoted_path(fake_compiler_exe)} ${os.quoted_path(fake_compiler_source)}') |
| 64 | assert compile_res.exit_code == 0, compile_res.output |
| 65 | project_dir := os.join_path(tmp_root, 'project') |
| 66 | os.mkdir_all(project_dir) or { panic(err) } |
| 67 | os.write_file(os.join_path(project_dir, '.vvmrc'), requested_vvm_test_version) or { panic(err) } |
| 68 | os.write_file(os.join_path(project_dir, 'main.v'), |
| 69 | "fn main() {\n\tprintln('from project')\n}\n") or { panic(err) } |
| 70 | mut envs := os.environ() |
| 71 | path_key := find_path_env_key(envs) |
| 72 | envs[path_key] = '${bin_dir}${os.path_delimiter}${envs[path_key]}' |
| 73 | envs['V_SKIP_VVMRC'] = '' |
| 74 | res := run_v_command(project_dir, ['run', 'main.v'], envs) |
| 75 | assert res.exit_code == 0, res.output |
| 76 | assert res.output.contains('${delegated_marker} run main.v'), res.output |
| 77 | } |
| 78 | |
| 79 | fn run_v_command(work_dir string, args []string, envs map[string]string) CmdResult { |
| 80 | mut process := os.new_process(current_vexe) |
| 81 | process.set_work_folder(work_dir) |
| 82 | process.set_args(args) |
| 83 | process.set_environment(envs) |
| 84 | process.set_redirect_stdio() |
| 85 | process.wait() |
| 86 | output := process.stdout_slurp() + process.stderr_slurp() |
| 87 | exit_code := if process.code == -1 { 1 } else { process.code } |
| 88 | process.close() |
| 89 | return CmdResult{ |
| 90 | exit_code: exit_code |
| 91 | output: output |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn find_path_env_key(envs map[string]string) string { |
| 96 | if 'PATH' in envs { |
| 97 | return 'PATH' |
| 98 | } |
| 99 | if 'Path' in envs { |
| 100 | return 'Path' |
| 101 | } |
| 102 | return 'PATH' |
| 103 | } |
| 104 | |