feat(std/dirs): retain state in subshells or with exec-restarts (#15080)

Persist `std/dirs`'s state in subshells and also when restarting the
shell with `exec $nu.current-exe`
This commit is contained in:
Bahex 2025-02-12 03:51:43 +03:00 committed by GitHub
parent f7d5162582
commit d577074da9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -17,8 +17,16 @@
# This situation could arise if we started with [/a, /b, /c], then # This situation could arise if we started with [/a, /b, /c], then
# we changed directories from /b to /var/tmp. # we changed directories from /b to /var/tmp.
export-env { export-env {
$env.DIRS_POSITION = 0 $env.ENV_CONVERSIONS.DIRS_LIST = {
$env.DIRS_LIST = [($env.PWD | path expand)] from_string: {|| split row (char esep)}
to_string: {|| str join (char esep)}
}
$env.ENV_CONVERSIONS.DIRS_POSITION = {
from_string: {|| into int}
to_string: {|| into string}
}
$env.DIRS_POSITION = $env.DIRS_POSITION? | default (0)
$env.DIRS_LIST = $env.DIRS_LIST? | default [($env.PWD | path expand)]
} }
# Add one or more directories to the list. # Add one or more directories to the list.

View File

@ -46,6 +46,8 @@ def dirs_command [] {
# must set PWD *before* doing `use` that will run the def --env block in dirs module. # must set PWD *before* doing `use` that will run the def --env block in dirs module.
cd $c.base_path cd $c.base_path
# hide existing variables to prevent the state from outside affecting the tests
hide-env -i DIRS_LIST DIRS_POSITION
# must execute these uses for the UOT commands *after* the test and *not* just put them at top of test module. # must execute these uses for the UOT commands *after* the test and *not* just put them at top of test module.
# the def --env gets messed up # the def --env gets messed up
use std/dirs use std/dirs
@ -96,6 +98,7 @@ def dirs_next [] {
cd $c.base_path cd $c.base_path
assert equal $env.PWD $c.base_path "test setup" assert equal $env.PWD $c.base_path "test setup"
hide-env -i DIRS_LIST DIRS_POSITION
use std/dirs use std/dirs
cur_dir_check $c.base_path "use module test setup" cur_dir_check $c.base_path "use module test setup"
@ -117,6 +120,7 @@ def dirs_cd [] {
# must set PWD *before* doing `use` that will run the def --env block in dirs module. # must set PWD *before* doing `use` that will run the def --env block in dirs module.
cd $c.base_path cd $c.base_path
hide-env -i DIRS_LIST DIRS_POSITION
use std/dirs use std/dirs
cur_dir_check $c.base_path "use module test setup" cur_dir_check $c.base_path "use module test setup"
@ -139,6 +143,8 @@ def dirs_cd [] {
def dirs_goto_bug10696 [] { def dirs_goto_bug10696 [] {
let $c = $in let $c = $in
cd $c.base_path cd $c.base_path
hide-env -i DIRS_LIST DIRS_POSITION
use std/dirs use std/dirs
dirs add $c.path_a dirs add $c.path_a
@ -153,6 +159,8 @@ def dirs_goto_bug10696 [] {
def dirs_goto [] { def dirs_goto [] {
let $c = $in let $c = $in
cd $c.base_path cd $c.base_path
hide-env -i DIRS_LIST DIRS_POSITION
use std/dirs use std/dirs
# check that goto can move *from* any position in the ring *to* any other position (correctly) # check that goto can move *from* any position in the ring *to* any other position (correctly)