v2 / vlib / v / tests / local_module_submodules_test.v
142 lines · 127 sloc · 5.01 KB · afcf82e759153b3155313c5842c5828d108e3bf5
Raw
1import os
2
3@[markused]
4const turn_off_vcolors = os.setenv('VCOLORS', 'never', true)
5
6const vexe = @VEXE
7
8fn temp_output_path(name string) string {
9 $if windows {
10 return os.join_path(os.vtmp_dir(), '${name}.exe')
11 }
12 return os.join_path(os.vtmp_dir(), name)
13}
14
15fn project_path() string {
16 return os.join_path(os.real_path(os.vtmp_dir()), 'local_module_submodules')
17}
18
19fn issue_24649_project_path() string {
20 return os.join_path(os.real_path(os.vtmp_dir()), 'issue_24649_local_module_submodules')
21}
22
23fn write_local_module_submodules_project() {
24 basepath := project_path()
25 os.rmdir_all(basepath) or {}
26 os.mkdir_all(os.join_path(basepath, 'confy', 'marshalers')) or { panic(err) }
27 os.write_file(os.join_path(basepath, 'main.v'),
28 ['module main', '', 'import confy', 'import confy.marshalers', '', 'fn main() {', '\tmut marshaler := marshalers.JSON{}', '\tload(mut marshaler)', '}', '', 'fn load(mut marshaler confy.Marshaler) {', '\tmarshaler.load()', '}'].join('\n') +
29 '\n') or { panic(err) }
30 os.write_file(os.join_path(basepath, 'confy', 'confy.v'),
31 ['module confy', '', 'pub interface Marshaler {', '\tmut:', '\t\tload()', '}'].join('\n') +
32 '\n') or { panic(err) }
33 os.write_file(os.join_path(basepath, 'confy', 'marshalers', 'json.v'),
34 ['module marshalers', '', 'pub struct JSON {}', '', 'pub fn (mut j JSON) load() {}'].join('\n') +
35 '\n') or { panic(err) }
36}
37
38fn write_issue_24649_project() {
39 basepath := issue_24649_project_path()
40 os.rmdir_all(basepath) or {}
41 os.mkdir_all(os.join_path(basepath, 'my_math', 'my_integer')) or { panic(err) }
42 os.write_file(os.join_path(basepath, 'running_my_math.v'),
43 ['module main', '', 'import my_math', 'import my_math.my_integer', '', 'fn main() {', '\ta := 34.5', '\tb := 13.2', '', "\tprintln('La suma de a y b es \${my_math.addition(a, b)}')", '', "\tprintln('La suma entera de a y b es \${my_integer.addition(a, b)}')", '}'].join('\n') +
44 '\n') or { panic(err) }
45 os.write_file(os.join_path(basepath, 'my_math', 'floating.v'),
46 ['module my_math', '', 'pub fn addition(x f64, y f64) f64 {', '\treturn x +
47 y', '}'].join('\n') +
48 '\n') or { panic(err) }
49 os.write_file(os.join_path(basepath, 'my_math', 'my_integer', 'integer.v'),
50 ['module my_integer', '', 'pub fn addition(x f64, y f64) int {', '\treturn int(x +
51 y)', '}'].join('\n') +
52 '\n') or { panic(err) }
53}
54
55fn compile_local_module_submodules(target string, out_name string) os.Result {
56 basepath := project_path()
57 write_local_module_submodules_project()
58 old_wd := os.getwd()
59 os.chdir(basepath) or { panic(err) }
60 defer {
61 os.chdir(old_wd) or {}
62 os.rmdir_all(basepath) or {}
63 }
64 return os.execute('${os.quoted_path(vexe)} -o ${os.quoted_path(out_name)} ${os.quoted_path(target)}')
65}
66
67fn compile_issue_24649_project(target string, out_name string, cwd string) os.Result {
68 basepath := issue_24649_project_path()
69 write_issue_24649_project()
70 old_wd := os.getwd()
71 os.chdir(cwd) or { panic(err) }
72 defer {
73 os.chdir(old_wd) or {}
74 os.rmdir_all(basepath) or {}
75 }
76 return os.execute('${os.quoted_path(vexe)} -o ${os.quoted_path(out_name)} ${os.quoted_path(target)}')
77}
78
79fn test_local_module_submodules_compile_from_main_file() {
80 out_name := temp_output_path('local_module_submodules_main')
81 os.rm(out_name) or {}
82 defer {
83 os.rm(out_name) or {}
84 }
85 res := compile_local_module_submodules('main.v', out_name)
86 assert res.exit_code == 0, res.output
87}
88
89fn test_local_module_submodules_compile_from_project_dir() {
90 out_name := temp_output_path('local_module_submodules_dir')
91 os.rm(out_name) or {}
92 defer {
93 os.rm(out_name) or {}
94 }
95 res := compile_local_module_submodules('.', out_name)
96 assert res.exit_code == 0, res.output
97}
98
99fn test_issue_24649_submodules_compile_from_main_file() {
100 basepath := issue_24649_project_path()
101 out_name := temp_output_path('issue_24649_local_module_submodules_main')
102 os.rm(out_name) or {}
103 defer {
104 os.rm(out_name) or {}
105 }
106 res := compile_issue_24649_project('running_my_math.v', out_name, basepath)
107 assert res.exit_code == 0, res.output
108}
109
110fn test_issue_24649_submodules_compile_from_dot_slash_main_file() {
111 basepath := issue_24649_project_path()
112 out_name := temp_output_path('issue_24649_local_module_submodules_dot_main')
113 os.rm(out_name) or {}
114 defer {
115 os.rm(out_name) or {}
116 }
117 res := compile_issue_24649_project('./running_my_math.v', out_name, basepath)
118 assert res.exit_code == 0, res.output
119}
120
121fn test_issue_24649_submodules_compile_from_project_dir() {
122 basepath := issue_24649_project_path()
123 out_name := temp_output_path('issue_24649_local_module_submodules_dir')
124 os.rm(out_name) or {}
125 defer {
126 os.rm(out_name) or {}
127 }
128 res := compile_issue_24649_project('.', out_name, basepath)
129 assert res.exit_code == 0, res.output
130}
131
132fn test_issue_24649_submodules_compile_from_absolute_main_file() {
133 basepath := issue_24649_project_path()
134 out_name := temp_output_path('issue_24649_local_module_submodules_abs_main')
135 os.rm(out_name) or {}
136 defer {
137 os.rm(out_name) or {}
138 }
139 target := os.join_path(basepath, 'running_my_math.v')
140 res := compile_issue_24649_project(target, out_name, os.dir(basepath))
141 assert res.exit_code == 0, res.output
142}
143