v2 / vlib / v / tests / vmodules_package_compile_regression_test.v
77 lines · 68 sloc · 3.18 KB · 1ccdcb8f3b7ce729de0b147dff5ecff6389cc1e0
Raw
1import os
2
3@[markused]
4const turn_off_vcolors = os.setenv('VCOLORS', 'never', true)
5
6const issue_20147_vexe = @VEXE
7
8fn issue_20147_env_snapshot(name string) (string, bool) {
9 val := os.getenv_opt(name) or { return '', false }
10 return val, true
11}
12
13fn issue_20147_restore_env(name string, value string, existed bool) {
14 if existed {
15 os.setenv(name, value, true)
16 } else {
17 os.unsetenv(name)
18 }
19}
20
21fn issue_20147_workspace() string {
22 return os.join_path(os.vtmp_dir(), 'issue_20147_vmodules_package_compile')
23}
24
25fn issue_20147_module_root() string {
26 return os.join_path(issue_20147_workspace(), '.vmodules', 'msgpack')
27}
28
29fn issue_20147_write_file(path string, contents string) {
30 os.write_file(path, contents) or { panic(err) }
31}
32
33fn issue_20147_write_project() {
34 basepath := issue_20147_module_root()
35 vmod_contents := ['Module {', "\tname: 'msgpack'", '}'].join_lines() + '\n'
36 config_contents :=
37 ['module config', '', 'pub struct Config {}', '', 'pub fn default_config() Config {', '\treturn Config{}', '}'].join_lines() +
38 '\n'
39 decoder_contents := ['module decoder', '', 'pub struct Decoder {}'].join_lines() + '\n'
40 to_contents := ['module msgpack.to', '', 'pub fn new_decoder(src []u8) {}'].join_lines() + '\n'
41 msgpack_contents :=
42 ['module msgpack', '', "pub const description = 'an empty module, used as a placeholder, for other modules'"].join_lines() +
43 '\n'
44 test_contents := ['fn test_a() {', '\tassert true', '}'].join_lines()
45 import_config_test_contents := ['import msgpack.config', '', test_contents].join_lines() + '\n'
46 import_decoder_test_contents := ['import decoder', '', test_contents].join_lines() + '\n'
47 import_to_test_contents := ['import msgpack.config', '', test_contents].join_lines() + '\n'
48 os.rmdir_all(issue_20147_workspace()) or {}
49 os.mkdir_all(os.join_path(basepath, 'config')) or { panic(err) }
50 os.mkdir_all(os.join_path(basepath, 'decoder')) or { panic(err) }
51 os.mkdir_all(os.join_path(basepath, 'to')) or { panic(err) }
52 issue_20147_write_file(os.join_path(basepath, 'v.mod'), vmod_contents)
53 issue_20147_write_file(os.join_path(basepath, 'config', 'config.v'), config_contents)
54 issue_20147_write_file(os.join_path(basepath, 'decoder', 'decoder.v'), decoder_contents)
55 issue_20147_write_file(os.join_path(basepath, 'to', 'to.v'), to_contents)
56 issue_20147_write_file(os.join_path(basepath, 'msgpack.v'), msgpack_contents)
57 issue_20147_write_file(os.join_path(basepath, 'import_config_test.v'),
58 import_config_test_contents)
59 issue_20147_write_file(os.join_path(basepath, 'import_decoder_test.v'),
60 import_decoder_test_contents)
61 issue_20147_write_file(os.join_path(basepath, 'import_to_test.v'), import_to_test_contents)
62}
63
64fn test_issue_20147_vmodules_package_tests_compile() {
65 issue_20147_write_project()
66 old_wd := os.getwd()
67 old_vmodules, had_vmodules := issue_20147_env_snapshot('VMODULES')
68 os.setenv('VMODULES', os.join_path(issue_20147_workspace(), '.vmodules'), true)
69 os.chdir(issue_20147_module_root()) or { panic(err) }
70 defer {
71 os.chdir(old_wd) or { panic(err) }
72 issue_20147_restore_env('VMODULES', old_vmodules, had_vmodules)
73 os.rmdir_all(issue_20147_workspace()) or {}
74 }
75 res := os.execute('${os.quoted_path(issue_20147_vexe)} test .')
76 assert res.exit_code == 0, res.output
77}
78