Extract .nu-env tests and more granularity (#3078)

The autoenv logic mutates environment variables in the running session as
it operates and decides what to do for trusted directories containing `.nu-env`
files. Few of the ways to interact with it were all in a single test function.

We separate out all the ways that were done in the single test function to document
 it better. This will greatly help once we start refactoring our way out from setting
 environment variables this way to just setting them to `Scope`.

This is part of an on-going effort to keep variables (`PATH` and `ENV`)
in our `Scope` and rely on it for everything related to variables.

We expect to move away from setting (`std::*`) envrironment variables in the current
running process. This is non-trivial since we need to handle cases from vars
coming in from the outside world, prioritize, and also compare to the ones
we have both stored in memory and in configuration files.

Also to send out our in-memory (in `Scope`) variables properly to external
programs once we no longer rely on `std::env` vars from the running process.
This commit is contained in:
Andrés N. Robalino
2021-02-18 20:24:27 -05:00
committed by GitHub
parent deff1aa63b
commit 7dc1d6a350
9 changed files with 405 additions and 245 deletions

View File

@ -34,237 +34,6 @@ fn takes_rows_of_nu_value_strings_and_pipes_it_to_stdin_of_external() {
})
}
#[cfg(feature = "directories-support")]
#[cfg(feature = "which-support")]
#[test]
fn autoenv() {
use nu_test_support::fs::Stub::FileWithContent;
Playground::setup("autoenv_test", |dirs, sandbox| {
sandbox.mkdir("foo/bar");
sandbox.mkdir("bizz/buzz");
sandbox.mkdir("foob");
// Windows uses a different command to create an empty file so we need to have different content on windows.
let full_nu_env = if cfg!(target_os = "windows") {
r#"[env]
testkey = "testvalue"
[scriptvars]
myscript = "echo myval"
[scripts]
entryscripts = ["echo nul > hello.txt"]
exitscripts = ["echo nul > bye.txt"]"#
} else {
r#"[env]
testkey = "testvalue"
[scriptvars]
myscript = "echo myval"
[scripts]
entryscripts = ["touch hello.txt"]
exitscripts = ["touch bye.txt"]"#
};
sandbox.with_files(vec![
FileWithContent(".nu-env", full_nu_env),
FileWithContent(
"foo/.nu-env",
r#"[env]
overwrite_me = "set_in_foo"
fookey = "fooval" "#,
),
FileWithContent(
"foo/bar/.nu-env",
r#"[env]
overwrite_me = "set_in_bar""#,
),
FileWithContent("bizz/.nu-env", full_nu_env),
]);
//Make sure basic keys are set
let actual = nu!(
cwd: dirs.test(),
r#"autoenv trust .
echo $nu.env.testkey"#
);
assert!(actual.out.ends_with("testvalue"));
// Make sure exitscripts are run in the directory they were specified.
let actual = nu!(
cwd: dirs.test(),
r#"autoenv trust
cd ..
cd autoenv_test
ls
ls | where name == "bye.txt" | get name"#
);
assert!(actual.out.contains("bye.txt"));
// Make sure entry scripts are run
let actual = nu!(
cwd: dirs.test(),
r#"cd ..
autoenv trust autoenv_test
cd autoenv_test
ls | where name == "hello.txt" | get name"#
);
assert!(actual.out.contains("hello.txt"));
// If inside a directory with exitscripts, entering a subdirectory should not trigger the exitscripts.
let actual = nu!(
cwd: dirs.test(),
r#"autoenv trust
cd foob
ls | where name == "bye.txt" | get name"#
);
assert!(!actual.out.contains("bye.txt"));
// Make sure entryscripts are run when re-visiting a directory
let actual = nu!(
cwd: dirs.test(),
r#"autoenv trust bizz
cd bizz
rm hello.txt
cd ..
cd bizz
ls | where name == "hello.txt" | get name"#
);
assert!(actual.out.contains("hello.txt"));
// Entryscripts should not run after changing to a subdirectory.
let actual = nu!(
cwd: dirs.test(),
r#"autoenv trust bizz
cd bizz
cd buzz
ls | where name == hello.txt | get name"#
);
assert!(!actual.out.ends_with("hello.txt"));
//Backing out of the directory should unset the keys
// let actual = nu!(
// cwd: dirs.test(),
// r#"cd ..
// echo $nu.env.testkey"#
// );
// assert!(!actual.out.ends_with("testvalue"));
// Make sure script keys are set
let actual = nu!(
cwd: dirs.test(),
r#"echo $nu.env.myscript"#
);
assert!(actual.out.ends_with("myval"));
//Going to sibling directory without passing parent should work.
let actual = nu!(
cwd: dirs.test(),
r#"autoenv trust foo
cd foob
cd ../foo
echo $nu.env.fookey
cd .."#
);
assert!(actual.out.ends_with("fooval"));
//Going to sibling directory should unset keys
// let actual = nu!(
// cwd: dirs.test(),
// r#"cd foo
// cd ../foob
// echo $nu.env.fookey
// cd .."#
// );
// assert!(!actual.out.ends_with("fooval"));
// Make sure entry scripts are run
let actual = nu!(
cwd: dirs.test(),
r#"ls | where name == "hello.txt" | get name"#
);
assert!(actual.out.contains("hello.txt"));
//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
cd foo/bar
autoenv trust
echo $nu.env.fookey"#
);
assert!(actual.out.ends_with("fooval"));
//Subdirectories should overwrite the values of parent directories.
let actual = nu!(
cwd: dirs.test(),
r#"autoenv trust foo
cd foo/bar
autoenv trust
echo $nu.env.overwrite_me"#
);
assert!(actual.out.ends_with("set_in_bar"));
//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"))
})
}
#[cfg(feature = "which")]
#[test]
fn nu_let_env_overwrites() {
Playground::setup("syncs_env_test_1", |dirs, sandbox| {
sandbox.with_files(vec![FileWithContent(
"configuration.toml",
r#"
[env]
SHELL = "/usr/bin/you_already_made_the_nu_choice"
"#,
)]);
let mut file = dirs.test().clone();
file.push("configuration.toml");
let fake_config = FakeConfig::new(&file);
let mut actual = EnvironmentSyncer::new();
actual.set_config(Box::new(fake_config));
// Here, the environment variables from the current session
// are cleared since we will load and set them from the
// configuration file (if any)
actual.clear_env_vars(&mut ctx);
// Nu loads the environment variables from the configuration file (if any)
actual.load_environment();
// By this point, Nu has already loaded the environment variables
// stored in the configuration file. Before continuing we check
// if any new environment variables have been added from the ones loaded
// in the configuration file.
//
// Nu sees the missing "USER" variable and accounts for it.
actual.sync_env_vars(&mut ctx);
let actual = nu!(
cwd: dirs.test(),
r#"let-env SHELL = bob
echo $nu.env.SHELL
"#
);
assert!(actual.out.ends_with("set_in_foo"))
});
}
#[test]
fn invocation_properly_redirects() {
let actual = nu!(