v / cmd / tools / translate_test.v
62 lines · 57 sloc · 2.18 KB · 5be2b1d9cd1f93efc8a776455991080648a51fb9
Raw
1import os
2
3const qvexe = os.quoted_path(@VEXE)
4
5fn test_translate_legacy_wrapper_flag_uses_wrapper_mode() {
6 test_dir := os.join_path(os.vtmp_dir(), 'translate_test_${os.getpid()}')
7 os.rmdir_all(test_dir) or {}
8 os.mkdir_all(test_dir)!
9 defer {
10 os.rmdir_all(test_dir) or {}
11 }
12 vmodules_dir := os.join_path(test_dir, 'vmodules')
13 compile_fake_c2v(vmodules_dir)!
14 sample_path := os.join_path(test_dir, 'usersapi.c')
15 args_out := os.join_path(test_dir, 'c2v_args.txt')
16 os.write_file(sample_path, 'int usersapi_get_number_of_users(void) { return 1; }\n')!
17 original_c2v_args_out := os.getenv('C2V_ARGS_OUT')
18 original_vmodules := os.getenv('VMODULES')
19 os.setenv('C2V_ARGS_OUT', args_out, true)
20 os.setenv('VMODULES', vmodules_dir, true)
21 defer {
22 restore_env('C2V_ARGS_OUT', original_c2v_args_out)
23 restore_env('VMODULES', original_vmodules)
24 }
25 mut process := os.new_process(@VEXE)
26 process.set_work_folder(@VEXEROOT)
27 process.set_args(['translate', '-wrapper', sample_path])
28 process.set_redirect_stdio()
29 process.wait()
30 stdout := process.stdout_slurp()
31 stderr := process.stderr_slurp()
32 exit_code := process.code
33 process.close()
34 assert exit_code == 0, 'stdout:\n${stdout}\nstderr:\n${stderr}'
35 args := os.read_file(args_out)!.split_into_lines()
36 assert args == ['wrapper', sample_path], args.str()
37}
38
39fn compile_fake_c2v(vmodules_dir string) ! {
40 c2v_dir := os.join_path(vmodules_dir, 'c2v')
41 os.mkdir_all(c2v_dir)!
42 fake_c2v_path := os.join_path(c2v_dir, 'fake_c2v.v')
43 c2v_bin := os.join_path(c2v_dir, 'c2v' + exe_suffix())
44 fake_c2v_source :=
45 ['import os', '', 'fn main() {', "\tout := os.getenv('C2V_ARGS_OUT')", "\tif out == '' {", "\t\teprintln('missing C2V_ARGS_OUT')", '\t\texit(1)', '\t}', "\tos.write_file(out, os.args[1..].join('\\n')) or {", '\t\teprintln(err)', '\t\texit(2)', '\t}', '}'].join('\n') +
46 '\n'
47 os.write_file(fake_c2v_path, fake_c2v_source)!
48 res := os.execute('${qvexe} -o ${os.quoted_path(c2v_bin)} ${os.quoted_path(fake_c2v_path)}')
49 assert res.exit_code == 0, res.output
50}
51
52fn exe_suffix() string {
53 return if os.user_os() == 'windows' { '.exe' } else { '' }
54}
55
56fn restore_env(name string, value string) {
57 if value == '' {
58 os.unsetenv(name)
59 } else {
60 os.setenv(name, value, true)
61 }
62}
63