vxx2 / vlib / v / pref / default_tcc_compiler_test.v
320 lines · 302 sloc · 8.75 KB · 0b1ee86f742567f2fcb3c534b0a49a6dddba36ab
Raw
1module pref
2
3import os
4
5fn 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
17fn 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
29fn 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
42fn 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
67fn 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
91fn 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
113fn 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
137fn test_try_to_use_tcc_by_default_skips_bundled_tcc_on_macos() {
138 $if !macos {
139 return
140 }
141 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test')
142 prepare_test_tcc_binary(test_root, 'exit 0')
143 fake_vexe := os.join_path(test_root, 'v')
144 old_vexe := os.getenv('VEXE')
145 os.setenv('VEXE', fake_vexe, true)
146 defer {
147 if old_vexe == '' {
148 os.unsetenv('VEXE')
149 } else {
150 os.setenv('VEXE', old_vexe, true)
151 }
152 os.rmdir_all(test_root) or {}
153 }
154 mut prefs := Preferences{
155 vroot: test_root
156 out_name: os.join_path(test_root, 'v')
157 }
158 prefs.try_to_use_tcc_by_default()
159 assert prefs.ccompiler == ''
160}
161
162fn test_usable_system_tcc_compiler_prefers_termux_tcc_from_path() {
163 $if windows {
164 return
165 }
166 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test')
167 system_tcc := prepare_test_executable(test_root, 'bin/tcc', 'exit 0')
168 old_path := os.getenv('PATH')
169 old_termux_version := os.getenv('TERMUX_VERSION')
170 os.setenv('PATH', os.dir(system_tcc), true)
171 os.setenv('TERMUX_VERSION', '0.118.0', true)
172 defer {
173 os.setenv('PATH', old_path, true)
174 if old_termux_version == '' {
175 os.unsetenv('TERMUX_VERSION')
176 } else {
177 os.setenv('TERMUX_VERSION', old_termux_version, true)
178 }
179 os.rmdir_all(test_root) or {}
180 }
181 assert usable_system_tcc_compiler() == system_tcc
182}
183
184fn test_default_tcc_compiler_uses_system_tcc_on_termux_when_bundled_is_missing() {
185 $if windows {
186 return
187 }
188 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test')
189 fake_vexe := os.join_path(test_root, 'v')
190 system_tcc := prepare_test_executable(test_root, 'bin/tcc', 'exit 0')
191 old_vexe := os.getenv('VEXE')
192 old_path := os.getenv('PATH')
193 old_termux_version := os.getenv('TERMUX_VERSION')
194 os.setenv('VEXE', fake_vexe, true)
195 os.setenv('PATH', os.dir(system_tcc), true)
196 os.setenv('TERMUX_VERSION', '0.118.0', true)
197 defer {
198 if old_vexe == '' {
199 os.unsetenv('VEXE')
200 } else {
201 os.setenv('VEXE', old_vexe, true)
202 }
203 os.setenv('PATH', old_path, true)
204 if old_termux_version == '' {
205 os.unsetenv('TERMUX_VERSION')
206 } else {
207 os.setenv('TERMUX_VERSION', old_termux_version, true)
208 }
209 os.rmdir_all(test_root) or {}
210 }
211 assert default_tcc_compiler() == system_tcc
212}
213
214fn test_try_to_use_tcc_by_default_resolves_explicit_tcc_to_system_tcc_on_termux() {
215 $if windows {
216 return
217 }
218 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_tcc_compiler_test')
219 fake_vexe := os.join_path(test_root, 'v')
220 system_tcc := prepare_test_executable(test_root, 'bin/tcc', 'exit 0')
221 old_vexe := os.getenv('VEXE')
222 old_path := os.getenv('PATH')
223 old_termux_version := os.getenv('TERMUX_VERSION')
224 os.setenv('VEXE', fake_vexe, true)
225 os.setenv('PATH', os.dir(system_tcc), true)
226 os.setenv('TERMUX_VERSION', '0.118.0', true)
227 defer {
228 if old_vexe == '' {
229 os.unsetenv('VEXE')
230 } else {
231 os.setenv('VEXE', old_vexe, true)
232 }
233 os.setenv('PATH', old_path, true)
234 if old_termux_version == '' {
235 os.unsetenv('TERMUX_VERSION')
236 } else {
237 os.setenv('TERMUX_VERSION', old_termux_version, true)
238 }
239 os.rmdir_all(test_root) or {}
240 }
241 mut prefs := Preferences{
242 ccompiler: 'tcc'
243 }
244 prefs.try_to_use_tcc_by_default()
245 assert prefs.ccompiler == system_tcc
246}
247
248fn test_windows_default_c_compiler_prefers_gcc_when_available() {
249 $if windows {
250 return
251 }
252 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_c_compiler_test')
253 prepare_test_tcc_binary(test_root, 'exit 0')
254 fake_gcc := prepare_test_executable(test_root, 'bin/gcc', 'exit 0')
255 old_path := os.getenv('PATH')
256 os.setenv('PATH', os.dir(fake_gcc), true)
257 defer {
258 os.setenv('PATH', old_path, true)
259 os.rmdir_all(test_root) or {}
260 }
261 assert windows_default_c_compiler(test_root) == 'gcc'
262}
263
264fn test_windows_default_c_compiler_falls_back_to_bundled_tcc_when_no_gcc() {
265 $if windows {
266 return
267 }
268 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_c_compiler_test')
269 tcc_path := prepare_test_tcc_binary(test_root, 'exit 0')
270 old_path := os.getenv('PATH')
271 os.setenv('PATH', os.join_path(test_root, 'no_such_path'), true)
272 defer {
273 os.setenv('PATH', old_path, true)
274 os.rmdir_all(test_root) or {}
275 }
276 assert windows_default_c_compiler(test_root) == tcc_path
277}
278
279fn test_windows_default_c_compiler_keeps_gcc_when_bundled_tcc_is_broken() {
280 $if windows {
281 return
282 }
283 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_c_compiler_test')
284 prepare_test_tcc_binary(test_root, 'exit 1')
285 old_path := os.getenv('PATH')
286 os.setenv('PATH', os.join_path(test_root, 'no_such_path'), true)
287 defer {
288 os.setenv('PATH', old_path, true)
289 os.rmdir_all(test_root) or {}
290 }
291 assert windows_default_c_compiler(test_root) == 'gcc'
292}
293
294fn test_windows_default_c_compiler_keeps_gcc_when_neither_is_available() {
295 $if windows {
296 return
297 }
298 test_root := os.join_path(os.vtmp_dir(), 'v_pref_default_c_compiler_test')
299 os.rmdir_all(test_root) or {}
300 old_path := os.getenv('PATH')
301 os.setenv('PATH', os.join_path(test_root, 'no_such_path'), true)
302 defer {
303 os.setenv('PATH', old_path, true)
304 os.rmdir_all(test_root) or {}
305 }
306 assert windows_default_c_compiler(test_root) == 'gcc'
307}
308
309fn prepare_test_executable(test_root string, relative_path string, exit_line string) string {
310 path := os.join_path(test_root, relative_path)
311 os.mkdir_all(os.dir(path)) or { panic(err) }
312 os.write_file(path, '#!/bin/sh\n${exit_line}\n') or { panic(err) }
313 os.chmod(path, 0o700) or { panic(err) }
314 return path
315}
316
317fn prepare_test_tcc_binary(test_root string, exit_line string) string {
318 os.rmdir_all(test_root) or {}
319 return prepare_test_executable(test_root, 'thirdparty/tcc/tcc.exe', exit_line)
320}
321