v4 / vlib / v3 / tests / posix_wait_header_codegen_test.v
67 lines · 58 sloc · 1.92 KB · 288feee4702b35beadb4037d02b96b5c8674156b
Raw
1import os
2
3const wait_header_vexe = @VEXE
4const wait_header_tests_dir = os.dir(@FILE)
5const wait_header_v3_dir = os.dir(wait_header_tests_dir)
6const wait_header_vlib_dir = os.dir(wait_header_v3_dir)
7const wait_header_v3_src = os.join_path(wait_header_v3_dir, 'v3.v')
8
9struct WaitHeaderProgram {
10 c_code string
11 out string
12}
13
14fn wait_header_build_v3() string {
15 pid := os.getpid()
16 v3_bin := os.join_path(os.temp_dir(), 'v3_wait_header_test_${pid}')
17 os.rm(v3_bin) or {}
18 build :=
19 os.execute('${wait_header_vexe} -gc none -path "${wait_header_vlib_dir}|@vlib|@vmodules" -o ${v3_bin} ${wait_header_v3_src}')
20 assert build.exit_code == 0, build.output
21 return v3_bin
22}
23
24fn wait_header_compile(v3_bin string, name string, source string) WaitHeaderProgram {
25 pid := os.getpid()
26 src := os.join_path(os.temp_dir(), 'v3_wait_header_${name}_${pid}.v')
27 out := os.join_path(os.temp_dir(), 'v3_wait_header_${name}_${pid}')
28 os.write_file(src, source) or { panic(err) }
29 os.rm(out) or {}
30 os.rm(out + '.c') or {}
31 compile := os.execute('${v3_bin} ${src} -b c -o ${out}')
32 assert compile.exit_code == 0, compile.output
33 return WaitHeaderProgram{
34 c_code: os.read_file(out + '.c') or { panic(err) }
35 out: out
36 }
37}
38
39fn test_os_import_emits_sys_wait_header() {
40 $if windows {
41 return
42 }
43 v3_bin := wait_header_build_v3()
44 with_os := wait_header_compile(v3_bin, 'with_os_execute', 'module main
45
46import os
47
48fn main() {
49 result := os.execute(\'/bin/sh -c "printf waitpid-ok"\')
50 assert result.exit_code == 0
51 println(result.output)
52}
53')
54 assert with_os.c_code.contains('#include <sys/wait.h>'), with_os.c_code
55 assert with_os.c_code.contains('waitpid('), with_os.c_code
56 run := os.execute(with_os.out)
57 assert run.exit_code == 0, run.output
58 assert run.output.trim_space() == 'waitpid-ok', run.output
59
60 hello := wait_header_compile(v3_bin, 'hello', "module main
61
62fn main() {
63 println('hello')
64}
65")
66 assert !hello.c_code.contains('#include <sys/wait.h>'), hello.c_code
67}
68