diff --git a/crates/nu-std/tests/run.nu b/crates/nu-std/tests/run.nu index b14ce12e7b..0ad59d8aa7 100644 --- a/crates/nu-std/tests/run.nu +++ b/crates/nu-std/tests/run.nu @@ -79,13 +79,16 @@ def main [ let tests = ( ls ($path | path join $module_search_pattern) | each {|row| {file: $row.name name: ($row.name | path parse | get stem)}} - | upsert test {|module| - nu -c $'use ($module.file) *; $nu.scope.commands | select name module_name | to nuon' + | upsert commands {|module| + nu -c $'use `($module.file)` *; $nu.scope.commands | select name module_name | to nuon' | from nuon | where module_name == $module.name - | where ($it.name | str starts-with "test_") | get name } + | upsert test {|module| $module.commands | where ($it | str starts-with "test_") } + | upsert setup {|module| "setup" in $module.commands } + | upsert teardown {|module| "teardown" in $module.commands } + | reject commands | flatten | rename file module name ) @@ -114,11 +117,27 @@ def main [ log info $"Running tests in ($module.name)" $module.tests | each {|test| log debug $"Running test ($test.name)" - nu -c $' - use ($test.file) ($test.name) + + let context_setup = if $test.setup { + $"use `($test.file)` setup; let context = \(setup\)" + } else { + "let context = {}" + } + + let context_teardown = if $test.teardown { + $"use `($test.file)` teardown; $context | teardown" + } else { + "" + } + + let nu_script = $' + ($context_setup) + use `($test.file)` ($test.name) try { - ($test.name) + $context | ($test.name) + ($context_teardown) } catch { |err| + ($context_teardown) if $err.msg == "ASSERT:SKIP" { exit 2 } else { @@ -126,6 +145,8 @@ def main [ } } ' + nu -c $nu_script + let result = match $env.LAST_EXIT_CODE { 0 => "pass", 2 => "skip", diff --git a/crates/nu-std/tests/test_dirs.nu b/crates/nu-std/tests/test_dirs.nu index 3d26434183..23866a578e 100644 --- a/crates/nu-std/tests/test_dirs.nu +++ b/crates/nu-std/tests/test_dirs.nu @@ -1,74 +1,55 @@ use std "assert length" use std "assert equal" -def clean [path: path] { - cd $path +export def setup [] { + {base_path: ($nu.temp-path | path join $"test_dirs_(random uuid)")} +} + +export def teardown [] { + let base_path = $in.base_path + cd $base_path cd .. - rm -r $path + rm -r $base_path } export def test_dirs_command [] { # need some directories to play with - let base_path = ($nu.temp-path | path join $"test_dirs_(random uuid)") + let base_path = $in.base_path let path_a = ($base_path | path join "a") let path_b = ($base_path | path join "b") - try { - mkdir $base_path $path_a $path_b + mkdir $base_path $path_a $path_b - cd $base_path - use std "dirs next" - use std "dirs prev" - use std "dirs add" - use std "dirs drop" - use std "dirs show" + cd $base_path + use std "dirs next" + use std "dirs prev" + use std "dirs add" + use std "dirs drop" + use std "dirs show" - assert length $env.DIRS_LIST 1 "list is just pwd after initialization" - assert equal $base_path $env.DIRS_LIST.0 "list is just pwd after initialization" + assert length $env.DIRS_LIST 1 "list is just pwd after initialization" + assert equal $base_path $env.DIRS_LIST.0 "list is just pwd after initialization" - dirs next - assert equal $base_path $env.DIRS_LIST.0 "next wraps at end of list" + dirs next + assert equal $base_path $env.DIRS_LIST.0 "next wraps at end of list" - dirs prev - assert equal $base_path $env.DIRS_LIST.0 "prev wraps at top of list" + dirs prev + assert equal $base_path $env.DIRS_LIST.0 "prev wraps at top of list" - dirs add $path_b $path_a - assert equal $path_b $env.PWD "add changes PWD to first added dir" - assert length $env.DIRS_LIST 3 "add in fact adds to list" - assert equal $path_a $env.DIRS_LIST.2 "add in fact adds to list" + dirs add $path_b $path_a + assert equal $path_b $env.PWD "add changes PWD to first added dir" + assert length $env.DIRS_LIST 3 "add in fact adds to list" + assert equal $path_a $env.DIRS_LIST.2 "add in fact adds to list" - dirs next 2 - assert equal $base_path $env.PWD "next wraps at end of list" + dirs next 2 + assert equal $base_path $env.PWD "next wraps at end of list" - dirs prev 1 - assert equal $path_a $env.PWD "prev wraps at start of list" + dirs prev 1 + assert equal $path_a $env.PWD "prev wraps at start of list" - dirs drop - assert length $env.DIRS_LIST 2 "drop removes from list" - assert equal $base_path $env.PWD "drop changes PWD to next in list (after dropped element)" + dirs drop + assert length $env.DIRS_LIST 2 "drop removes from list" + assert equal $base_path $env.PWD "drop changes PWD to next in list (after dropped element)" - assert equal (dirs show) [[active path]; [true $base_path] [false $path_b]] "show table contains expected information" - } catch { |error| - 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) - } - } - } - - try { clean $base_path } + assert equal (dirs show) [[active path]; [true $base_path] [false $path_b]] "show table contains expected information" } diff --git a/crates/nu-std/tests/test_setup_teardown.nu b/crates/nu-std/tests/test_setup_teardown.nu new file mode 100644 index 0000000000..9861c4d4e0 --- /dev/null +++ b/crates/nu-std/tests/test_setup_teardown.nu @@ -0,0 +1,32 @@ +use std "log debug" +use std "log error" +use std "assert" +use std "assert skip" + +export def setup [] { + log debug "Setup is running" + {msg: "This is the context"} +} + +export def teardown [] { + log debug $"Teardown is running. Context: ($in)" +} + +export def test_assert_pass [] { + log debug $"Assert is running. Context: ($in)" +} + +export def test_assert_skip [] { + log debug $"Assert is running. Context: ($in)" + assert skip +} + +export def test_assert_fail_skipped_by_default [] { + assert skip # Comment this line if you want to see what happens if a test fails + log debug $"Assert is running. Context: ($in)" + assert false +} + +export def unrelated [] { + log error "This should not run" +} diff --git a/crates/nu-std/tests/test_xml.nu b/crates/nu-std/tests/test_xml.nu index d5037f529d..27054d8406 100644 --- a/crates/nu-std/tests/test_xml.nu +++ b/crates/nu-std/tests/test_xml.nu @@ -3,8 +3,23 @@ use std "xupdate" use std "xinsert" use std "assert equal" +export def setup [] { + {sample_xml: (' + + + + + + + z + x + + ' | from xml) + } +} + export def test_xml_xaccess [] { - let sample_xml = ('zx' | from xml) + let sample_xml = $in.sample_xml assert equal ($sample_xml | xaccess [a]) [$sample_xml] assert equal ($sample_xml | xaccess [*]) [$sample_xml] @@ -14,7 +29,7 @@ export def test_xml_xaccess [] { } export def test_xml_xupdate [] { - let sample_xml = ('zx' | from xml) + let sample_xml = $in.sample_xml assert equal ($sample_xml | xupdate [*] {|x| $x | update attributes {i: j}}) ('zx' | from xml) assert equal ($sample_xml | xupdate [* d e *] {|x| $x | update content 'nushell'}) ('nushellnushell' | from xml) @@ -22,7 +37,7 @@ export def test_xml_xupdate [] { } export def test_xml_xinsert [] { - let sample_xml = ('zx' | from xml) + let sample_xml = $in.sample_xml assert equal ($sample_xml | xinsert [a] {tag: b attributes:{} content: []}) ('zx' | from xml) assert equal ($sample_xml | xinsert [a d *] {tag: null attributes: null content: 'n'} | to xml) 'znxn'