mirror of
https://github.com/nushell/nushell
synced 2024-12-26 13:03:07 +00:00
rewrite the runner in a structured way
This commit is contained in:
parent
ec85b6fd3f
commit
9c12211564
1 changed files with 49 additions and 18 deletions
|
@ -1,28 +1,59 @@
|
||||||
use std.nu *
|
use std.nu *
|
||||||
|
|
||||||
def main [] {
|
# show a test record in a pretty way
|
||||||
for test_file in (ls ($env.FILE_PWD | path join "test_*.nu") -f | get name) {
|
#
|
||||||
let $module_name = ($test_file | path parse).stem
|
# `$in` must be a `record<file: string, module: string, name: string, pass: bool>`.
|
||||||
|
#
|
||||||
|
# the output would be like
|
||||||
|
# - "<indentation> x <module>::<test>" all in red if failed
|
||||||
|
# - "<indentation> <module>::<test>" all in green if passed
|
||||||
|
def show-pretty-test [indent: int = 4] {
|
||||||
|
let test = $in
|
||||||
|
|
||||||
log info $"Run tests in ($module_name) module"
|
[
|
||||||
let tests = (
|
(" " * $indent)
|
||||||
nu -c $'use ($test_file) *; $nu.scope.commands | select name module_name | to nuon'
|
(if $test.pass { ansi green } else { ansi red})
|
||||||
|
(if $test.pass { " " } else { char failed})
|
||||||
|
" "
|
||||||
|
$"($test.module)::($test.name)"
|
||||||
|
ansi reset
|
||||||
|
] | str join
|
||||||
|
}
|
||||||
|
|
||||||
|
def main [] {
|
||||||
|
let tests = (
|
||||||
|
ls ($env.FILE_PWD | path join "test_*.nu") | 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'
|
||||||
| from nuon
|
| from nuon
|
||||||
| where module_name == $module_name
|
| where module_name == $module.name
|
||||||
| where ($it.name | str starts-with "test_")
|
| where ($it.name | str starts-with "test_")
|
||||||
| get name
|
| get name
|
||||||
)
|
}
|
||||||
|
| flatten
|
||||||
for test_case in $tests {
|
| rename file module name
|
||||||
log debug $"Run test ($module_name) ($test_case)"
|
| upsert pass {|test|
|
||||||
|
log info $"Run test ($test.module) ($test.name)"
|
||||||
try {
|
try {
|
||||||
nu -c $'use ($test_file) ($test_case); ($test_case)'
|
nu -c $'use ($test.file) ($test.name); ($test.name)'
|
||||||
} catch { error make {
|
true
|
||||||
msg: $"(ansi red)std::tests::test_failed(ansi reset)"
|
} catch { false }
|
||||||
label: {
|
}
|
||||||
text: $"($module_name)::($test_case) failed."
|
)
|
||||||
}
|
|
||||||
}}
|
if not ($tests | where not pass | is-empty) {
|
||||||
|
let text = ([
|
||||||
|
$"(ansi purple)some tests did not pass (char lparen)see complete errors above(char rparen):(ansi reset)"
|
||||||
|
""
|
||||||
|
($tests | each {|test| ($test | show-pretty-test 8)} | str join "\n")
|
||||||
|
""
|
||||||
|
] | str join "\n")
|
||||||
|
|
||||||
|
error make {
|
||||||
|
msg: $"(ansi red)std::tests::some_tests_failed(ansi reset)"
|
||||||
|
label: {
|
||||||
|
text: $text
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue