Skip to content

Commit

Permalink
Test spec from string for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
NonlinearFruit committed Mar 29, 2024
1 parent c1f9b7e commit cf0081d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
35 changes: 28 additions & 7 deletions nuunit.nu
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
def main [
--test-spec-module-name = "test-spec.nu"
--test-spec-module-script:string
--as-json
] {
if (not ($test_spec_module_name | path exists)) {
if (not ($test_spec_module_name | path exists) and ($test_spec_module_script | is-empty)) {
return $"Invalid test spec module: ($test_spec_module_name)"
}
let module = $test_spec_module_name | str replace '.nu' ''
let importScript = $"use ($test_spec_module_name) *"
discover-tests $module $importScript
let importScript = $test_spec_module_script | default $"use ($test_spec_module_name) *"
let testResults = discover-tests $module $importScript
| run-tests
| output-tests

$testResults
| output-tests $as_json
| print

$testResults
| get exit_code
| sort
| first
| exit $in
}

def discover-tests [module testImportScript] {
Expand All @@ -27,7 +38,13 @@ def discover-tests [module testImportScript] {
{
id: \($it.index + 1)
name: $it.item
exec: $'($testImportScript); try {\($it.item)} catch {|err| print -e $err.debug; exit 1}'
exec: $'($testImportScript)
try {
\($it.item)
} catch {|err|
print -e $err.debug
exit 1
}'
}
}
| to nuon"
Expand All @@ -50,8 +67,12 @@ def run-tests [] {
}
}

def output-tests [] {
$in
def output-tests [asJson] {
let tests = $in
if ($asJson) {
return ($tests | to json)
}
$tests
| each {|testResult|
if $testResult.exit_code == 0 {
$"ok ($testResult.id) - ($testResult.name)"
Expand Down
30 changes: 30 additions & 0 deletions test-spec.nu
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,33 @@ export def "test handles when spec does not exist" [] {
^$nu.current-exe --no-config-file nuunit.nu --test-spec-module-name $specFile
| assert equal $"Invalid test spec module: ($specFile)" ($in | str trim)
}

export def "test exit with error when test errors" [] {
let specScript = "
export module test {
export def test_that_fails [] { exit 400 }
}
"
do {
^nu --no-config-file nuunit.nu --test-spec-module-name "test" --test-spec-module-script $specScript
}
| complete
| assert not equal 0 ($in.exit_code)
}

export def "test runs tests from spec script" [] {
let specScript = "
export module test {
export def test_the_stuff [] {}
export def test_the_other [] {}
}
use test *
"
(^$nu.current-exe --no-config-file nuunit.nu
--test-spec-module-name "test"
--test-spec-module-script $specScript
--as-json)
| from json
| length
| assert equal 2 $in
}

0 comments on commit cf0081d

Please sign in to comment.