tools: make cmd/tools/vretry_test.v independent from the presence of git (fix issue #23398)

This commit is contained in:
Delyan Angelov 2025-01-13 08:10:13 +02:00
parent 89d405ec52
commit 3523c44f42
No known key found for this signature in database
GPG key ID: 66886C0F12D595ED
2 changed files with 39 additions and 12 deletions

13
cmd/tools/check_retry.vsh Normal file
View file

@ -0,0 +1,13 @@
#!/usr/bin/env -S v -raw-vsh-tmp-prefix tmp
// This script is used by cmd/tools/vretry_test.v, to check that the `v retry`
// subcommand works as expected, without relying on external commands like
// `git`, or on non portable ones like `true`/`false` or `echo`.
fn main() {
args := arguments()#[1..]
println(args)
if args == ['too', 'many', 'arguments'] {
exit(1)
}
}

View file

@ -1,8 +1,11 @@
// This test uses the script cmd/tools/check_retry.vsh
import os import os
import log
const vexe = @VEXE const vexe = @VEXE
const vroot = os.dir(vexe)
const is_ci = os.getenv('CI') == 'true' const is_ci = os.getenv('CI') != ''
fn dump_on_ci[T](x T) { fn dump_on_ci[T](x T) {
if is_ci { if is_ci {
@ -10,38 +13,49 @@ fn dump_on_ci[T](x T) {
} }
} }
fn run(cmd string) os.Result {
log.info('>>> running cmd: ${cmd}')
defer {
log.info('>>> finished cmd: ${cmd}')
}
return os.execute(cmd)
}
fn test_retry() { fn test_retry() {
log.warn('start...')
defer {
log.warn('... done')
}
tpath := os.join_path(os.vtmp_dir(), 'vretry_test') tpath := os.join_path(os.vtmp_dir(), 'vretry_test')
os.rmdir_all(tpath) or {} os.rmdir_all(tpath) or {}
os.mkdir_all(tpath)! os.mkdir_all(tpath)!
defer { defer {
os.rmdir_all(tpath) or {} os.rmdir_all(tpath) or {}
} }
os.chdir(vroot)!
fail_cmd := 'git asdf' fail_cmd := '${vexe} run cmd/tools/check_retry.vsh too many arguments'
if is_ci { if is_ci {
// Skip longer running test on local runs. // Skip longer running test on local runs.
res := os.execute('${vexe} retry ${fail_cmd}') res := run('${vexe} retry ${fail_cmd}')
assert res.exit_code != 0 assert res.exit_code != 0
assert res.output.contains('error: exceeded maximum number of retries') assert res.output.contains('error: exceeded maximum number of retries')
} }
mut res := os.execute('${vexe} retry -d 0.2 -r 3 ${fail_cmd}') mut res := run('${vexe} retry -d 0.2 -r 3 ${fail_cmd}')
dump_on_ci(res) dump_on_ci(res)
assert res.exit_code != 0 assert res.exit_code != 0
assert res.output.contains('error: exceeded maximum number of retries (3)!') assert res.output.contains('error: exceeded maximum number of retries (3)!')
os.chdir(os.dir(vexe))! pass_cmd := '${vexe} run cmd/tools/check_retry.vsh'
pass_cmd := 'git branch' res = run('${vexe} retry ${pass_cmd}')
res = os.execute('${vexe} retry ${pass_cmd}')
dump_on_ci(res) dump_on_ci(res)
assert res.exit_code == 0 assert res.exit_code == 0
assert res.output == os.execute(pass_cmd).output assert res.output == run(pass_cmd).output
// Include flags on the cmd as well. // Include flags on the cmd as well.
pass_cmd_with_flags := 'git branch --list' pass_cmd_with_flags := '${vexe} run cmd/tools/check_retry.vsh --list -x arguments'
res = os.execute('${vexe} retry -r 3 -- ${pass_cmd_with_flags}') res = run('${vexe} retry -r 3 -- ${pass_cmd_with_flags}')
dump_on_ci(res) dump_on_ci(res)
assert res.exit_code == 0 assert res.exit_code == 0
assert res.output == os.execute(pass_cmd_with_flags).output assert res.output == run(pass_cmd_with_flags).output
} }