mirror of
https://github.com/nushell/nushell
synced 2025-01-12 13:19:01 +00:00
make exec command decrement SHLVL correctly & SHLVL related test (#14707)
# Description Rework of #14570, fixing #14567. `exec` will decrement `SHLVL` env value before passing it to target executable (in interactive mode). (Same as last pr, but this time there's no wrong change to current working code) Two `SHLVL` related tests were also added this time.
This commit is contained in:
parent
e7877db078
commit
4884894ddb
2 changed files with 30 additions and 0 deletions
|
@ -61,6 +61,16 @@ On Windows based systems, Nushell will wait for the command to finish and then e
|
|||
let envs = env_to_strings(engine_state, stack)?;
|
||||
command.env_clear();
|
||||
command.envs(envs);
|
||||
// Decrement SHLVL as removing the current shell from the stack
|
||||
// (only works in interactive mode, same as initialization)
|
||||
if engine_state.is_interactive {
|
||||
let shlvl = engine_state
|
||||
.get_env_var("SHLVL")
|
||||
.and_then(|shlvl_env| shlvl_env.coerce_str().ok()?.parse::<i64>().ok())
|
||||
.unwrap_or(1)
|
||||
.saturating_sub(1);
|
||||
command.env("SHLVL", shlvl.to_string());
|
||||
}
|
||||
|
||||
// Configure args.
|
||||
let args = crate::eval_arguments_from_call(engine_state, stack, call)?;
|
||||
|
|
|
@ -229,3 +229,23 @@ fn std_log_env_vars_have_defaults() {
|
|||
assert!(actual.err.contains("%MSG%"));
|
||||
assert!(actual.err.contains("%Y-"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn env_shlvl() {
|
||||
let actual = nu!("
|
||||
$env.SHLVL = 5
|
||||
nu -i -c 'print $env.SHLVL'
|
||||
");
|
||||
|
||||
assert_eq!(actual.out, "6");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn env_shlvl_in_exec() {
|
||||
let actual = nu!("
|
||||
$env.SHLVL = 29
|
||||
nu -i -c \"exec nu -i -c 'print $env.SHLVL'\"
|
||||
");
|
||||
|
||||
assert_eq!(actual.out, "30");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue