Tests for autoenv (and fixes for bugs the tests found) (#2148)

* add test basic_autoenv_vars_are_added

* Tests

* Entry and exit scripts

* Recursive set and overwrite

* Make sure that overwritten vals are restored

* Move tests to autoenv

* Move tests out of cli crate

* Tests help, apparently. Windows has issues

On windows, .nu-env is not applied immediately after running autoenv trust.
You have to cd out of the directory for it to work.

* Sort paths non-lexicographically

* Sibling dir test

* Revert "Sort paths non-lexicographically"

This reverts commit 72e4b856af.

* Rename test

* Change conditions

* Revert "Revert "Sort paths non-lexicographically""

This reverts commit 71606bc62f.

* Set vars as they are discovered

This means that if a parent directory is untrusted,
the variables in its child directories are still set properly.

* format

* Fix cleanup issues too

* Run commands in their separate functions

* Make everything into one large function like all the cool kids

* Refactoring

* fmt

* Debugging windows path issue

* Canonicalize

* Trim whitespace

* On windows, use echo nul instead of touch to create file in test

* Avoid cloning by using drain()
This commit is contained in:
Sam Hedin
2020-07-12 06:14:09 +02:00
committed by GitHub
parent bdef5d7d72
commit f3f40df4dd
5 changed files with 241 additions and 118 deletions

View File

@ -1,4 +1,5 @@
use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::fs::Stub::FileWithContentToBeTrimmed;
use nu_test_support::nu;
use nu_test_support::pipeline;
@ -36,6 +37,148 @@ fn takes_rows_of_nu_value_strings_and_pipes_it_to_stdin_of_external() {
})
}
#[test]
fn autoenv() {
Playground::setup("autoenv_test", |dirs, sandbox| {
sandbox.mkdir("foo/bar");
sandbox.mkdir("foob");
let scriptfile = if cfg!(target_os = "windows") {
FileWithContent(
".nu-env",
r#"[env]
testkey = "testvalue"
[scriptvars]
myscript = "echo myval"
[scripts]
entryscripts = ["echo nul > hello.txt"]
exitscripts = ["echo nul > bye.txt"]"#,
)
} else {
FileWithContent(
".nu-env",
r#"[env]
testkey = "testvalue"
[scriptvars]
myscript = "echo myval"
[scripts]
entryscripts = ["touch hello.txt"]
exitscripts = ["touch bye.txt"]"#,
)
};
sandbox.with_files(vec![
scriptfile,
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""#,
),
]);
//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"));
//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"));
// Make sure exit scripts are run
let actual = nu!(
cwd: dirs.test(),
r#"cd ..
ls | where name == "bye.txt" | get name"#
);
assert!(actual.out.contains("bye.txt"));
//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"));
//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"));
//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"))
})
}
#[test]
fn proper_it_expansion() {
Playground::setup("ls_test_1", |dirs, sandbox| {