Skip to content

Commit 109fc22

Browse files
authored
tools.vpm: simplify, use more stdlib functionality part2, add more tests (#19692)
1 parent c6d3a23 commit 109fc22

File tree

2 files changed

+57
-28
lines changed

2 files changed

+57
-28
lines changed

cmd/tools/vpm/install_test.v

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,30 @@ import v.vmod
55
// The following will result in e.g. `$VTMP/tsession_7fe8e93bd740_1612958707536/test-vmodules/`.
66
const test_path = os.join_path(os.vtmp_dir(), 'test-vmodules')
77

8-
fn prep_test_path() {
8+
fn testsuite_begin() {
99
os.setenv('VMODULES', test_path, true)
1010
}
1111

12+
fn testsuite_end() {
13+
os.rmdir_all(test_path) or {}
14+
}
15+
1216
fn test_install_from_git_url() {
13-
prep_test_path()
1417
res := os.execute(@VEXE + ' install https://github.com/vlang/markdown')
18+
assert res.exit_code == 0, res.output
1519
mod := vmod.from_file(os.join_path(test_path, 'markdown', 'v.mod')) or {
1620
assert false, err.str()
1721
return
1822
}
1923
assert mod.name == 'markdown'
2024
assert mod.dependencies == []string{}
25+
assert res.output.contains('Installing module "markdown" from "https://github.com/vlang/markdown')
26+
assert res.output.contains('Relocating module from "vlang/markdown" to "markdown"')
2127
}
2228

2329
fn test_install_from_vpm_ident() {
24-
prep_test_path()
2530
res := os.execute(@VEXE + ' install nedpals.args')
31+
assert res.exit_code == 0, res.output
2632
mod := vmod.from_file(os.join_path(test_path, 'nedpals', 'args', 'v.mod')) or {
2733
assert false, err.str()
2834
return
@@ -32,12 +38,38 @@ fn test_install_from_vpm_ident() {
3238
}
3339

3440
fn test_install_from_vpm_short_ident() {
35-
prep_test_path()
3641
res := os.execute(@VEXE + ' install pcre')
42+
assert res.exit_code == 0, res.output
3743
mod := vmod.from_file(os.join_path(test_path, 'pcre', 'v.mod')) or {
3844
assert false, err.str()
3945
return
4046
}
4147
assert mod.name == 'pcre'
4248
assert mod.description == 'A simple regex library for V.'
4349
}
50+
51+
fn test_install_already_existant() {
52+
// Skip on windows for now due permission errors with rmdir.
53+
$if windows {
54+
return
55+
}
56+
mod_url := 'https://github.com/vlang/markdown'
57+
mut res := os.execute(@VEXE + ' install ${mod_url}')
58+
assert res.exit_code == 0, res.output
59+
res = os.execute(@VEXE + ' install ${mod_url}')
60+
assert res.exit_code == 0, res.output
61+
mod := vmod.from_file(os.join_path(test_path, 'markdown', 'v.mod')) or {
62+
assert false, err.str()
63+
return
64+
}
65+
assert mod.name == 'markdown'
66+
assert mod.dependencies == []string{}
67+
assert res.output.contains('already exists')
68+
}
69+
70+
fn test_missing_repo_name_in_url() {
71+
incomplete_url := 'https://github.com/vlang'
72+
res := os.execute(@VEXE + ' install ${incomplete_url}')
73+
assert res.exit_code == 1
74+
assert res.output.trim_space() == 'Errors while retrieving module name for: "${incomplete_url}"'
75+
}

cmd/tools/vpm/vpm.v

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,48 +258,45 @@ fn ensure_vcs_is_installed(vcs string) bool {
258258
return res
259259
}
260260

261-
fn vpm_install_from_vcs(module_names []string, vcs_key string) {
261+
fn vpm_install_from_vcs(modules []string, vcs_key string) {
262262
mut errors := 0
263-
for n in module_names {
264-
url := n.trim_space()
265-
266-
first_cut_pos := url.last_index('/') or {
263+
for raw_url in modules {
264+
url := urllib.parse(raw_url) or {
267265
errors++
268-
eprintln('Errors while retrieving name for module "${url}" :')
269-
eprintln(err)
266+
eprintln('Errors while parsing module url "${raw_url}" : ${err}')
270267
continue
271268
}
272269

273-
mod_name := url.substr(first_cut_pos + 1, url.len)
274-
275-
second_cut_pos := url.substr(0, first_cut_pos).last_index('/') or {
270+
// Module identifier based on URL.
271+
// E.g.: `https://github.com/owner/awesome-v-project` -> `owner/awesome_v_project`
272+
mut ident := url.path#[1..].replace('-', '_')
273+
owner, repo_name := ident.split_once('/') or {
276274
errors++
277-
eprintln('Errors while retrieving name for module "${url}" :')
278-
eprintln(err)
275+
eprintln('Errors while retrieving module name for: "${url}"')
279276
continue
280277
}
278+
mut final_module_path := os.real_path(os.join_path(settings.vmodules_path, owner.to_lower(),
279+
repo_name.to_lower()))
281280

282-
repo_name := url.substr(second_cut_pos + 1, first_cut_pos)
283-
mut name := os.join_path(repo_name, mod_name)
284-
mod_name_as_path := name.replace('-', '_').to_lower()
285-
mut final_module_path := os.real_path(os.join_path(settings.vmodules_path, mod_name_as_path))
286281
if os.exists(final_module_path) {
287-
vpm_update([name.replace('-', '_')])
282+
vpm_update([ident])
288283
continue
289284
}
285+
290286
if !ensure_vcs_is_installed(vcs_key) {
291287
errors++
292288
eprintln('VPM needs `${vcs_key}` to be installed.')
293289
continue
294290
}
295-
println('Installing module "${name}" from "${url}" to "${final_module_path}" ...')
291+
292+
println('Installing module "${repo_name}" from "${url}" to "${final_module_path}" ...')
296293
vcs_install_cmd := '${vcs_key} ${supported_vcs_install_cmds[vcs_key]}'
297294
cmd := '${vcs_install_cmd} "${url}" "${final_module_path}"'
298295
verbose_println(' command: ${cmd}')
299296
cmdres := os.execute(cmd)
300297
if cmdres.exit_code != 0 {
301298
errors++
302-
eprintln('Failed installing module "${name}" to "${final_module_path}" .')
299+
eprintln('Failed installing module "${repo_name}" to "${final_module_path}" .')
303300
print_failed_cmd(cmd, cmdres)
304301
continue
305302
}
@@ -311,7 +308,7 @@ fn vpm_install_from_vcs(module_names []string, vcs_key string) {
311308
}
312309
minfo := mod_name_info(manifest.name)
313310
if final_module_path != minfo.final_module_path {
314-
println('Relocating module from "${name}" to "${manifest.name}" ( "${minfo.final_module_path}" ) ...')
311+
println('Relocating module from "${ident}" to "${manifest.name}" ("${minfo.final_module_path}") ...')
315312
if os.exists(minfo.final_module_path) {
316313
eprintln('Warning module "${minfo.final_module_path}" already exists!')
317314
eprintln('Removing module "${minfo.final_module_path}" ...')
@@ -324,7 +321,7 @@ fn vpm_install_from_vcs(module_names []string, vcs_key string) {
324321
}
325322
os.mv(final_module_path, minfo.final_module_path) or {
326323
errors++
327-
eprintln('Errors while relocating module "${name}" :')
324+
eprintln('Errors while relocating module "${repo_name}" :')
328325
eprintln(err)
329326
os.rmdir_all(final_module_path) or {
330327
errors++
@@ -334,7 +331,7 @@ fn vpm_install_from_vcs(module_names []string, vcs_key string) {
334331
}
335332
continue
336333
}
337-
println('Module "${name}" relocated to "${manifest.name}" successfully.')
334+
println('Module "${repo_name}" relocated to "${manifest.name}" successfully.')
338335
publisher_dir := final_module_path.all_before_last(os.path_separator)
339336
if os.is_dir_empty(publisher_dir) {
340337
os.rmdir(publisher_dir) or {
@@ -345,9 +342,9 @@ fn vpm_install_from_vcs(module_names []string, vcs_key string) {
345342
}
346343
final_module_path = minfo.final_module_path
347344
}
348-
name = manifest.name
345+
ident = manifest.name
349346
}
350-
resolve_dependencies(name, final_module_path, module_names)
347+
resolve_dependencies(ident, final_module_path, modules)
351348
}
352349
if errors > 0 {
353350
exit(1)

0 commit comments

Comments
 (0)