diff --git a/nuunit.nu b/nuunit.nu index 0198b16..7872cfc 100644 --- a/nuunit.nu +++ b/nuunit.nu @@ -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] { @@ -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" @@ -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)" diff --git a/test-spec.nu b/test-spec.nu index ffd100b..9380614 100644 --- a/test-spec.nu +++ b/test-spec.nu @@ -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 +}