From 55e4e84f5d9d48b402f5ae5ef9a2b27b28c81ade Mon Sep 17 00:00:00 2001 From: Sam Hedin Date: Fri, 10 Jul 2020 03:54:02 +0200 Subject: [PATCH] Make sure that overwritten vals are restored --- .../src/env/directory_specific_environment.rs | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/crates/nu-cli/src/env/directory_specific_environment.rs b/crates/nu-cli/src/env/directory_specific_environment.rs index 4ba4c567a9..1f29328479 100644 --- a/crates/nu-cli/src/env/directory_specific_environment.rs +++ b/crates/nu-cli/src/env/directory_specific_environment.rs @@ -244,38 +244,32 @@ mod tests { use nu_test_support::playground::Playground; #[test] - fn basic_autoenv_vars_are_added_and_removed() { + fn autoenv() { Playground::setup("autoenv_test_1", |dirs, sandbox| { sandbox.mkdir("foo/bar"); sandbox.with_files(vec![ FileWithContent( ".nu-env", - r#" - [env] + r#"[env] testkey = "testvalue" [scriptvars] myscript = "echo 'myval'" [scripts] entryscripts = ["touch hello.txt"] - exitscripts = ["touch bye.txt"] - "#, + exitscripts = ["touch bye.txt"]"#, ), FileWithContent( "foo/.nu-env", - r#" - [env] - overwrite_me = "set_in_foo" - fookey = "fooval" - "#, + r#"[env] + overwrite_me = "set_in_foo" + fookey = "fooval""#, ), FileWithContent( "foo/bar/.nu-env", - r#" - [env] - overwrite_me = "set_in_bar" - "#, - ) + r#"[env] + overwrite_me = "set_in_bar""#, + ), ]); //Make sure basic keys are set @@ -326,8 +320,7 @@ mod tests { ); assert!(actual.out.ends_with("set_in_bar")); - - //Variables set in parent directories should be set even if you cd to a subdir + //Variables set in parent directories should be set even if you directly cd to a subdir let actual = nu!( cwd: dirs.test(), r#"autoenv trust foo @@ -336,6 +329,18 @@ mod tests { echo $nu.env.fookey"# ); assert!(actual.out.ends_with("fooval")); + + //Make sure that overwritten values are restored. + //By deleting foo/.nu-env, we make sure that the value is actually restored and not just set again by autoenv when we re-visit foo. + let actual = nu!( + cwd: dirs.test(), + r#"cd foo + cd bar + rm ../.nu-env + cd .. + echo $nu.env.overwrite_me"# + ); + assert!(actual.out.ends_with("set_in_foo")) }) } }