stdlib: add assert skip command to skip test case (#8748)

This commit is contained in:
Máté FARKAS 2023-04-06 20:03:10 +02:00 committed by GitHub
parent 8a030f3bfc
commit 74283c3ebc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 14 deletions

View File

@ -67,6 +67,16 @@ export def "assert error" [
}
}
# Skip the current test case
#
# # Examples
#
# if $condition { assert skip }
export def "assert skip" [] {
error make {msg: "ASSERT:SKIP"}
}
# Assert $left == $right
#
# For more documentation see the assert command

View File

@ -57,8 +57,6 @@ export def test_assert_length [] {
assert error { assert length [0, 0] 3 }
}
# export def test_assert_§ [] {
# assert §
# assert error { assert § }
# }
export def test_assert_skip [] {
assert skip # This test case is skipped on purpose
}

View File

@ -6,14 +6,23 @@ use std.nu *
#
# the output would be like
# - "<indentation> x <module> <test>" all in red if failed
# - "<indentation> s <module> <test>" all in yellow if skipped
# - "<indentation> <module> <test>" all in green if passed
def show-pretty-test [indent: int = 4] {
let test = $in
[
(" " * $indent)
(if $test.pass { ansi green } else { ansi red})
(if $test.pass { " " } else { char failed})
(match $test.result {
"pass" => { ansi green },
"skip" => { ansi yellow },
_ => { ansi red }
})
(match $test.result {
"pass" => " ",
"skip" => "s",
_ => { char failed }
})
" "
$"($test.module) ($test.name)"
(ansi reset)
@ -105,18 +114,33 @@ def main [
log info $"Running tests in ($module.name)"
$module.tests | each {|test|
log debug $"Running test ($test.name)"
let did_pass = (try {
nu -c $'use ($test.file) ($test.name); ($test.name)'
true
} catch { false })
$test | merge ({pass: $did_pass})
nu -c $'
use ($test.file) ($test.name)
try {
($test.name)
} catch { |err|
if $err.msg == "ASSERT:SKIP" {
exit 2
} else {
$err | get raw
}
}
'
let result = match $env.LAST_EXIT_CODE {
0 => "pass",
2 => "skip",
_ => "fail",
}
if $result == "skip" {
log warning $"Test case ($test.name) is skipped"
}
$test | merge ({result: $result})
}
}
| flatten
)
if not ($tests | where not pass | is-empty) {
if not ($tests | where result == "fail" | is-empty) {
let text = ([
$"(ansi purple)some tests did not pass (char lparen)see complete errors above(char rparen):(ansi reset)"
""