diff --git a/crates/nu-utils/standard_library/std.nu b/crates/nu-utils/standard_library/std.nu index c6f451757..462b81536 100644 --- a/crates/nu-utils/standard_library/std.nu +++ b/crates/nu-utils/standard_library/std.nu @@ -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 diff --git a/crates/nu-utils/standard_library/test_asserts.nu b/crates/nu-utils/standard_library/test_asserts.nu index 1c1dc30c8..8ca0db7b1 100644 --- a/crates/nu-utils/standard_library/test_asserts.nu +++ b/crates/nu-utils/standard_library/test_asserts.nu @@ -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 +} diff --git a/crates/nu-utils/standard_library/tests.nu b/crates/nu-utils/standard_library/tests.nu index d163c4543..5424a03ad 100644 --- a/crates/nu-utils/standard_library/tests.nu +++ b/crates/nu-utils/standard_library/tests.nu @@ -6,14 +6,23 @@ use std.nu * # # the output would be like # - " x " all in red if failed +# - " s " all in yellow if skipped # - " " 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)" ""