2021-02-19 02:24:27 +01:00
|
|
|
use super::support::Trusted;
|
|
|
|
|
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
use nu_test_support::playground::Playground;
|
|
|
|
use nu_test_support::{nu, pipeline};
|
2023-04-08 20:52:37 +02:00
|
|
|
use pretty_assertions::assert_eq;
|
2021-02-19 02:24:27 +01:00
|
|
|
|
|
|
|
use serial_test::serial;
|
|
|
|
|
2021-03-31 07:52:34 +02:00
|
|
|
const SCRIPTS: &str = r#"startup = ["touch hello.txt"]
|
|
|
|
on_exit = ["touch bye.txt"]"#;
|
2021-02-19 02:24:27 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn picks_up_env_keys_when_entering_trusted_directory() {
|
|
|
|
Playground::setup("autoenv_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
&format!(
|
|
|
|
"{}\n{}",
|
2021-03-31 07:52:34 +02:00
|
|
|
SCRIPTS,
|
2021-02-19 02:24:27 +01:00
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
2021-08-28 14:59:09 +02:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
[scriptvars]
|
|
|
|
myscript = "echo myval"
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
2021-02-19 02:24:27 +01:00
|
|
|
),
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let expected = "testvalue";
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let actual = Trusted::in_path(&dirs, || nu!(cwd: dirs.test(), "echo $env.testkey"));
|
2021-02-19 02:24:27 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-28 07:31:22 +02:00
|
|
|
#[cfg(feature = "which-support")]
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn picks_up_and_lets_go_env_keys_when_entering_trusted_directory_with_implied_cd() {
|
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
Playground::setup("autoenv_test", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("foo");
|
|
|
|
sandbox.mkdir("foo/bar");
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
FileWithContent(
|
|
|
|
"foo/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
FileWithContent(
|
|
|
|
"foo/bar/.nu-env",
|
|
|
|
r#"
|
|
|
|
[env]
|
|
|
|
bar = "true"
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-12-22 21:30:10 +01:00
|
|
|
do {autoenv trust -q foo ; = null }
|
2021-04-28 07:31:22 +02:00
|
|
|
foo
|
2023-07-12 19:07:20 +02:00
|
|
|
echo $env.testkey"
|
2021-04-28 07:31:22 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "testvalue");
|
|
|
|
//Assert testkey is gone when leaving foo
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-12-22 21:30:10 +01:00
|
|
|
do {autoenv trust -q foo; = null } ;
|
2021-04-28 07:31:22 +02:00
|
|
|
foo
|
|
|
|
..
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.testkey
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2021-04-28 07:31:22 +02:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("Unknown"));
|
|
|
|
//Assert testkey is present also when jumping over foo
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2022-12-22 21:30:10 +01:00
|
|
|
do {autoenv trust -q foo; = null } ;
|
|
|
|
do {autoenv trust -q foo/bar; = null } ;
|
2021-04-28 07:31:22 +02:00
|
|
|
foo/bar
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.testkey
|
|
|
|
echo $env.bar
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2021-04-28 07:31:22 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "testvaluetrue");
|
|
|
|
//Assert bar removed after leaving bar
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo;
|
2021-04-28 07:31:22 +02:00
|
|
|
foo/bar
|
|
|
|
../..
|
2023-07-12 19:07:20 +02:00
|
|
|
echo $env.bar"
|
2021-04-28 07:31:22 +02:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("Unknown"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
2021-03-31 07:52:34 +02:00
|
|
|
#[ignore]
|
2021-02-19 02:24:27 +01:00
|
|
|
fn picks_up_script_vars_when_entering_trusted_directory() {
|
|
|
|
Playground::setup("autoenv_test_2", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
&format!(
|
|
|
|
"{}\n{}",
|
2021-03-31 07:52:34 +02:00
|
|
|
SCRIPTS,
|
2021-02-19 02:24:27 +01:00
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
2021-08-28 14:59:09 +02:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
[scriptvars]
|
|
|
|
myscript = "echo myval"
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
2021-02-19 02:24:27 +01:00
|
|
|
),
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let expected = "myval";
|
|
|
|
|
2022-02-02 21:59:01 +01:00
|
|
|
let actual = Trusted::in_path(&dirs, || nu!(cwd: dirs.test(), "echo $env.myscript"));
|
2021-02-19 02:24:27 +01:00
|
|
|
|
2021-03-31 07:52:34 +02:00
|
|
|
// scriptvars are not supported
|
|
|
|
// and why is myval expected when myscript is "echo myval"
|
2021-02-19 02:24:27 +01:00
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn picks_up_env_keys_when_entering_trusted_directory_indirectly() {
|
|
|
|
Playground::setup("autoenv_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("crates");
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
r#"[env]
|
2021-04-20 08:34:10 +02:00
|
|
|
nu-ver = "0.30.0" "#,
|
2021-02-19 02:24:27 +01:00
|
|
|
)]);
|
|
|
|
|
2021-04-20 08:34:10 +02:00
|
|
|
let expected = "0.30.0";
|
2021-02-19 02:24:27 +01:00
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
2023-07-12 19:07:20 +02:00
|
|
|
nu!(cwd: dirs.test().join("crates"), "
|
2021-02-19 02:24:27 +01:00
|
|
|
cd ../../autoenv_test_3
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.nu-ver
|
2023-07-12 19:07:20 +02:00
|
|
|
")
|
2021-02-19 02:24:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, expected);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn entering_a_trusted_directory_runs_entry_scripts() {
|
|
|
|
Playground::setup("autoenv_test_4", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
&format!(
|
|
|
|
"{}\n{}",
|
2021-03-31 07:52:34 +02:00
|
|
|
SCRIPTS,
|
2021-02-19 02:24:27 +01:00
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
2021-02-19 02:24:27 +01:00
|
|
|
),
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
|
|
|
nu!(cwd: dirs.test(), pipeline(r#"
|
|
|
|
ls
|
|
|
|
| where name == "hello.txt"
|
|
|
|
| get name
|
|
|
|
"#))
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello.txt");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn leaving_a_trusted_directory_runs_exit_scripts() {
|
|
|
|
Playground::setup("autoenv_test_5", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
&format!(
|
|
|
|
"{}\n{}",
|
2021-03-31 07:52:34 +02:00
|
|
|
SCRIPTS,
|
2021-02-19 02:24:27 +01:00
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
2021-08-28 14:59:09 +02:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
[scriptvars]
|
|
|
|
myscript = "echo myval"
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
2021-02-19 02:24:27 +01:00
|
|
|
),
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
|
|
|
nu!(cwd: dirs.test(), r#"
|
|
|
|
cd ..
|
|
|
|
ls autoenv_test_5 | get name | path basename | where $it == "bye.txt"
|
|
|
|
"#)
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "bye.txt");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn entry_scripts_are_called_when_revisiting_a_trusted_directory() {
|
|
|
|
Playground::setup("autoenv_test_6", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
&format!(
|
|
|
|
"{}\n{}",
|
2021-03-31 07:52:34 +02:00
|
|
|
SCRIPTS,
|
2021-02-19 02:24:27 +01:00
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
2021-08-28 14:59:09 +02:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
[scriptvars]
|
|
|
|
myscript = "echo myval"
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
2021-02-19 02:24:27 +01:00
|
|
|
),
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
|
|
|
nu!(cwd: dirs.test(), r#"
|
2021-06-18 03:04:51 +02:00
|
|
|
do { rm hello.txt | ignore } ; # Silence file deletion message from output
|
2021-02-19 02:24:27 +01:00
|
|
|
cd ..
|
|
|
|
cd autoenv_test_6
|
|
|
|
ls | where name == "hello.txt" | get name
|
|
|
|
"#)
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "hello.txt");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn given_a_trusted_directory_with_entry_scripts_when_entering_a_subdirectory_entry_scripts_are_not_called(
|
|
|
|
) {
|
|
|
|
Playground::setup("autoenv_test_7", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("time_to_cook_arepas");
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
&format!(
|
|
|
|
"{}\n{}",
|
2021-03-31 07:52:34 +02:00
|
|
|
SCRIPTS,
|
2021-02-19 02:24:27 +01:00
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
2021-08-28 14:59:09 +02:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
[scriptvars]
|
|
|
|
myscript = "echo myval"
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
2021-02-19 02:24:27 +01:00
|
|
|
),
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
|
|
|
nu!(cwd: dirs.test(), r#"
|
|
|
|
cd time_to_cook_arepas
|
2021-03-13 22:46:40 +01:00
|
|
|
ls | where name == "hello.txt" | length
|
2021-02-19 02:24:27 +01:00
|
|
|
"#)
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "0");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn given_a_trusted_directory_with_exit_scripts_when_entering_a_subdirectory_exit_scripts_are_not_called(
|
|
|
|
) {
|
|
|
|
Playground::setup("autoenv_test_8", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("time_to_cook_arepas");
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
&format!(
|
|
|
|
"{}\n{}",
|
2021-03-31 07:52:34 +02:00
|
|
|
SCRIPTS,
|
2021-02-19 02:24:27 +01:00
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
2021-08-28 14:59:09 +02:00
|
|
|
|
2021-02-19 02:24:27 +01:00
|
|
|
[scriptvars]
|
|
|
|
myscript = "echo myval"
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
2021-02-19 02:24:27 +01:00
|
|
|
),
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
|
|
|
nu!(cwd: dirs.test(), r#"
|
|
|
|
cd time_to_cook_arepas
|
2021-03-13 22:46:40 +01:00
|
|
|
ls | where name == "bye.txt" | length
|
2021-02-19 02:24:27 +01:00
|
|
|
"#)
|
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "0");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
2023-01-16 12:43:46 +01:00
|
|
|
fn given_a_hierarchy_of_trusted_directories_when_entering_in_any_nested_ones_should_carry_over_variables_set_from_the_root(
|
2021-02-19 02:24:27 +01:00
|
|
|
) {
|
|
|
|
Playground::setup("autoenv_test_9", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("nu_plugin_rb");
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
r#"[env]
|
|
|
|
organization = "nushell""#,
|
|
|
|
),
|
|
|
|
FileWithContent(
|
|
|
|
"nu_plugin_rb/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
language = "Ruby""#,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
2023-07-12 19:07:20 +02:00
|
|
|
nu!(cwd: dirs.test().parent().unwrap(), "
|
2022-12-22 21:30:10 +01:00
|
|
|
do { autoenv trust -q autoenv_test_9/nu_plugin_rb ; = null } # Silence autoenv trust -q message from output
|
2021-02-19 02:24:27 +01:00
|
|
|
cd autoenv_test_9/nu_plugin_rb
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.organization
|
2023-07-12 19:07:20 +02:00
|
|
|
")
|
2021-02-19 02:24:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "nushell");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
2023-01-16 12:43:46 +01:00
|
|
|
fn given_a_hierarchy_of_trusted_directories_nested_ones_should_overwrite_variables_from_parent_directories(
|
2021-02-19 02:24:27 +01:00
|
|
|
) {
|
|
|
|
Playground::setup("autoenv_test_10", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("nu_plugin_rb");
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
r#"[env]
|
|
|
|
organization = "nushell""#,
|
|
|
|
),
|
|
|
|
FileWithContent(
|
|
|
|
"nu_plugin_rb/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
organization = "Andrab""#,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
2023-07-12 19:07:20 +02:00
|
|
|
nu!(cwd: dirs.test().parent().unwrap(), "
|
2022-12-22 21:30:10 +01:00
|
|
|
do { autoenv trust -q autoenv_test_10/nu_plugin_rb ; = null } # Silence autoenv trust -q message from output
|
2021-02-19 02:24:27 +01:00
|
|
|
cd autoenv_test_10/nu_plugin_rb
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.organization
|
2023-07-12 19:07:20 +02:00
|
|
|
")
|
2021-02-19 02:24:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "Andrab");
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-31 07:52:34 +02:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
#[cfg(not(windows))] //TODO figure out why this test doesn't work on windows
|
|
|
|
fn local_config_should_not_be_added_when_running_scripts() {
|
|
|
|
Playground::setup("autoenv_test_10", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("foo");
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
r#"[env]
|
|
|
|
organization = "nu""#,
|
|
|
|
),
|
|
|
|
FileWithContent(
|
|
|
|
"foo/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
organization = "foo""#,
|
|
|
|
),
|
|
|
|
FileWithContent(
|
|
|
|
"script.nu",
|
2023-07-12 19:07:20 +02:00
|
|
|
"cd foo
|
|
|
|
echo $env.organization",
|
2021-03-31 07:52:34 +02:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
2023-07-12 19:07:20 +02:00
|
|
|
nu!(cwd: dirs.test(), "
|
2021-06-18 03:04:51 +02:00
|
|
|
do { autoenv trust -q foo } # Silence autoenv trust message from output
|
2021-03-31 07:52:34 +02:00
|
|
|
nu script.nu
|
2023-07-12 19:07:20 +02:00
|
|
|
")
|
2021-03-31 07:52:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "nu");
|
|
|
|
})
|
|
|
|
}
|
2021-02-19 02:24:27 +01:00
|
|
|
#[test]
|
|
|
|
#[serial]
|
2023-01-16 12:43:46 +01:00
|
|
|
fn given_a_hierarchy_of_trusted_directories_going_back_restores_overwritten_variables() {
|
2021-02-19 02:24:27 +01:00
|
|
|
Playground::setup("autoenv_test_11", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("nu_plugin_rb");
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
r#"[env]
|
|
|
|
organization = "nushell""#,
|
|
|
|
),
|
|
|
|
FileWithContent(
|
|
|
|
"nu_plugin_rb/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
organization = "Andrab""#,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || {
|
2023-07-12 19:07:20 +02:00
|
|
|
nu!(cwd: dirs.test().parent().unwrap(), "
|
2021-06-18 03:04:51 +02:00
|
|
|
do { autoenv trust -q autoenv_test_11/nu_plugin_rb } # Silence autoenv trust message from output
|
2021-02-19 02:24:27 +01:00
|
|
|
cd autoenv_test_11
|
|
|
|
cd nu_plugin_rb
|
2021-06-18 03:04:51 +02:00
|
|
|
do { rm ../.nu-env | ignore } # By deleting the root nu-env we have guarantees that the variable gets restored (not by autoenv when re-entering)
|
2021-02-19 02:24:27 +01:00
|
|
|
cd ..
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.organization
|
2023-07-12 19:07:20 +02:00
|
|
|
")
|
2021-02-19 02:24:27 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "nushell");
|
|
|
|
})
|
|
|
|
}
|
2021-03-31 07:52:34 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "which-support")]
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn local_config_env_var_present_and_removed_correctly() {
|
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
Playground::setup("autoenv_test", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("foo");
|
|
|
|
sandbox.mkdir("foo/bar");
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
"foo/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
testkey = "testvalue"
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
//Assert testkey is not present before entering directory
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo;
|
|
|
|
echo $env.testkey"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("Unknown"));
|
|
|
|
//Assert testkey is present in foo
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo; cd foo
|
|
|
|
echo $env.testkey"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "testvalue");
|
|
|
|
//Assert testkey is present also in subdirectories
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo; cd foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd bar
|
2023-07-12 19:07:20 +02:00
|
|
|
echo $env.testkey"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "testvalue");
|
|
|
|
//Assert testkey is present also when jumping over foo
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo; cd foo/bar
|
|
|
|
echo $env.testkey"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "testvalue");
|
|
|
|
//Assert testkey removed after leaving foo
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo; cd foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd ..
|
2023-07-12 19:07:20 +02:00
|
|
|
echo $env.testkey"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("Unknown"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "which-support")]
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn local_config_env_var_gets_overwritten() {
|
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
Playground::setup("autoenv_test", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("foo");
|
|
|
|
sandbox.mkdir("foo/bar");
|
|
|
|
sandbox.with_files(vec![
|
|
|
|
FileWithContent(
|
|
|
|
"foo/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
overwrite_me = "foo"
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
FileWithContent(
|
|
|
|
"foo/bar/.nu-env",
|
|
|
|
r#"[env]
|
|
|
|
overwrite_me = "bar"
|
|
|
|
"#,
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
//Assert overwrite_me is not present before entering directory
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo;
|
|
|
|
echo $env.overwrite_me"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("Unknown"));
|
|
|
|
//Assert overwrite_me is foo in foo
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo; cd foo
|
|
|
|
echo $env.overwrite_me"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
//Assert overwrite_me is bar in bar
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo
|
2021-06-18 03:04:51 +02:00
|
|
|
autoenv trust -q foo/bar
|
2021-03-31 07:52:34 +02:00
|
|
|
cd foo
|
|
|
|
cd bar
|
2023-07-12 19:07:20 +02:00
|
|
|
echo $env.overwrite_me"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
//Assert overwrite_me is present also when jumping over foo
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo; autoenv trust -q foo/bar; cd foo/bar
|
2022-02-02 21:59:01 +01:00
|
|
|
echo $env.overwrite_me
|
2023-07-12 19:07:20 +02:00
|
|
|
"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "bar");
|
|
|
|
//Assert overwrite_me removed after leaving bar
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-07-12 19:07:20 +02:00
|
|
|
"autoenv trust -q foo; autoenv trust -q foo/bar; cd foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd bar
|
|
|
|
cd ..
|
2023-07-12 19:07:20 +02:00
|
|
|
echo $env.overwrite_me"
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "which-support")]
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn autoenv_test_entry_scripts() {
|
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
Playground::setup("autoenv_test", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("foo/bar");
|
|
|
|
|
|
|
|
// Windows uses a different command to create an empty file so we need to have different content on windows.
|
|
|
|
let nu_env = if cfg!(target_os = "windows") {
|
|
|
|
r#"startup = ["echo nul > hello.txt"]"#
|
|
|
|
} else {
|
|
|
|
r#"startup = ["touch hello.txt"]"#
|
|
|
|
};
|
|
|
|
|
|
|
|
sandbox.with_files(vec![FileWithContent("foo/.nu-env", nu_env)]);
|
|
|
|
|
|
|
|
// Make sure entryscript is run when entering directory
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2021-06-18 03:04:51 +02:00
|
|
|
r#"autoenv trust -q foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd foo
|
|
|
|
ls | where name == "hello.txt" | get name"#
|
|
|
|
);
|
|
|
|
assert!(actual.out.contains("hello.txt"));
|
|
|
|
|
|
|
|
// Make sure entry scripts are also run when jumping over directory
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2021-06-18 03:04:51 +02:00
|
|
|
r#"autoenv trust -q foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd foo/bar
|
|
|
|
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(),
|
2021-06-18 03:04:51 +02:00
|
|
|
r#"autoenv trust -q foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd foo
|
|
|
|
rm hello.txt
|
|
|
|
cd bar
|
|
|
|
ls .. | where name == "../hello.txt" | length"#
|
|
|
|
);
|
2021-04-06 18:19:43 +02:00
|
|
|
assert!(actual.out.contains('0'));
|
2021-03-31 07:52:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "which-support")]
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
fn autoenv_test_exit_scripts() {
|
|
|
|
use nu_test_support::fs::Stub::FileWithContent;
|
|
|
|
Playground::setup("autoenv_test", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("foo/bar");
|
|
|
|
|
|
|
|
// Windows uses a different command to create an empty file so we need to have different content on windows.
|
|
|
|
let nu_env = r#"on_exit = ["touch bye.txt"]"#;
|
|
|
|
|
|
|
|
sandbox.with_files(vec![FileWithContent("foo/.nu-env", nu_env)]);
|
|
|
|
|
|
|
|
// Make sure exitscript is run
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2021-06-18 03:04:51 +02:00
|
|
|
r#"autoenv trust -q foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd foo
|
|
|
|
cd ..
|
|
|
|
ls foo | where name =~ "bye.txt" | length
|
2021-06-18 03:04:51 +02:00
|
|
|
rm foo/bye.txt | ignore; cd .
|
2021-03-31 07:52:34 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
|
|
|
|
// Entering a subdir should not trigger exitscripts
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2021-06-18 03:04:51 +02:00
|
|
|
r#"autoenv trust -q foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd foo
|
|
|
|
cd bar
|
|
|
|
ls .. | where name =~ "bye.txt" | length"#
|
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "0");
|
|
|
|
|
|
|
|
// Also run exitscripts when jumping over directory
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2021-06-18 03:04:51 +02:00
|
|
|
r#"autoenv trust -q foo
|
2021-03-31 07:52:34 +02:00
|
|
|
cd foo/bar
|
|
|
|
cd ../..
|
|
|
|
ls foo | where name =~ "bye.txt" | length
|
2021-06-18 03:04:51 +02:00
|
|
|
rm foo/bye.txt | ignore; cd ."#
|
2021-03-31 07:52:34 +02:00
|
|
|
);
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[serial]
|
|
|
|
#[cfg(unix)]
|
|
|
|
fn prepends_path_from_local_config() {
|
|
|
|
//If this test fails for you, make sure that your environment from which you start nu
|
|
|
|
//contains some env vars
|
|
|
|
Playground::setup("autoenv_test_1", |dirs, sandbox| {
|
|
|
|
sandbox.with_files(vec![FileWithContent(
|
|
|
|
".nu-env",
|
|
|
|
r#"
|
|
|
|
path = ["/hi", "/nushell"]
|
|
|
|
"#,
|
|
|
|
)]);
|
|
|
|
|
|
|
|
let expected = "[\"/hi\",\"/nushell\",";
|
|
|
|
|
|
|
|
let actual = Trusted::in_path(&dirs, || nu!(cwd: dirs.test(), "echo $nu.path | to json"));
|
|
|
|
// assert_eq!("", actual.out);
|
|
|
|
assert!(actual.out.starts_with(expected));
|
|
|
|
assert!(actual.out.len() > expected.len());
|
|
|
|
})
|
|
|
|
}
|