v2 / cmd / tools / vpm / install_local_test.v
110 lines · 101 sloc · 3.09 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1module main
2
3import os
4import rand
5import v.vmod
6import test_utils { cmd_ok }
7
8const test_path = os.join_path(os.vtmp_dir(), 'vpm_install_local_test_${rand.ulid()}')
9
10struct LocalInstallCase {
11 args string
12 module_name string
13 workdir string
14}
15
16fn testsuite_begin() {
17 test_utils.set_test_env(test_path)
18}
19
20fn testsuite_end() {
21 os.rmdir_all(test_path) or {}
22}
23
24fn test_install_from_local_git_repository_variants() {
25 repo_path := os.join_path(test_path, 'local_repo')
26 repo_dot_git_path := os.join_path(test_path, 'local_repo.git')
27 create_local_git_module(repo_path, 'local_repo_pkg')
28 create_local_git_module(repo_dot_git_path, 'local_repo_dot_git_pkg')
29
30 cases := [
31 LocalInstallCase{
32 args: os.quoted_path(repo_path)
33 module_name: 'local_repo_pkg'
34 },
35 LocalInstallCase{
36 args: file_url(repo_path)
37 module_name: 'local_repo_pkg'
38 },
39 LocalInstallCase{
40 args: '--git ${os.quoted_path(repo_path)}'
41 module_name: 'local_repo_pkg'
42 },
43 LocalInstallCase{
44 args: '--git ${file_url(repo_path)}'
45 module_name: 'local_repo_pkg'
46 },
47 LocalInstallCase{
48 args: os.file_name(repo_path)
49 module_name: 'local_repo_pkg'
50 workdir: test_path
51 },
52 LocalInstallCase{
53 args: os.quoted_path(repo_dot_git_path)
54 module_name: 'local_repo_dot_git_pkg'
55 },
56 LocalInstallCase{
57 args: file_url(repo_dot_git_path)
58 module_name: 'local_repo_dot_git_pkg'
59 },
60 LocalInstallCase{
61 args: '--git ${os.quoted_path(repo_dot_git_path)}'
62 module_name: 'local_repo_dot_git_pkg'
63 },
64 LocalInstallCase{
65 args: '--git ${file_url(repo_dot_git_path)}'
66 module_name: 'local_repo_dot_git_pkg'
67 },
68 LocalInstallCase{
69 args: os.file_name(repo_dot_git_path)
70 module_name: 'local_repo_dot_git_pkg'
71 workdir: test_path
72 },
73 ]
74 for i, c in cases {
75 vmodules_path := os.join_path(test_path, 'vmodules_case_${i}')
76 test_utils.set_test_env(vmodules_path)
77 cmd := '${vexe} install ${c.args}'
78 old_dir := os.getwd()
79 if c.workdir != '' {
80 os.chdir(c.workdir) or { panic(err) }
81 }
82 res := cmd_ok(@LOCATION, cmd)
83 if c.workdir != '' {
84 os.chdir(old_dir) or {}
85 }
86 assert res.output.contains('Installed `${c.module_name}`'), res.output
87 manifest := vmod.from_file(os.join_path(vmodules_path, c.module_name, 'v.mod')) or {
88 panic('Failed to parse v.mod for `${c.module_name}`. ${err}')
89 }
90 assert manifest.name == c.module_name
91 }
92}
93
94fn create_local_git_module(repo_path string, module_name string) {
95 os.mkdir_all(repo_path) or { panic(err) }
96 os.write_file(os.join_path(repo_path, 'v.mod'),
97 "Module{\n\tname: '${module_name}'\n\tversion: '0.0.1'\n}\n") or { panic(err) }
98 cmd_ok(@LOCATION, 'git init ${os.quoted_path(repo_path)}')
99 cmd_ok(@LOCATION, 'git -C ${os.quoted_path(repo_path)} add v.mod')
100 cmd_ok(@LOCATION,
101 'git -C ${os.quoted_path(repo_path)} -c user.email="[email protected]" -c user.name="V CI" commit -m "initial commit"')
102}
103
104fn file_url(path string) string {
105 mut normalized_path := path.replace('\\', '/')
106 if !normalized_path.starts_with('/') {
107 normalized_path = '/${normalized_path}'
108 }
109 return 'file://${normalized_path}'
110}
111