| 1 | module pref |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | fn test_usable_bundled_tcc_compiler_skips_broken_binary() { |
| 6 | $if windows { |
| 7 | return |
| 8 | } |
| 9 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 10 | prepare_test_tcc_binary(test_root, 'exit 1') |
| 11 | defer { |
| 12 | os.rmdir_all(test_root) or {} |
| 13 | } |
| 14 | assert usable_bundled_tcc_compiler(test_root) == '' |
| 15 | } |
| 16 | |
| 17 | fn test_usable_bundled_tcc_compiler_accepts_working_binary() { |
| 18 | $if windows { |
| 19 | return |
| 20 | } |
| 21 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 22 | tcc_path := prepare_test_tcc_binary(test_root, 'exit 0') |
| 23 | defer { |
| 24 | os.rmdir_all(test_root) or {} |
| 25 | } |
| 26 | assert usable_bundled_tcc_compiler(test_root) == tcc_path |
| 27 | } |
| 28 | |
| 29 | fn test_usable_bundled_tcc_compiler_rejects_non_executable_file() { |
| 30 | $if windows { |
| 31 | return |
| 32 | } |
| 33 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 34 | tcc_path := prepare_test_tcc_binary(test_root, 'exit 0') |
| 35 | os.chmod(tcc_path, 0o600) or { panic(err) } |
| 36 | defer { |
| 37 | os.rmdir_all(test_root) or {} |
| 38 | } |
| 39 | assert usable_bundled_tcc_compiler(test_root) == '' |
| 40 | } |
| 41 | |
| 42 | fn test_try_to_use_tcc_by_default_keeps_explicit_system_tcc_on_musl() { |
| 43 | $if windows { |
| 44 | return |
| 45 | } |
| 46 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 47 | prepare_test_tcc_binary(test_root, 'exit 1') |
| 48 | fake_vexe := os.join_path(test_root, 'v') |
| 49 | old_vexe := os.getenv('VEXE') |
| 50 | os.setenv('VEXE', fake_vexe, true) |
| 51 | defer { |
| 52 | if old_vexe == '' { |
| 53 | os.unsetenv('VEXE') |
| 54 | } else { |
| 55 | os.setenv('VEXE', old_vexe, true) |
| 56 | } |
| 57 | os.rmdir_all(test_root) or {} |
| 58 | } |
| 59 | mut prefs := Preferences{ |
| 60 | ccompiler: 'tcc' |
| 61 | is_musl: true |
| 62 | } |
| 63 | prefs.try_to_use_tcc_by_default() |
| 64 | assert prefs.ccompiler == 'tcc' |
| 65 | } |
| 66 | |
| 67 | fn test_try_to_use_tcc_by_default_skips_broken_bundled_tcc_on_musl() { |
| 68 | $if windows { |
| 69 | return |
| 70 | } |
| 71 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 72 | prepare_test_tcc_binary(test_root, 'exit 1') |
| 73 | fake_vexe := os.join_path(test_root, 'v') |
| 74 | old_vexe := os.getenv('VEXE') |
| 75 | os.setenv('VEXE', fake_vexe, true) |
| 76 | defer { |
| 77 | if old_vexe == '' { |
| 78 | os.unsetenv('VEXE') |
| 79 | } else { |
| 80 | os.setenv('VEXE', old_vexe, true) |
| 81 | } |
| 82 | os.rmdir_all(test_root) or {} |
| 83 | } |
| 84 | mut prefs := Preferences{ |
| 85 | is_musl: true |
| 86 | } |
| 87 | prefs.try_to_use_tcc_by_default() |
| 88 | assert prefs.ccompiler == '' |
| 89 | } |
| 90 | |
| 91 | fn test_try_to_use_tcc_by_default_skips_broken_bundled_tcc_off_musl() { |
| 92 | $if windows { |
| 93 | return |
| 94 | } |
| 95 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 96 | prepare_test_tcc_binary(test_root, 'exit 1') |
| 97 | fake_vexe := os.join_path(test_root, 'v') |
| 98 | old_vexe := os.getenv('VEXE') |
| 99 | os.setenv('VEXE', fake_vexe, true) |
| 100 | defer { |
| 101 | if old_vexe == '' { |
| 102 | os.unsetenv('VEXE') |
| 103 | } else { |
| 104 | os.setenv('VEXE', old_vexe, true) |
| 105 | } |
| 106 | os.rmdir_all(test_root) or {} |
| 107 | } |
| 108 | mut prefs := Preferences{} |
| 109 | prefs.try_to_use_tcc_by_default() |
| 110 | assert prefs.ccompiler == '' |
| 111 | } |
| 112 | |
| 113 | fn test_try_to_use_tcc_by_default_skips_tcc_for_prealloc() { |
| 114 | $if windows { |
| 115 | return |
| 116 | } |
| 117 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 118 | prepare_test_tcc_binary(test_root, 'exit 0') |
| 119 | fake_vexe := os.join_path(test_root, 'v') |
| 120 | old_vexe := os.getenv('VEXE') |
| 121 | os.setenv('VEXE', fake_vexe, true) |
| 122 | defer { |
| 123 | if old_vexe == '' { |
| 124 | os.unsetenv('VEXE') |
| 125 | } else { |
| 126 | os.setenv('VEXE', old_vexe, true) |
| 127 | } |
| 128 | os.rmdir_all(test_root) or {} |
| 129 | } |
| 130 | mut prefs := Preferences{ |
| 131 | prealloc: true |
| 132 | } |
| 133 | prefs.try_to_use_tcc_by_default() |
| 134 | assert prefs.ccompiler == '' |
| 135 | } |
| 136 | |
| 137 | fn test_usable_system_tcc_compiler_prefers_termux_tcc_from_path() { |
| 138 | $if windows { |
| 139 | return |
| 140 | } |
| 141 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 142 | system_tcc := prepare_test_executable(test_root, 'bin/tcc', 'exit 0') |
| 143 | old_path := os.getenv('PATH') |
| 144 | old_termux_version := os.getenv('TERMUX_VERSION') |
| 145 | os.setenv('PATH', os.dir(system_tcc), true) |
| 146 | os.setenv('TERMUX_VERSION', '0.118.0', true) |
| 147 | defer { |
| 148 | os.setenv('PATH', old_path, true) |
| 149 | if old_termux_version == '' { |
| 150 | os.unsetenv('TERMUX_VERSION') |
| 151 | } else { |
| 152 | os.setenv('TERMUX_VERSION', old_termux_version, true) |
| 153 | } |
| 154 | os.rmdir_all(test_root) or {} |
| 155 | } |
| 156 | assert usable_system_tcc_compiler() == system_tcc |
| 157 | } |
| 158 | |
| 159 | fn test_default_tcc_compiler_uses_system_tcc_on_termux_when_bundled_is_missing() { |
| 160 | $if windows { |
| 161 | return |
| 162 | } |
| 163 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 164 | fake_vexe := os.join_path(test_root, 'v') |
| 165 | system_tcc := prepare_test_executable(test_root, 'bin/tcc', 'exit 0') |
| 166 | old_vexe := os.getenv('VEXE') |
| 167 | old_path := os.getenv('PATH') |
| 168 | old_termux_version := os.getenv('TERMUX_VERSION') |
| 169 | os.setenv('VEXE', fake_vexe, true) |
| 170 | os.setenv('PATH', os.dir(system_tcc), true) |
| 171 | os.setenv('TERMUX_VERSION', '0.118.0', true) |
| 172 | defer { |
| 173 | if old_vexe == '' { |
| 174 | os.unsetenv('VEXE') |
| 175 | } else { |
| 176 | os.setenv('VEXE', old_vexe, true) |
| 177 | } |
| 178 | os.setenv('PATH', old_path, true) |
| 179 | if old_termux_version == '' { |
| 180 | os.unsetenv('TERMUX_VERSION') |
| 181 | } else { |
| 182 | os.setenv('TERMUX_VERSION', old_termux_version, true) |
| 183 | } |
| 184 | os.rmdir_all(test_root) or {} |
| 185 | } |
| 186 | assert default_tcc_compiler() == system_tcc |
| 187 | } |
| 188 | |
| 189 | fn test_try_to_use_tcc_by_default_resolves_explicit_tcc_to_system_tcc_on_termux() { |
| 190 | $if windows { |
| 191 | return |
| 192 | } |
| 193 | test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test') |
| 194 | fake_vexe := os.join_path(test_root, 'v') |
| 195 | system_tcc := prepare_test_executable(test_root, 'bin/tcc', 'exit 0') |
| 196 | old_vexe := os.getenv('VEXE') |
| 197 | old_path := os.getenv('PATH') |
| 198 | old_termux_version := os.getenv('TERMUX_VERSION') |
| 199 | os.setenv('VEXE', fake_vexe, true) |
| 200 | os.setenv('PATH', os.dir(system_tcc), true) |
| 201 | os.setenv('TERMUX_VERSION', '0.118.0', true) |
| 202 | defer { |
| 203 | if old_vexe == '' { |
| 204 | os.unsetenv('VEXE') |
| 205 | } else { |
| 206 | os.setenv('VEXE', old_vexe, true) |
| 207 | } |
| 208 | os.setenv('PATH', old_path, true) |
| 209 | if old_termux_version == '' { |
| 210 | os.unsetenv('TERMUX_VERSION') |
| 211 | } else { |
| 212 | os.setenv('TERMUX_VERSION', old_termux_version, true) |
| 213 | } |
| 214 | os.rmdir_all(test_root) or {} |
| 215 | } |
| 216 | mut prefs := Preferences{ |
| 217 | ccompiler: 'tcc' |
| 218 | } |
| 219 | prefs.try_to_use_tcc_by_default() |
| 220 | assert prefs.ccompiler == system_tcc |
| 221 | } |
| 222 | |
| 223 | fn prepare_test_executable(test_root string, relative_path string, exit_line string) string { |
| 224 | path := os.join_path(test_root, relative_path) |
| 225 | os.mkdir_all(os.dir(path)) or { panic(err) } |
| 226 | os.write_file(path, '#!/bin/sh\n${exit_line}\n') or { panic(err) } |
| 227 | os.chmod(path, 0o700) or { panic(err) } |
| 228 | return path |
| 229 | } |
| 230 | |
| 231 | fn prepare_test_tcc_binary(test_root string, exit_line string) string { |
| 232 | os.rmdir_all(test_root) or {} |
| 233 | return prepare_test_executable(test_root, 'thirdparty/tcc/tcc.exe', exit_line) |
| 234 | } |
| 235 | |