| 1 | import os |
| 2 | |
| 3 | @[markused] |
| 4 | const turn_off_vcolors = os.setenv('VCOLORS', 'never', true) |
| 5 | |
| 6 | const issue_20147_vexe = @VEXE |
| 7 | |
| 8 | fn issue_20147_env_snapshot(name string) (string, bool) { |
| 9 | val := os.getenv_opt(name) or { return '', false } |
| 10 | return val, true |
| 11 | } |
| 12 | |
| 13 | fn 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 | |
| 21 | fn issue_20147_workspace() string { |
| 22 | return os.join_path(os.vtmp_dir(), 'issue_20147_vmodules_package_compile') |
| 23 | } |
| 24 | |
| 25 | fn issue_20147_module_root() string { |
| 26 | return os.join_path(issue_20147_workspace(), '.vmodules', 'msgpack') |
| 27 | } |
| 28 | |
| 29 | fn issue_20147_write_file(path string, contents string) { |
| 30 | os.write_file(path, contents) or { panic(err) } |
| 31 | } |
| 32 | |
| 33 | fn 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 | |
| 64 | fn 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 | |
| 79 | // Regression test for https://github.com/vlang/v/issues/27391 : |
| 80 | // modules installed as symlinks inside a `.vmodules` namespace folder |
| 81 | // (e.g. `.vmodules/einar_hjortdal/luuid -> /real/luuid`) were resolved to |
| 82 | // their real path via os.real_path and then rejected as belonging to a |
| 83 | // different v.mod project, so `import einar_hjortdal.luuid` could not be found. |
| 84 | fn issue_27391_workspace() string { |
| 85 | return os.join_path(os.vtmp_dir(), 'issue_27391_symlinked_vmodules') |
| 86 | } |
| 87 | |
| 88 | fn issue_27391_write_project() ! { |
| 89 | workspace := issue_27391_workspace() |
| 90 | vmodules_ns := os.join_path(workspace, '.vmodules', 'einar_hjortdal') |
| 91 | real_luuid := os.join_path(workspace, 'real', 'luuid') |
| 92 | real_firebird := os.join_path(workspace, 'real', 'firebird') |
| 93 | app_root := os.join_path(workspace, 'app') |
| 94 | luuid_vmod := ['Module {', "\tname: 'luuid'", '}'].join_lines() + '\n' |
| 95 | luuid_contents := |
| 96 | ['module luuid', '', "pub fn hello() string { return 'luuid-ok' }"].join_lines() + '\n' |
| 97 | firebird_vmod := ['Module {', "\tname: 'firebird'", '}'].join_lines() + '\n' |
| 98 | firebird_contents := |
| 99 | ['module firebird', '', 'import einar_hjortdal.luuid', '', 'pub fn run() string { return luuid.hello() }'].join_lines() + |
| 100 | '\n' |
| 101 | app_vmod := ['Module {', "\tname: 'app'", '}'].join_lines() + '\n' |
| 102 | app_contents := |
| 103 | ['module main', '', 'import einar_hjortdal.luuid', 'import einar_hjortdal.firebird', '', 'fn main() {', '\tprintln(luuid.hello())', '\tprintln(firebird.run())', '}'].join_lines() + |
| 104 | '\n' |
| 105 | os.rmdir_all(workspace) or {} |
| 106 | os.mkdir_all(vmodules_ns)! |
| 107 | os.mkdir_all(real_luuid)! |
| 108 | os.mkdir_all(real_firebird)! |
| 109 | os.mkdir_all(app_root)! |
| 110 | issue_20147_write_file(os.join_path(real_luuid, 'v.mod'), luuid_vmod) |
| 111 | issue_20147_write_file(os.join_path(real_luuid, 'luuid.v'), luuid_contents) |
| 112 | issue_20147_write_file(os.join_path(real_firebird, 'v.mod'), firebird_vmod) |
| 113 | issue_20147_write_file(os.join_path(real_firebird, 'firebird.v'), firebird_contents) |
| 114 | // install both real modules as symlinks inside the `einar_hjortdal` namespace |
| 115 | os.symlink(real_luuid, os.join_path(vmodules_ns, 'luuid'))! |
| 116 | os.symlink(real_firebird, os.join_path(vmodules_ns, 'firebird'))! |
| 117 | issue_20147_write_file(os.join_path(app_root, 'v.mod'), app_vmod) |
| 118 | issue_20147_write_file(os.join_path(app_root, 'main.v'), app_contents) |
| 119 | } |
| 120 | |
| 121 | fn test_issue_27391_symlinked_namespaced_vmodules_import_compiles() { |
| 122 | issue_27391_write_project() or { |
| 123 | $if windows { |
| 124 | eprintln('skipping symlinked vmodules import regression test: ${err}') |
| 125 | return |
| 126 | } $else { |
| 127 | panic(err) |
| 128 | } |
| 129 | } |
| 130 | old_vmodules, had_vmodules := issue_20147_env_snapshot('VMODULES') |
| 131 | os.setenv('VMODULES', os.join_path(issue_27391_workspace(), '.vmodules'), true) |
| 132 | defer { |
| 133 | issue_20147_restore_env('VMODULES', old_vmodules, had_vmodules) |
| 134 | os.rmdir_all(issue_27391_workspace()) or {} |
| 135 | } |
| 136 | main_file := os.join_path(issue_27391_workspace(), 'app', 'main.v') |
| 137 | res := os.execute('${os.quoted_path(issue_20147_vexe)} run ${os.quoted_path(main_file)}') |
| 138 | assert res.exit_code == 0, res.output |
| 139 | assert res.output.trim_space() == 'luuid-ok\nluuid-ok', res.output |
| 140 | } |
| 141 | |