diff --git a/dev/dev-test.nu b/dev/dev-test.nu index 7a2726a..7fb62ab 100755 --- a/dev/dev-test.nu +++ b/dev/dev-test.nu @@ -50,6 +50,18 @@ const FILTER_CACHE = "target/skyg-test-filters.cache" # Nushell would otherwise accept `--nocapture` on every subcommand and reject # the rest before `main` can explain them. export def --wrapped main [...args: string] { + # Only script mode reaches this entry point: `use ... *` binds the file stem to + # a wrapper that calls the subcommands directly. A non-zero status is still + # wanted from `nu dev/dev-test.nu `, and `exit` is acceptable here. + try { + dispatch-script-args $args + } catch {|e| + print -e $"($RUNNER): ($e.msg)" + exit 1 + } +} + +def dispatch-script-args [args: list] { let parsed = (parse-script-args $args) match $parsed.cmd { "help" => { print-help } @@ -143,6 +155,7 @@ export def "dev-test self-check" [] { ++ (check-filter-cache) ++ (check-openssl-verdict) ++ (check-kvm-step) + ++ (check-module-mode) ++ (check-shell-completion) ++ (check-nu-completions) ++ (check-docs) @@ -150,7 +163,8 @@ export def "dev-test self-check" [] { if not ($problems | is-empty) { print -e "dev-test self-check failed:" $problems | each {|p| print -e $" - ($p)" } | ignore - exit 1 + # Catchable, so a module-mode caller survives; script mode still exits 1. + error make { msg: "dev-test self-check failed; see the problems above" } } print "dev-test self-check: ok" } @@ -332,7 +346,7 @@ def lint-steps [] { let detail = (if not (($found.stdout | str trim) | is-empty) { $"openssl is in the dependency tree:\n($found.stdout)" } else { - $"cargo tree failed (exit ($found.exit_code)): ($found.stderr | str trim)" + $"cargo tree failed with status ($found.exit_code): ($found.stderr | str trim)" }) error make { msg: $detail } } @@ -358,8 +372,11 @@ def run-steps [steps: list] { print ((pad $r.step $width "left") + (if $r.result == "ok" { $"(ansi green)($status)(ansi reset)" } else { $"(ansi red)($status)(ansi reset)" }) + (pad $"(($r.seconds))s" 9 "right") + $" ($r.command)") } print "" - if ($results | any {|r| $r.result != "ok" }) { - exit 1 + let failed = ($results | where result != "ok" | get step) + if not ($failed | is-empty) { + # A catchable error, not `exit`: with `use dev/dev-test.nu *` in a Nushell + # config, `exit` ends the caller's session and cannot be caught. + error make { msg: $"($failed | str join ', ') failed; see the summary above" } } } @@ -696,6 +713,40 @@ def check-filter-cache [] { $problems } +# `exit` in a module ends the caller's Nushell session and cannot be caught, so +# a failing check must raise; only the script-mode entry point may exit. +def check-module-mode [] { + mut problems = [] + let lines = (open --raw $SCRIPT_PATH | lines) + let entry = ($lines | enumerate | where {|r| $r.item =~ '^export def --wrapped main'} | get 0?.index) + let next = ($lines | enumerate | where {|r| $entry != null and $r.index > $entry and ($r.item =~ '^export def ')} | get 0?.index) + let exits = ($lines | enumerate | where {|r| $r.item =~ '^\s*exit\b'} | get index) + let allowed = ($exits | where {|i| $entry != null and $i > $entry and ($next == null or $i < $next)}) + for i in ($exits | where {|i| $i not-in $allowed}) { + $problems = ($problems | append $"line ($i + 1) calls exit outside the script-mode entry point: ($lines | get $i | str trim)") + } + # `(exit ...)` inside a double-quoted string is a subexpression that ends the + # process instead of building the message. + for r in ($lines | enumerate | where {|r| $r.item =~ '\$".*\(\s*exit\b'}) { + $problems = ($problems | append $"line ($r.index + 1) interpolates a call to exit: ($r.item | str trim)") + } + # The failure path itself: catch it in module mode and keep going. + let probe = (with-env { SKYG_TEST_ASSUME_NO_KVM: "1" } { + run-external "nu" "--no-config-file" "-c" $"use ($SCRIPT_PATH) *; try { ($RUNNER) vm } catch { print 'caught' }; print 'after'" | complete + }) + if $probe.exit_code != 0 or not ($probe.stdout | str contains "caught") or not ($probe.stdout | str contains "after") { + $problems = ($problems | append $"module mode: a failing step was not catchable, exit code ($probe.exit_code); wanted 'caught' and 'after' in: ($probe.stdout | str trim) ($probe.stderr | str trim)") + } + # Script mode still has to fail. + let script = (with-env { SKYG_TEST_ASSUME_NO_KVM: "1" } { + run-external "nu" "--no-config-file" $SCRIPT_PATH "vm" | complete + }) + if $script.exit_code == 0 or not ($script.stderr | str contains "/dev/kvm") { + $problems = ($problems | append $"script mode: a failing step exited ($script.exit_code), expected non-zero with the /dev/kvm reason: ($script.stderr | str trim)") + } + $problems +} + def check-openssl-verdict [] { mut problems = [] let cases = [