mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
stdlib: optimize test search and add better errors (#8626)
the first part of this PR comes from a request from @presidento in #8525. the second one is an improvement of the error support. # Description this PR - computes `module_search_pattern` to only `ls` the selected modules => the goal is to save search time in the future with more tests - gives better errors when - the `--path` is invalid - the `--module` does not exist - the search is too strict ### examples ```bash >_ nu crates/nu-utils/standard_library/tests.nu --path does-not-exist Error: × directory_not_found ╭─[<commandline>:1:1] 1 │ main --path does-not-exist · ───────┬────── · ╰── no such directory ╰──── ``` ```bash >_ nu crates/nu-utils/standard_library/tests.nu --module does-not-exist Error: × module_not_found ╭─[<commandline>:1:1] 1 │ main --module does-not-exist · ───────┬────── · ╰── no such module in /home/amtoine/.local/share/git/store/github.com/amtoine/nushell/crates/nu-utils/standard_library/ ╰──── ``` ```bash >_ nu crates/nu-utils/standard_library/tests.nu --command does_not_exist Error: × no test to run ``` instead of the previous ```bash >_ nu crates/nu-utils/standard_library/tests.nu --path does-not-exist Error: × No matches found for /home/amtoine/.local/share/git/store/github.com/amtoine/nushell/does-not-exist/test_*.nu ╭─[/home/amtoine/.local/share/git/store/github.com/amtoine/nushell/crates/nu-utils/standard_library/tests.nu:32:1] 32 │ let tests = ( 33 │ ls ($path | default $env.FILE_PWD | path join "test_*.nu") · ───────────────────────────┬─────────────────────────── · ╰── Pattern, file or folder not found 34 │ | each {|row| {file: $row.name name: ($row.name | path parse | get stem)}} ╰──── help: no matches found ``` ```bash >_ nu crates/nu-utils/standard_library/tests.nu --module does-not-exist Error: × expected table from pipeline ╭─[/home/amtoine/.local/share/git/store/github.com/amtoine/nushell/crates/nu-utils/standard_library/tests.nu:59:1] 59 │ $tests_to_run 60 │ | group-by module · ────┬─── · ╰── requires a table input 61 │ | transpose name tests ╰──── ``` ```bash >_ nu crates/nu-utils/standard_library/tests.nu --command does-not-exist Error: × expected table from pipeline ╭─[/home/amtoine/.local/share/git/store/github.com/amtoine/nushell/crates/nu-utils/standard_library/tests.nu:59:1] 59 │ $tests_to_run 60 │ | group-by module · ────┬─── · ╰── requires a table input 61 │ | transpose name tests ╰──── ``` # User-Facing Changes ``` $nothing ``` # Tests + Formatting ``` $nothing ``` # After Submitting ``` $nothing ```
This commit is contained in:
parent
944cad35bf
commit
332f1192a6
@ -20,6 +20,17 @@ def show-pretty-test [indent: int = 4] {
|
||||
] | str join
|
||||
}
|
||||
|
||||
def throw-error [error: record] {
|
||||
error make {
|
||||
msg: $"(ansi red)($error.msg)(ansi reset)"
|
||||
label: {
|
||||
text: ($error.label)
|
||||
start: $error.span.start
|
||||
end: $error.span.end
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Test executor
|
||||
#
|
||||
# It executes exported "test_*" commands in "test_*" modules
|
||||
@ -29,8 +40,35 @@ def main [
|
||||
--command: string, # Test command to run. Default: all test command found in the files.
|
||||
--list, # list the selected tests without running them.
|
||||
] {
|
||||
let module_search_pattern = ({
|
||||
stem: ($module | default "test_*")
|
||||
extension: nu
|
||||
} | path join)
|
||||
|
||||
if not ($path | is-empty) {
|
||||
if not ($path | path exists) {
|
||||
throw-error {
|
||||
msg: "directory_not_found"
|
||||
label: "no such directory"
|
||||
span: (metadata $path | get span)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let path = ($path | default $env.FILE_PWD)
|
||||
|
||||
if not ($module | is-empty) {
|
||||
if not ($path | path join $module_search_pattern | path exists) {
|
||||
throw-error {
|
||||
msg: "module_not_found"
|
||||
label: $"no such module in ($path)"
|
||||
span: (metadata $module | get span)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let tests = (
|
||||
ls ($path | default $env.FILE_PWD | path join "test_*.nu")
|
||||
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'
|
||||
@ -55,6 +93,10 @@ def main [
|
||||
return ($tests_to_run | select module name file)
|
||||
}
|
||||
|
||||
if ($tests_to_run | is-empty) {
|
||||
error make --unspanned {msg: "no test to run"}
|
||||
}
|
||||
|
||||
let tests = (
|
||||
$tests_to_run
|
||||
| group-by module
|
||||
|
Loading…
Reference in New Issue
Block a user