| 1 | module util |
| 2 | |
| 3 | import os |
| 4 | import time |
| 5 | import v.pref |
| 6 | |
| 7 | fn test_tool_recompilation_args_force_system_cc_for_vdoc_on_freebsd() { |
| 8 | assert tool_recompilation_args('vdoc', 'freebsd') == ['-cc', 'cc'] |
| 9 | } |
| 10 | |
| 11 | fn test_tool_recompilation_args_use_openssl_for_vpm() { |
| 12 | assert tool_recompilation_args('vpm', 'macos') == ['-d', 'use_openssl'] |
| 13 | } |
| 14 | |
| 15 | fn test_tool_recompilation_args_do_not_change_other_tools_or_platforms() { |
| 16 | assert tool_recompilation_args('vfmt', 'freebsd').len == 0 |
| 17 | assert tool_recompilation_args('vdoc', 'linux').len == 0 |
| 18 | // musl-gcc on docker-ubuntu-musl can't find glibc's sys/cdefs.h that |
| 19 | // OpenSSL pulls in, so the `-d use_openssl` workaround is restricted to |
| 20 | // macOS where the original tcc/Apple-Silicon bug was reported. |
| 21 | assert tool_recompilation_args('vpm', 'linux').len == 0 |
| 22 | assert tool_recompilation_args('vpm', 'windows').len == 0 |
| 23 | } |
| 24 | |
| 25 | fn test_fallback_tool_executable_path_uses_vtmp_for_missing_single_file_tool() { |
| 26 | tmp_dir := os.join_path(os.vtmp_dir(), 'util_test_fallback_tool_executable_path') |
| 27 | os.mkdir_all(tmp_dir) or { panic(err) } |
| 28 | defer { |
| 29 | os.rmdir_all(tmp_dir) or {} |
| 30 | } |
| 31 | tool_source := os.join_path(tmp_dir, 'vdoctor.v') |
| 32 | os.write_file(tool_source, 'fn main() {}') or { panic(err) } |
| 33 | tool_exe := os.join_path(tmp_dir, 'vdoctor') |
| 34 | |
| 35 | fallback := fallback_tool_executable_path(@VEXE, '/opt/vlang', 'vdoctor', tool_source, |
| 36 | tool_exe, true) |
| 37 | |
| 38 | assert fallback != tool_exe |
| 39 | assert fallback.starts_with(os.join_path(os.vtmp_dir(), 'tools')) |
| 40 | assert fallback.ends_with(path_of_executable('vdoctor')) |
| 41 | } |
| 42 | |
| 43 | fn test_fallback_tool_executable_path_keeps_directory_tools_in_place() { |
| 44 | tmp_dir := os.join_path(os.vtmp_dir(), 'util_test_fallback_tool_directory') |
| 45 | os.mkdir_all(tmp_dir) or { panic(err) } |
| 46 | defer { |
| 47 | os.rmdir_all(tmp_dir) or {} |
| 48 | } |
| 49 | tool_source := os.join_path(tmp_dir, 'vdoc') |
| 50 | os.mkdir_all(tool_source) or { panic(err) } |
| 51 | tool_exe := os.join_path(tool_source, 'vdoc') |
| 52 | |
| 53 | fallback := |
| 54 | fallback_tool_executable_path(@VEXE, '/opt/vlang', 'vdoc', tool_source, tool_exe, true) |
| 55 | |
| 56 | assert fallback == tool_exe |
| 57 | } |
| 58 | |
| 59 | fn test_fallback_tool_executable_path_uses_vtmp_for_outdated_single_file_tool_in_readonly_dir() { |
| 60 | $if windows { |
| 61 | return |
| 62 | } |
| 63 | if os.geteuid() == 0 { |
| 64 | return |
| 65 | } |
| 66 | tmp_dir := os.join_path(os.vtmp_dir(), |
| 67 | 'util_test_fallback_tool_executable_path_readonly_outdated') |
| 68 | os.mkdir_all(tmp_dir) or { panic(err) } |
| 69 | tool_source := os.join_path(tmp_dir, 'vrepl.v') |
| 70 | tool_exe := os.join_path(tmp_dir, 'vrepl') |
| 71 | os.write_file(tool_exe, '') or { panic(err) } |
| 72 | time.sleep(1100 * time.millisecond) |
| 73 | os.write_file(tool_source, 'fn main() {}') or { panic(err) } |
| 74 | os.chmod(tmp_dir, 0o500) or { panic(err) } |
| 75 | defer { |
| 76 | os.chmod(tmp_dir, 0o700) or {} |
| 77 | os.rmdir_all(tmp_dir) or {} |
| 78 | } |
| 79 | if os.is_writable(tmp_dir) { |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | fallback := fallback_tool_executable_path(@VEXE, '/opt/vlang', 'vrepl', tool_source, tool_exe, |
| 84 | false) |
| 85 | |
| 86 | assert fallback != tool_exe |
| 87 | assert fallback.starts_with(os.join_path(os.vtmp_dir(), 'tools')) |
| 88 | assert fallback.ends_with(path_of_executable('vrepl')) |
| 89 | } |
| 90 | |
| 91 | fn test_fallback_tool_executable_path_keeps_current_single_file_tool_in_readonly_dir() { |
| 92 | $if windows { |
| 93 | return |
| 94 | } |
| 95 | if os.geteuid() == 0 { |
| 96 | return |
| 97 | } |
| 98 | tmp_dir := os.join_path(os.vtmp_dir(), |
| 99 | 'util_test_fallback_tool_executable_path_readonly_current') |
| 100 | os.mkdir_all(tmp_dir) or { panic(err) } |
| 101 | tool_source := os.join_path(tmp_dir, 'vrepl.v') |
| 102 | tool_exe := os.join_path(tmp_dir, 'vrepl') |
| 103 | os.write_file(tool_source, 'fn main() {}') or { panic(err) } |
| 104 | time.sleep(1100 * time.millisecond) |
| 105 | os.write_file(tool_exe, '') or { panic(err) } |
| 106 | os.chmod(tmp_dir, 0o500) or { panic(err) } |
| 107 | defer { |
| 108 | os.chmod(tmp_dir, 0o700) or {} |
| 109 | os.rmdir_all(tmp_dir) or {} |
| 110 | } |
| 111 | if os.is_writable(tmp_dir) { |
| 112 | return |
| 113 | } |
| 114 | |
| 115 | fallback := fallback_tool_executable_path(@VEXE, '/opt/vlang', 'vrepl', tool_source, tool_exe, |
| 116 | false) |
| 117 | |
| 118 | assert fallback == tool_exe |
| 119 | } |
| 120 | |
| 121 | fn test_vlines_escape_path_does_not_restore_old_tcc_prefix_workaround() { |
| 122 | tmp_dir := os.join_path(os.vtmp_dir(), 'util_test_vlines_escape_path_${os.getpid()}') |
| 123 | os.mkdir_all(tmp_dir) or { panic(err) } |
| 124 | defer { |
| 125 | os.rmdir_all(tmp_dir) or {} |
| 126 | } |
| 127 | source_path := os.join_path(tmp_dir, 'probe.v') |
| 128 | os.write_file(source_path, 'fn main() {}') or { panic(err) } |
| 129 | |
| 130 | expected := cescaped_path(os.real_path(source_path)) |
| 131 | assert vlines_escape_path(source_path, 'gcc') == expected |
| 132 | |
| 133 | escaped_tcc_path := vlines_escape_path(source_path, 'tcc') |
| 134 | assert escaped_tcc_path == expected |
| 135 | assert !escaped_tcc_path.starts_with('../../../../../..') |
| 136 | $if windows { |
| 137 | assert escaped_tcc_path.starts_with(os.windows_volume(source_path)) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | fn test_qualify_import_stops_at_nearest_vmod_issue_26828() { |
| 142 | root := os.join_path(os.vtmp_dir(), 'v_qualify_import_issue_26828_${os.getpid()}') |
| 143 | os.rmdir_all(root) or {} |
| 144 | defer { |
| 145 | os.rmdir_all(root) or {} |
| 146 | } |
| 147 | project_root := os.join_path(root, 'outer') |
| 148 | module_root := os.join_path(project_root, 'cli004') |
| 149 | os.mkdir_all(os.join_path(module_root, 'sub'))! |
| 150 | os.write_file(os.join_path(project_root, 'v.mod'), "Module {\n\tname: 'outer'\n}\n")! |
| 151 | os.write_file(os.join_path(module_root, 'v.mod'), "Module {\n\tname: 'cli004'\n}\n")! |
| 152 | main_file := os.join_path(module_root, 'cli004.v') |
| 153 | os.write_file(main_file, 'module main\n\nimport sub\n\nfn main() {}\n')! |
| 154 | |
| 155 | mut p := pref.new_preferences() |
| 156 | p.path = '.' |
| 157 | old_dir := os.getwd() |
| 158 | defer { |
| 159 | os.chdir(old_dir) or { panic(err) } |
| 160 | } |
| 161 | os.chdir(module_root)! |
| 162 | |
| 163 | assert qualify_import(p, 'sub', main_file) == 'sub' |
| 164 | assert qualify_import(p, 'sub', 'cli004.v') == 'sub' |
| 165 | } |
| 166 | |