nushell/crates/nu-utils/standard_library/test_dirs.nu
Antoine Stevan f9cf1d943c
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` 😉

# After Submitting
```
$nothing
```
2023-03-18 09:23:41 -05:00

73 lines
2.2 KiB
Plaintext

use std.nu assert
def clean [path: path] {
cd $path
cd ..
rm -r $path
}
export def test_dirs_command [] {
# need some directories to play with
let base_path = (($nu.temp-path) | path join $"test_dirs_(random uuid)" | path expand )
let path_a = ($base_path | path join "a")
let path_b = ($base_path | path join "b")
try {
mkdir $base_path $path_a $path_b
cd $base_path
use std.nu "dirs next"
use std.nu "dirs prev"
use std.nu "dirs add"
use std.nu "dirs drop"
use std.nu "dirs show"
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
assert ($base_path == $env.DIRS_LIST.0) "next wraps at end of list"
dirs prev
assert ($base_path == $env.DIRS_LIST.0) "prev wraps at top of list"
dirs add $path_b $path_a
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
assert ($base_path == $env.PWD) "next wraps at end of list"
dirs prev 1
assert ($path_a == $env.PWD) "prev wraps at start of list"
dirs drop
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)"
assert ((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 }
}