v / cmd / tools / vpm / dependency_test.v
98 lines · 88 sloc · 3.46 KB · 9317f4b5078c90aee517b362099900f8f34c21ef
Raw
1import os
2import time
3import rand
4import v.vmod
5import test_utils { cmd_ok }
6
7const v = os.quoted_path(@VEXE)
8const test_path = os.join_path(os.vtmp_dir(), 'vpm_dependency_test_${rand.ulid()}')
9
10fn 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
21fn testsuite_end() {
22 dump(os.system('find ${test_path}'))
23 os.rmdir_all(test_path) or {}
24}
25
26fn 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.
35fn 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, 'spytheman.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, 'spytheman', '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
69fn 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, 'ttytm.webview')
73 expect_installing(@LOCATION, res.output, 'miniaudio')
74 // `ttytm.webview` is a registered VPM module, so it lands in `<vmodules_dir>/ttytm/webview`.
75 // `miniaudio` is unregistered, so it lands in `<vmodules_dir>/miniaudio`.
76 assert get_mod_name(os.join_path(test_path, 'ttytm', 'webview', 'v.mod')) == 'webview'
77 assert get_mod_name(os.join_path(test_path, 'miniaudio', 'v.mod')) == 'miniaudio'
78}
79
80fn test_install_with_recursive_dependencies() {
81 spawn fn () {
82 time.sleep(2 * time.minute)
83 eprintln('Timeout while testing installation with recursive dependencies.')
84 exit(1)
85 }()
86 cmd_ok(@LOCATION, '${v} install https://gitlab.com/tobealive/a')
87
88 // Test the installation of a module when passing its URL with the `.git` extension.
89 // One of the modules dependencies `https://gitlab.com/tobealive/c` has the
90 // `https://gitlab.com/tobealive/a` dependency without `.git`.
91 cmd_ok(@LOCATION, '${v} remove a b c')
92 cmd_ok(@LOCATION, '${v} install https://gitlab.com/tobealive/a.git')
93}
94
95fn expect_installing(location string, output string, what string) {
96 eprintln('>>> location: ${location}')
97 assert output.contains('Installing `${what}`') || output.contains('Scanning `${what}`'), output
98}
99