T: a dispatch error raises, so a module-mode session survives a typo
This commit is contained in:
1 parent
e736c12a0e
commit
4d7d7defe6
2 files changed
+32
-17
No files matched your search
+26
-17
@@ -52,15 +52,12 @@ const FILTER_CACHE_REL = "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 <sub>`, and `exit` is acceptable here.
|
||||
try {
|
||||
dispatch-script-args $args
|
||||
} catch {|e|
|
||||
print -e $"($RUNNER): ($e.msg)"
|
||||
exit 1
|
||||
}
|
||||
# Both script mode (`nu dev/dev-test.nu <sub>`) and module mode land here:
|
||||
# `use ... *` binds the bare name `dev-test` to this entry point, so a typo
|
||||
# such as `dev-test bogus` calls it too. Raising keeps a module-mode caller
|
||||
# alive; an uncaught error already gives script mode a non-zero status, so
|
||||
# never `exit` here.
|
||||
dispatch-script-args $args
|
||||
}
|
||||
|
||||
def dispatch-script-args [args: list<string>] {
|
||||
@@ -377,7 +374,7 @@ def lint-steps [] {
|
||||
}
|
||||
|
||||
# Run each step in order, stop at the first failure, always print the summary
|
||||
# table and exit non-zero if anything failed.
|
||||
# table and raise if anything failed (the process then exits non-zero).
|
||||
def run-steps [steps: list] {
|
||||
mut results = []
|
||||
for s in $steps {
|
||||
@@ -759,16 +756,16 @@ def check-filter-cache [] {
|
||||
}
|
||||
|
||||
# `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.
|
||||
# no code path may call it, not even the entry point: `use ... *` binds the bare
|
||||
# name to `main` and `dev-test bogus` reaches it. A failing check raises instead.
|
||||
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)")
|
||||
# The scan has to see an inline `catch { …; exit 1 }` too, not just a line that
|
||||
# starts with `exit`; comment lines are dropped first so prose cannot trip it.
|
||||
let exits = ($lines | enumerate | where {|r| ($r.item | str replace --regex '^\s*#.*$' "") =~ '(^|[;{])\s*exit\b'} | get index)
|
||||
for i in $exits {
|
||||
$problems = ($problems | append $"line ($i + 1) calls exit, which ends a module-mode session: ($lines | get $i | str trim)")
|
||||
}
|
||||
# `(exit ...)` inside a double-quoted string is a subexpression that ends the
|
||||
# process instead of building the message.
|
||||
@@ -782,6 +779,12 @@ def check-module-mode [] {
|
||||
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)")
|
||||
}
|
||||
# Dispatch errors reach `main` too (`use ... *` binds the bare name), so a typo
|
||||
# must be catchable as well.
|
||||
let typo = (run-external "nu" "--no-config-file" "-c" $"use ($SCRIPT_PATH) *; try { ($RUNNER) bogus } catch { print 'caught' }; print 'after'" | complete)
|
||||
if $typo.exit_code != 0 or not ($typo.stdout | str contains "caught") or not ($typo.stdout | str contains "after") {
|
||||
$problems = ($problems | append $"module mode: an unknown subcommand was not catchable, exit code ($typo.exit_code); wanted 'caught' and 'after' in: ($typo.stdout | str trim) ($typo.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
|
||||
@@ -789,6 +792,12 @@ def check-module-mode [] {
|
||||
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)")
|
||||
}
|
||||
# The dispatch error is no longer caught in `main`, so script mode relies on
|
||||
# an uncaught error for its failure status.
|
||||
let script_typo = (run-external "nu" "--no-config-file" $SCRIPT_PATH "bogus" | complete)
|
||||
if $script_typo.exit_code == 0 or not ($script_typo.stderr | str contains "unknown subcommand") {
|
||||
$problems = ($problems | append $"script mode: an unknown subcommand exited ($script_typo.exit_code), expected non-zero with the unknown-subcommand reason: ($script_typo.stderr | str trim)")
|
||||
}
|
||||
$problems
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user