Fix try not working with let, etc. (#13885)

# Description
Partialy addresses #13868. `try` does not catch non-zero exit code
errors from the last command in a pipeline if the result is assigned to
a variable using `let` (or `mut`).

This was fixed by adding a new `OutDest::Value` case. This is used when
the pipeline is in a "value" position. I.e., it will be collected into a
value. This ended up replacing most of the usages of `OutDest::Capture`.
So, this PR also renames `OutDest::Capture` to `OutDest::PipeSeparate`
to better fit the few remaining use cases for it.

# User-Facing Changes
Bug fix.

# Tests + Formatting
Added two tests.
This commit is contained in:
Ian Manske
2024-09-23 04:44:25 -07:00
committed by GitHub
parent 2541a712e4
commit 03ee54a4df
32 changed files with 127 additions and 98 deletions

View File

@ -105,3 +105,15 @@ fn exit_code_available_in_catch() {
let actual = nu!("try { nu -c 'exit 42' } catch { |e| $e.exit_code }");
assert_eq!(actual.out, "42");
}
#[test]
fn try_catches_exit_code_in_assignment() {
let actual = nu!("let x = try { nu -c 'exit 42' } catch { |e| $e.exit_code }; $x");
assert_eq!(actual.out, "42");
}
#[test]
fn try_catches_exit_code_in_expr() {
let actual = nu!("print (try { nu -c 'exit 42' } catch { |e| $e.exit_code })");
assert_eq!(actual.out, "42");
}