From f9cf1d943c6d3bea99de57b36f5e90cf2862645b Mon Sep 17 00:00:00 2001 From: Antoine Stevan <44101798+amtoine@users.noreply.github.com> Date: Sat, 18 Mar 2023 15:23:41 +0100 Subject: [PATCH] standard library: use the standard assert and fix test output (#8509) # Description ## in the `test_dirs` test module - use the `std assert` function in the `test_dirs` module instead of `myassert` - refactor the "test cleaning" in the `clean` command - allows to clean the tests and then throw a real error in the `catch` block in case there is an error - the test still "try"s to `clean` the test directory after the `catch`, like in a "finally" block - parse the `catch` error and `error make` a proper one instead of `debug`ging it => because the `catch` will be triggered as soon as one error occurs, there will always only be a single error in the tests, so this does not change the behaviour of failing `dirs` tests! > **Note** > i'm not particularly happy with the parsing stage in the `catch` block. > however that's the simplest i found to both keep the `try` / `catch` construct that allows to clean the test directory and have a proper error at the same time! ## in the global `tests` module - use `print` instead of `echo` to make sure the log statements show up during the tests # User-Facing Changes ``` $nothing ``` # Tests + Formatting ```bash nu crates/nu-utils/standard_library/tests.nu ``` passes but now with - proper log statements - proper error when a `dirs` error occurs => try with `sd 'assert \(1' "assert (10" crates/nu-utils/standard_library/test_dirs.nu` :wink: # After Submitting ``` $nothing ``` --- crates/nu-utils/standard_library/test_dirs.nu | 69 ++++++++++--------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/crates/nu-utils/standard_library/test_dirs.nu b/crates/nu-utils/standard_library/test_dirs.nu index 60f29c9873..88faa2567a 100644 --- a/crates/nu-utils/standard_library/test_dirs.nu +++ b/crates/nu-utils/standard_library/test_dirs.nu @@ -1,19 +1,9 @@ -use std.nu +use std.nu assert -def "myassert" [ - predicate: bool - msg?:string = "..." - --verbose = false (-v) # enable to see successful tests -] { - if not $predicate { - let span = (metadata $predicate).span - error make {msg: $"Assertion failed checking ($msg)", - label: {text: "Condition not true" start: $span.start end: $span.end}} - } else { - if $verbose { - echo $"check succeeded: ($msg)" - } - } +def clean [path: path] { + cd $path + cd .. + rm -r $path } export def test_dirs_command [] { @@ -31,37 +21,52 @@ export def test_dirs_command [] { use std.nu "dirs drop" use std.nu "dirs show" - myassert (1 == ($env.DIRS_LIST | length)) "list is just pwd after initialization" - myassert ($base_path == $env.DIRS_LIST.0) "list is just pwd after initialization" + assert (1 == ($env.DIRS_LIST | length)) "list is just pwd after initialization" + assert ($base_path == $env.DIRS_LIST.0) "list is just pwd after initialization" dirs next - myassert ($base_path == $env.DIRS_LIST.0) "next wraps at end of list" + assert ($base_path == $env.DIRS_LIST.0) "next wraps at end of list" dirs prev - myassert ($base_path == $env.DIRS_LIST.0) "prev wraps at top of list" + assert ($base_path == $env.DIRS_LIST.0) "prev wraps at top of list" dirs add $path_b $path_a - myassert ($path_b == $env.PWD) "add changes PWD to first added dir" - myassert (3 == ($env.DIRS_LIST | length)) "add in fact adds to list" - myassert ($path_a == $env.DIRS_LIST.2) "add in fact adds to list" + assert ($path_b == $env.PWD) "add changes PWD to first added dir" + assert (3 == ($env.DIRS_LIST | length)) "add in fact adds to list" + assert ($path_a == $env.DIRS_LIST.2) "add in fact adds to list" dirs next 2 - myassert ($base_path == $env.PWD) "next wraps at end of list" + assert ($base_path == $env.PWD) "next wraps at end of list" dirs prev 1 - myassert ($path_a == $env.PWD) "prev wraps at start of list" + assert ($path_a == $env.PWD) "prev wraps at start of list" dirs drop - myassert (2 == ($env.DIRS_LIST | length)) "drop removes from list" - myassert ($base_path == $env.PWD) "drop changes PWD to next in list (after dropped element)" + assert (2 == ($env.DIRS_LIST | length)) "drop removes from list" + assert ($base_path == $env.PWD) "drop changes PWD to next in list (after dropped element)" - myassert ((dirs show) == [[active path]; [true $base_path] [false $path_b]]) "show table contains expected information" + assert ((dirs show) == [[active path]; [true $base_path] [false $path_b]]) "show table contains expected information" } catch { |error| - $error | debug - true + clean $base_path + + let error = ( + $error + | get debug + | str replace "{" "(" + | str replace "}" ")" + | parse 'GenericError("{msg}", "{text}", Some(Span ( start: {start}, end: {end} )), {rest})' + | reject rest + | get 0 + ) + error make { + msg: $error.msg + label: { + text: $error.text + start: ($error.start | into int) + end: ($error.end | into int) + } + } } - cd $base_path - cd .. - rm -r $base_path + try { clean $base_path } }