| 1 | import os |
| 2 | import time |
| 3 | import rand |
| 4 | import v.vmod |
| 5 | import test_utils { cmd_ok } |
| 6 | |
| 7 | const v = os.quoted_path(@VEXE) |
| 8 | const test_path = os.join_path(os.vtmp_dir(), 'vpm_dependency_test_${rand.ulid()}') |
| 9 | |
| 10 | fn testsuite_begin() { |
| 11 | $if !network ? { |
| 12 | eprintln('> skipping ${@FILE}, when `-d network` is missing') |
| 13 | exit(0) |
| 14 | } |
| 15 | dump(test_path) |
| 16 | test_utils.set_test_env(test_path) |
| 17 | os.mkdir_all(test_path) or {} |
| 18 | os.chdir(test_path)! |
| 19 | } |
| 20 | |
| 21 | fn testsuite_end() { |
| 22 | dump(os.system('find ${test_path}')) |
| 23 | os.rmdir_all(test_path) or {} |
| 24 | } |
| 25 | |
| 26 | fn get_mod_name(path string) string { |
| 27 | mod := vmod.from_file(path) or { |
| 28 | eprintln(err) |
| 29 | return '' |
| 30 | } |
| 31 | return mod.name |
| 32 | } |
| 33 | |
| 34 | // Case: running `v install` without specifying modules in a V project directory. |
| 35 | fn test_install_dependencies_in_module_dir() { |
| 36 | mod := 'my_module' |
| 37 | mod_path := os.join_path(test_path, mod) |
| 38 | os.mkdir(mod_path)! |
| 39 | os.chdir(mod_path)! |
| 40 | // Create a v.mod file that lists dependencies. |
| 41 | vmod_path := os.join_path(mod_path, 'v.mod') |
| 42 | vmod_contents := "Module { |
| 43 | name: '${mod}' |
| 44 | description: '' |
| 45 | version: '0.0.0' |
| 46 | license: 'MIT' |
| 47 | dependencies: ['markdown', 'pcre', 'https://github.com/spytheman/vtray'] |
| 48 | }" |
| 49 | os.write_file(vmod_path, vmod_contents)! |
| 50 | v_mod := vmod.from_file(vmod_path) or { |
| 51 | assert false, err.msg() |
| 52 | return |
| 53 | } |
| 54 | assert v_mod.dependencies == ['markdown', 'pcre', 'https://github.com/spytheman/vtray'] |
| 55 | // Run `v install` |
| 56 | mut res := cmd_ok(@LOCATION, '${v} install --once') |
| 57 | assert res.output.contains('Detected v.mod file inside the project directory. Using it...'), res.output |
| 58 | expect_installing(@LOCATION, res.output, 'markdown') |
| 59 | expect_installing(@LOCATION, res.output, 'pcre') |
| 60 | expect_installing(@LOCATION, res.output, 'vtray') |
| 61 | |
| 62 | assert get_mod_name(os.join_path(test_path, 'markdown', 'v.mod')) == 'markdown' |
| 63 | assert get_mod_name(os.join_path(test_path, 'pcre', 'v.mod')) == 'pcre' |
| 64 | assert get_mod_name(os.join_path(test_path, 'vtray', 'v.mod')) == 'vtray' |
| 65 | res = cmd_ok(@LOCATION, '${v} install --once') |
| 66 | assert res.output.contains('All modules are already installed.'), res.output |
| 67 | } |
| 68 | |
| 69 | fn test_resolve_external_dependencies_during_module_install() { |
| 70 | res := cmd_ok(@LOCATION, '${v} install -v https://github.com/ttytm/emoji-mart-desktop') |
| 71 | assert res.output.contains('Found 2 dependencies'), res.output |
| 72 | expect_installing(@LOCATION, res.output, 'webview') |
| 73 | expect_installing(@LOCATION, res.output, 'miniaudio') |
| 74 | // The external dependencies should have been installed to `<vmodules_dir>/<dependency_name>` |
| 75 | assert get_mod_name(os.join_path(test_path, 'webview', 'v.mod')) == 'webview' |
| 76 | assert get_mod_name(os.join_path(test_path, 'miniaudio', 'v.mod')) == 'miniaudio' |
| 77 | } |
| 78 | |
| 79 | fn test_install_with_recursive_dependencies() { |
| 80 | spawn fn () { |
| 81 | time.sleep(2 * time.minute) |
| 82 | eprintln('Timeout while testing installation with recursive dependencies.') |
| 83 | exit(1) |
| 84 | }() |
| 85 | cmd_ok(@LOCATION, '${v} install https://gitlab.com/tobealive/a') |
| 86 | |
| 87 | // Test the installation of a module when passing its URL with the `.git` extension. |
| 88 | // One of the modules dependencies `https://gitlab.com/tobealive/c` has the |
| 89 | // `https://gitlab.com/tobealive/a` dependency without `.git`. |
| 90 | cmd_ok(@LOCATION, '${v} remove a b c') |
| 91 | cmd_ok(@LOCATION, '${v} install https://gitlab.com/tobealive/a.git') |
| 92 | } |
| 93 | |
| 94 | fn expect_installing(location string, output string, what string) { |
| 95 | eprintln('>>> location: ${location}') |
| 96 | assert output.contains('Installing `${what}`') || output.contains('Scanning `${what}`'), output |
| 97 | } |
| 98 | |