Skip to content

Commit

Permalink
tools.vpm: extract outdated logic into outdated.v, fix outdated hg, e…
Browse files Browse the repository at this point in the history
…xtend tests (#20116)
  • Loading branch information
ttytm committed Dec 8, 2023
1 parent 64e8139 commit 3d0fcfe
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 81 deletions.
53 changes: 0 additions & 53 deletions cmd/tools/vpm/common.v
Expand Up @@ -3,7 +3,6 @@ module main
import os
import net.http
import net.urllib
import sync.pool
import v.vmod
import json
import term
Expand All @@ -17,13 +16,6 @@ struct ModuleVpmInfo {
nr_downloads int
}

pub struct ModuleDateInfo {
name string
mut:
outdated bool
exec_err bool
}

@[params]
struct ErrorOptions {
details string
Expand All @@ -33,35 +25,6 @@ struct ErrorOptions {
const vexe = os.quoted_path(os.getenv('VEXE'))
const home_dir = os.home_dir()

fn get_mod_date_info(mut pp pool.PoolProcessor, idx int, wid int) &ModuleDateInfo {
mut result := &ModuleDateInfo{
name: pp.get_item[string](idx)
}
path := get_path_of_existing_module(result.name) or { return result }
vcs := vcs_used_in_dir(path) or { return result }
args := vcs_info[vcs].args
mut outputs := []string{}
for step in args.outdated {
cmd := [vcs.str(), args.path, os.quoted_path(path), step].join(' ')
res := os.execute(cmd)
if res.exit_code < 0 {
verbose_println('Error command: ${cmd}')
verbose_println('Error details:\n${res.output}')
result.exec_err = true
return result
}
if vcs == .hg && res.exit_code == 1 {
result.outdated = true
return result
}
outputs << res.output
}
if vcs == .git && outputs[1] != outputs[2] {
result.outdated = true
}
return result
}

fn get_mod_vpm_info(name string) !ModuleVpmInfo {
if name.len < 2 || (!name[0].is_digit() && !name[0].is_letter()) {
return error('invalid module name `${name}`.')
Expand Down Expand Up @@ -125,22 +88,6 @@ fn normalize_mod_path(path string) string {
return path.replace('-', '_').to_lower()
}

fn get_outdated() ![]string {
installed := get_installed_modules()
mut outdated := []string{}
mut pp := pool.new_pool_processor(callback: get_mod_date_info)
pp.work_on_items(installed)
for res in pp.get_results[ModuleDateInfo]() {
if res.exec_err {
return error('failed to check the latest commits for `${res.name}`.')
}
if res.outdated {
outdated << res.name
}
}
return outdated
}

fn get_all_modules() []string {
url := get_working_server_url()
r := http.get(url) or {
Expand Down
5 changes: 1 addition & 4 deletions cmd/tools/vpm/install_test.v
Expand Up @@ -11,10 +11,7 @@ import test_utils
const test_path = os.join_path(os.vtmp_dir(), 'vpm_install_test_${rand.ulid()}')

fn testsuite_begin() {
os.setenv('VMODULES', test_path, true)
os.setenv('VPM_DEBUG', '', true)
os.setenv('VPM_NO_INCREMENT', '1', true)
os.setenv('VPM_FAIL_ON_PROMPT', '1', true)
test_utils.set_test_env(test_path)
}

fn testsuite_end() {
Expand Down
5 changes: 2 additions & 3 deletions cmd/tools/vpm/install_version_input_test.v
Expand Up @@ -2,6 +2,7 @@
import os
import rand
import v.vmod
import test_utils

const vexe = os.quoted_path(@VEXE)
const test_path = os.join_path(os.vtmp_dir(), 'vpm_install_version_input_test_${rand.ulid()}')
Expand All @@ -12,9 +13,7 @@ const expect_exe = os.quoted_path(os.find_abs_path_of_executable('expect') or {
})

fn testsuite_begin() {
os.setenv('VMODULES', test_path, true)
os.setenv('VPM_NO_INCREMENT', '1', true)
os.setenv('VPM_DEBUG', '', true)
test_utils.set_test_env(test_path)
// Explicitly disable fail on prompt.
os.setenv('VPM_FAIL_ON_PROMPT', '', true)
os.mkdir_all(test_path) or {}
Expand Down
6 changes: 2 additions & 4 deletions cmd/tools/vpm/install_version_test.v
Expand Up @@ -4,14 +4,12 @@ module main
import os
import rand
import v.vmod
import test_utils

const test_path = os.join_path(os.vtmp_dir(), 'vpm_install_version_test_${rand.ulid()}')

fn testsuite_begin() {
os.setenv('VMODULES', test_path, true)
os.setenv('VPM_DEBUG', '', true)
os.setenv('VPM_NO_INCREMENT', '1', true)
os.setenv('VPM_FAIL_ON_PROMPT', '1', true)
test_utils.set_test_env(test_path)
}

fn testsuite_end() {
Expand Down
66 changes: 66 additions & 0 deletions cmd/tools/vpm/outdated.v
@@ -0,0 +1,66 @@
module main

import os
import sync.pool

pub struct OutdatedResult {
name string
mut:
outdated bool
}

fn vpm_outdated() {
outdated := get_outdated()
if outdated.len > 0 {
println('Outdated modules:')
for m in outdated {
println(' ${m}')
}
} else {
println('Modules are up to date.')
}
}

fn get_outdated() []string {
installed := get_installed_modules()
if installed.len == 0 {
println('No modules installed.')
exit(0)
}
mut pp := pool.new_pool_processor(
callback: fn (mut pp pool.PoolProcessor, idx int, wid int) &OutdatedResult {
mut result := &OutdatedResult{
name: pp.get_item[string](idx)
}
path := get_path_of_existing_module(result.name) or { return result }
result.outdated = is_outdated(path)
return result
}
)
pp.work_on_items(installed)
mut outdated := []string{}
for res in pp.get_results[OutdatedResult]() {
if res.outdated {
outdated << res.name
}
}
return outdated
}

fn is_outdated(path string) bool {
vcs := vcs_used_in_dir(path) or { return false }
args := vcs_info[vcs].args
mut outputs := []string{}
for step in args.outdated {
cmd := [vcs.str(), args.path, os.quoted_path(path), step].join(' ')
vpm_log(@FILE_LINE, @FN, 'cmd: ${cmd}')
res := os.execute(cmd)
vpm_log(@FILE_LINE, @FN, 'output: ${res.output}')
if vcs == .hg {
return res.exit_code == 0
}
outputs << res.output
}
// Compare the current and latest origin commit sha.
return vcs == .git && outputs[1] != outputs[2]
}
40 changes: 40 additions & 0 deletions cmd/tools/vpm/outdated_test.v
@@ -0,0 +1,40 @@
// vtest retry: 3
module main

import os
import rand
import test_utils

const test_path = os.join_path(os.vtmp_dir(), 'vpm_outdated_test_${rand.ulid()}')

fn testsuite_begin() {
test_utils.set_test_env(test_path)
os.mkdir_all(test_path)!
os.chdir(test_path)!
}

fn testsuite_end() {
os.rmdir_all(test_path) or {}
}

fn test_is_outdated_git_module() {
os.execute_or_exit('git clone https://github.com/vlang/libsodium.git')
assert !is_outdated('libsodium')
os.execute_or_exit('git -C libsodium reset --hard HEAD~1')
assert is_outdated('libsodium')
os.execute_or_exit('git -C libsodium pull')
assert !is_outdated('libsodium')
}

fn test_is_outdated_hg_module() {
os.find_abs_path_of_executable('hg') or {
eprintln('skipping test, since `hg` is not executable.')
return
}
os.execute_or_exit('hg clone https://www.mercurial-scm.org/repo/hello')
assert !is_outdated('hello')
os.execute_or_exit('hg --config extensions.strip= -R hello strip -r tip')
assert is_outdated('hello')
os.execute_or_exit('hg -R hello pull')
assert !is_outdated('hello')
}
9 changes: 8 additions & 1 deletion cmd/tools/vpm/test_utils/utils.v
Expand Up @@ -4,7 +4,14 @@ import os
import net
import time

fn hg_serve(hg_path string, path string) (&os.Process, int) {
pub fn set_test_env(test_path string) {
os.setenv('VMODULES', test_path, true)
os.setenv('VPM_DEBUG', '', true)
os.setenv('VPM_NO_INCREMENT', '1', true)
os.setenv('VPM_FAIL_ON_PROMPT', '1', true)
}

pub fn hg_serve(hg_path string, path string) (&os.Process, int) {
mut port := 8000
for {
if mut l := net.listen_tcp(.ip6, ':${port}') {
Expand Down
5 changes: 2 additions & 3 deletions cmd/tools/vpm/update_test.v
@@ -1,14 +1,13 @@
// vtest retry: 3
import os
import rand
import test_utils

const v = os.quoted_path(@VEXE)
const test_path = os.join_path(os.vtmp_dir(), 'vpm_update_test_${rand.ulid()}')

fn testsuite_begin() {
os.setenv('VMODULES', test_path, true)
os.setenv('VPM_DEBUG', '', true)
os.setenv('VPM_NO_INCREMENT', '1', true)
test_utils.set_test_env(test_path)
}

fn testsuite_end() {
Expand Down
14 changes: 1 addition & 13 deletions cmd/tools/vpm/vpm.v
Expand Up @@ -66,26 +66,14 @@ fn main() {
}

fn vpm_upgrade() {
outdated := get_outdated() or { exit(1) }
outdated := get_outdated()
if outdated.len > 0 {
vpm_update(outdated)
} else {
println('Modules are up to date.')
}
}

fn vpm_outdated() {
outdated := get_outdated() or { exit(1) }
if outdated.len > 0 {
println('Outdated modules:')
for m in outdated {
println(' ${m}')
}
} else {
println('Modules are up to date.')
}
}

fn vpm_list() {
installed := get_installed_modules()
if installed.len == 0 {
Expand Down

0 comments on commit 3d0fcfe

Please sign in to comment.