tests: add ability to check the output of commands for .starts_with, .ends_with and .contains strings, in `v test-all`

Delyan Angelov 2022-04-25 11:58:26 +03:00 committed by Jef Roosens
parent b43c538bc0
commit 81963b51ab
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 64 additions and 14 deletions

View File

@ -49,17 +49,26 @@ enum RunCommandKind {
const expect_nothing = '<nothing>' const expect_nothing = '<nothing>'
const starts_with_nothing = '<nothing>'
const ends_with_nothing = '<nothing>'
const contains_nothing = '<nothing>'
struct Command { struct Command {
mut: mut:
line string line string
label string // when set, the label will be printed *before* cmd.line is executed label string // when set, the label will be printed *before* cmd.line is executed
ecode int ecode int
okmsg string okmsg string
errmsg string errmsg string
rmfile string rmfile string
runcmd RunCommandKind = .system runcmd RunCommandKind = .system
expect string = expect_nothing expect string = expect_nothing
output string starts_with string = starts_with_nothing
ends_with string = ends_with_nothing
contains string = contains_nothing
output string
} }
fn get_all_commands() []Command { fn get_all_commands() []Command {
@ -99,6 +108,14 @@ fn get_all_commands() []Command {
runcmd: .execute runcmd: .execute
expect: 'Hello, World!\n' expect: 'Hello, World!\n'
} }
res << Command{
line: '$vexe interpret examples/hanoi.v'
okmsg: 'V can interpret hanoi.v'
runcmd: .execute
starts_with: 'Disc 1 from A to C...\n'
ends_with: 'Disc 1 from A to C...\n'
contains: 'Disc 7 from A to C...\n'
}
res << Command{ res << Command{
line: '$vexe -o - examples/hello_world.v | grep "#define V_COMMIT_HASH" > /dev/null' line: '$vexe -o - examples/hello_world.v | grep "#define V_COMMIT_HASH" > /dev/null'
okmsg: 'V prints the generated source code to stdout with `-o -` .' okmsg: 'V prints the generated source code to stdout with `-o -` .'
@ -252,23 +269,56 @@ fn (mut cmd Command) run() {
spent := sw.elapsed().milliseconds() spent := sw.elapsed().milliseconds()
// //
mut is_failed := false mut is_failed := false
mut is_failed_expected := false
mut is_failed_starts_with := false
mut is_failed_ends_with := false
mut is_failed_contains := false
if cmd.ecode != 0 { if cmd.ecode != 0 {
is_failed = true is_failed = true
} }
if cmd.expect != expect_nothing { if cmd.expect != expect_nothing {
if cmd.output != cmd.expect { if cmd.output != cmd.expect {
is_failed = true is_failed = true
is_failed_expected = true
}
}
if cmd.starts_with != starts_with_nothing {
if !cmd.output.starts_with(cmd.starts_with) {
is_failed = true
is_failed_starts_with = true
}
}
if cmd.ends_with != ends_with_nothing {
if !cmd.output.ends_with(cmd.ends_with) {
is_failed = true
is_failed_ends_with = true
}
}
if cmd.contains != contains_nothing {
if !cmd.output.contains(cmd.contains) {
is_failed = true
is_failed_contains = true
} }
} }
// //
run_label := if is_failed { term.failed('FAILED') } else { term_highlight('OK') } run_label := if is_failed { term.failed('FAILED') } else { term_highlight('OK') }
println('> Running: "$cmd.line" took: $spent ms ... $run_label') println('> Running: "$cmd.line" took: $spent ms ... $run_label')
// //
if is_failed && cmd.expect != expect_nothing { if is_failed && is_failed_expected {
if cmd.output != cmd.expect { eprintln('> expected:\n$cmd.expect')
eprintln('> expected:\n$cmd.expect') eprintln('> output:\n$cmd.output')
eprintln('> output:\n$cmd.output') }
} if is_failed && is_failed_starts_with {
eprintln('> expected to start with:\n$cmd.starts_with')
eprintln('> output:\n${cmd.output#[..cmd.starts_with.len]}')
}
if is_failed && is_failed_ends_with {
eprintln('> expected to end with:\n$cmd.ends_with')
eprintln('> output:\n${cmd.output#[-cmd.starts_with.len..]}')
}
if is_failed && is_failed_contains {
eprintln('> expected to contain:\n$cmd.contains')
eprintln('> output:\n$cmd.output')
} }
if vtest_nocleanup { if vtest_nocleanup {
return return