std: refactor test-runner to no longer require tests to be exported (#9355)

# Description
Test runner now performs following actions in order to run tests:
* Module file is opened
* Public function with random name is added to the source code, this
function calls user-specified private function
* Modified module file is saved under random name in $nu.temp-path
* Modified module file is imported in subprocess, injected function is
called by the test runner
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
* Test functions no longer need to be exported
* test functions no longer need to reside in separate test_ files
* setup and teardown renamed to before-each and after-each respectively
* before-all and after-all functions added that run before all tests in
given module. This matches the behavior of test runners used by other
languages such as JUnit/TestNG or Mocha
# Tests + Formatting

# After Submitting

---------

Co-authored-by: Kamil <skelly37@protonmail.com>
Co-authored-by: amtoine <stevan.antoine@gmail.com>
This commit is contained in:
Yethal
2023-06-10 20:16:17 +02:00
committed by GitHub
parent a86f34d0ad
commit 0bdc362e13
15 changed files with 334 additions and 255 deletions

View File

@@ -286,174 +286,3 @@ It's been this long since (ansi green)Nushell(ansi reset)'s first commit:
Startup Time: ($nu.startup-time)
"
}
# show a test record in a pretty way
#
# `$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> s <module> <test>" all in yellow if skipped
# - "<indentation> <module> <test>" all in green if passed
def show-pretty-test [indent: int = 4] {
let test = $in
[
(" " * $indent)
(match $test.result {
"pass" => { ansi green },
"skip" => { ansi yellow },
_ => { ansi red }
})
(match $test.result {
"pass" => " ",
"skip" => "s",
_ => { char failed }
})
" "
$"($test.module) ($test.test)"
(ansi reset)
] | 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
}
}
}
# Run Nushell tests
#
# It executes exported "test_*" commands in "test_*" modules
export def 'run-tests' [
--path: path, # Path to look for tests. Default: current directory.
--module: string, # Test module to run. Default: all test modules found.
--test: string, # Individual test to run. Default: all test command found in the files.
--list, # list the selected tests without running them.
] {
let module_search_pattern = ('**' | path join ({
stem: ($module | default "test_*")
extension: nu
} | path join))
let path = ($path | default $env.PWD)
if not ($path | path exists) {
throw-error {
msg: "directory_not_found"
label: "no such directory"
span: (metadata $path | get span)
}
}
if not ($module | is-empty) {
try { ls ($path | path join $module_search_pattern) | null } catch {
throw-error {
msg: "module_not_found"
label: $"no such module in ($path)"
span: (metadata $module | get span)
}
}
}
let tests = (
ls ($path | path join $module_search_pattern)
| each {|row| {file: $row.name name: ($row.name | path parse | get stem)}}
| upsert commands {|module|
^$nu.current-exe -c $'use `($module.file)` *; $nu.scope.commands | select name module_name | to nuon'
| from nuon
| where module_name == $module.name
| 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 test
)
let tests_to_run = (if not ($test | is-empty) {
$tests | where test == $test
} else if not ($module | is-empty) {
$tests | where module == $module
} else {
$tests
})
if $list {
return ($tests_to_run | select module test file)
}
if ($tests_to_run | is-empty) {
error make --unspanned {msg: "no test to run"}
}
let tests = (
$tests_to_run
| group-by module
| transpose name tests
| each {|module|
log info $"Running tests in module ($module.name)"
$module.tests | each {|test|
log debug $"Running test ($test.test)"
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.test)
try {
$context | ($test.test)
($context_teardown)
} catch { |err|
($context_teardown)
if $err.msg == "ASSERT:SKIP" {
exit 2
} else {
$err | get raw
}
}
'
^$nu.current-exe -c $nu_script
let result = match $env.LAST_EXIT_CODE {
0 => "pass",
2 => "skip",
_ => "fail",
}
if $result == "skip" {
log warning $"Test case ($test.test) is skipped"
}
$test | merge ({result: $result})
}
}
| flatten
)
if not ($tests | where result == "fail" | 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 4)} | str join "\n")
""
] | str join "\n")
error make --unspanned { msg: $text }
}
}