mirror of
https://github.com/nushell/nushell
synced 2024-12-28 05:53:09 +00:00
4af24363c2
# Description For years, Nushell has used `let-env` to set a single environment variable. As our work on scoping continued, we refined what it meant for a variable to be in scope using `let` but never updated how `let-env` would work. Instead, `let-env` confusingly created mutations to the command's copy of `$env`. So, to help fix the mental model and point people to the right way of thinking about what changing the environment means, this PR removes `let-env` to encourage people to think of it as updating the command's environment variable via mutation. Before: ``` let-env FOO = "BAR" ``` Now: ``` $env.FOO = "BAR" ``` It's also a good reminder that the environment owned by the command is in the `$env` variable rather than global like it is in other shells. # User-Facing Changes BREAKING CHANGE BREAKING CHANGE This completely removes `let-env FOO = "BAR"` so that we can focus on `$env.FOO = "BAR"`. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After / Before Submitting integration scripts to update: - ✔️ [starship](https://github.com/starship/starship/blob/master/src/init/starship.nu) - ✔️ [virtualenv](https://github.com/pypa/virtualenv/blob/main/src/virtualenv/activation/nushell/activate.nu) - ✔️ [atuin](https://github.com/ellie/atuin/blob/main/atuin/src/shell/atuin.nu) (PR: https://github.com/ellie/atuin/pull/1080) - ❌ [zoxide](https://github.com/ajeetdsouza/zoxide/blob/main/templates/nushell.txt) (PR: https://github.com/ajeetdsouza/zoxide/pull/587) - ✔️ [oh-my-posh](https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/src/shell/scripts/omp.nu) (pr: https://github.com/JanDeDobbeleer/oh-my-posh/pull/4011)
563 lines
13 KiB
Rust
563 lines
13 KiB
Rust
use nu_test_support::{nu, nu_repl_code};
|
|
use pretty_assertions::assert_eq;
|
|
|
|
fn env_change_hook_code_list(name: &str, code_list: &[&str]) -> String {
|
|
let mut list = String::new();
|
|
|
|
for code in code_list.iter() {
|
|
list.push_str("{ code: ");
|
|
list.push_str(code);
|
|
list.push_str(" }\n");
|
|
}
|
|
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
env_change: {{
|
|
{name} : [
|
|
{list}
|
|
]
|
|
}}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
fn env_change_hook(name: &str, code: &str) -> String {
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
env_change: {{
|
|
{name} : {code}
|
|
}}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
fn env_change_hook_code(name: &str, code: &str) -> String {
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
env_change: {{
|
|
{name} : {{
|
|
code: {code}
|
|
}}
|
|
}}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
fn env_change_hook_code_condition(name: &str, condition: &str, code: &str) -> String {
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
env_change: {{
|
|
{name} : {{
|
|
condition: {condition}
|
|
code: {code}
|
|
}}
|
|
}}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
fn pre_prompt_hook(code: &str) -> String {
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
pre_prompt: {code}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
fn pre_prompt_hook_code(code: &str) -> String {
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
pre_prompt: {{
|
|
code: {code}
|
|
}}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
fn pre_execution_hook(code: &str) -> String {
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
pre_execution: {code}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
fn pre_execution_hook_code(code: &str) -> String {
|
|
format!(
|
|
r#"$env.config = {{
|
|
hooks: {{
|
|
pre_execution: {{
|
|
code: {code}
|
|
}}
|
|
}}
|
|
}}"#
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_define_command() {
|
|
let inp = &[
|
|
&env_change_hook_code("FOO", r#"'def foo [] { "got foo!" }'"#),
|
|
"$env.FOO = 1",
|
|
"foo",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_define_variable() {
|
|
let inp = &[
|
|
&env_change_hook_code("FOO", r#"'let x = "spam"'"#),
|
|
"$env.FOO = 1",
|
|
"$x",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_define_env_var() {
|
|
let inp = &[
|
|
&env_change_hook_code("FOO", r#"'$env.SPAM = "spam"'"#),
|
|
"$env.FOO = 1",
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_define_alias() {
|
|
let inp = &[
|
|
&env_change_hook_code("FOO", r#"'alias spam = echo "spam"'"#),
|
|
"$env.FOO = 1",
|
|
"spam",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_simple_block_preserve_env_var() {
|
|
let inp = &[
|
|
&env_change_hook("FOO", r#"{|| $env.SPAM = "spam" }"#),
|
|
"$env.FOO = 1",
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_simple_block_list_shadow_env_var() {
|
|
let inp = &[
|
|
&env_change_hook(
|
|
"FOO",
|
|
r#"[
|
|
{|| $env.SPAM = "foo" }
|
|
{|| $env.SPAM = "spam" }
|
|
]"#,
|
|
),
|
|
"$env.FOO = 1",
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_block_preserve_env_var() {
|
|
let inp = &[
|
|
&env_change_hook_code("FOO", r#"{|| $env.SPAM = "spam" }"#),
|
|
"$env.FOO = 1",
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_prompt_define_command() {
|
|
let inp = &[
|
|
&pre_prompt_hook_code(r#"'def foo [] { "got foo!" }'"#),
|
|
"foo",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_prompt_simple_block_preserve_env_var() {
|
|
let inp = &[&pre_prompt_hook(r#"{|| $env.SPAM = "spam" }"#), "$env.SPAM"];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_prompt_simple_block_list_shadow_env_var() {
|
|
let inp = &[
|
|
&pre_prompt_hook(
|
|
r#"[
|
|
{|| $env.SPAM = "foo" }
|
|
{|| $env.SPAM = "spam" }
|
|
]"#,
|
|
),
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_prompt_block_preserve_env_var() {
|
|
let inp = &[
|
|
&pre_prompt_hook_code(r#"{|| $env.SPAM = "spam" }"#),
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_execution_define_command() {
|
|
let inp = &[
|
|
&pre_execution_hook_code(r#"'def foo [] { "got foo!" }'"#),
|
|
"foo",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_execution_simple_block_preserve_env_var() {
|
|
let inp = &[
|
|
&pre_execution_hook(r#"{|| $env.SPAM = "spam" }"#),
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_execution_simple_block_list_shadow_env_var() {
|
|
let inp = &[
|
|
&pre_execution_hook(
|
|
r#"[
|
|
{|| $env.SPAM = "foo" }
|
|
{|| $env.SPAM = "spam" }
|
|
]"#,
|
|
),
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_execution_block_preserve_env_var() {
|
|
let inp = &[
|
|
&pre_execution_hook_code(r#"{|| $env.SPAM = "spam" }"#),
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn pre_execution_commandline() {
|
|
let inp = &[
|
|
&pre_execution_hook_code(r#"{|| $env.repl_commandline = (commandline) }"#),
|
|
"$env.repl_commandline",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "$env.repl_commandline");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_shadow_command() {
|
|
let inp = &[
|
|
&env_change_hook_code_list(
|
|
"FOO",
|
|
&[
|
|
r#"'def foo [] { "got spam!" }'"#,
|
|
r#"'def foo [] { "got foo!" }'"#,
|
|
],
|
|
),
|
|
"$env.FOO = 1",
|
|
"foo",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "got foo!");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_block_dont_preserve_command() {
|
|
let inp = &[
|
|
&env_change_hook_code("FOO", r#"{|| def foo [] { "foo" } }"#),
|
|
"$env.FOO = 1",
|
|
"foo",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
#[cfg(windows)]
|
|
assert_ne!(actual_repl.out, "foo");
|
|
#[cfg(not(windows))]
|
|
assert!(actual_repl.err.contains("external_command"));
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_block_condition_pwd() {
|
|
let inp = &[
|
|
&env_change_hook_code_condition(
|
|
"PWD",
|
|
r#"{|before, after| ($after | path basename) == samples }"#,
|
|
r#"'source-env .nu-env'"#,
|
|
),
|
|
"cd samples",
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "spam");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_block_condition_correct_args() {
|
|
let inp = &[
|
|
r#"$env.FOO = 1"#,
|
|
&env_change_hook_code_condition(
|
|
"FOO",
|
|
r#"{|before, after| $before == 1 and $after == 2}"#,
|
|
r#"{|before, after| $env.SPAM = ($before == 1 and $after == 2) }"#,
|
|
),
|
|
"",
|
|
r#"$env.FOO = 2"#,
|
|
"$env.SPAM",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert_eq!(actual_repl.err, "");
|
|
assert_eq!(actual_repl.out, "true");
|
|
}
|
|
|
|
#[test]
|
|
fn env_change_dont_panic_with_many_args() {
|
|
let inp = &[
|
|
&env_change_hook_code("FOO", r#"{ |a, b, c| $env.SPAM = 'spam' }"#),
|
|
"$env.FOO = 1",
|
|
"",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert!(actual_repl.err.contains("incompatible_parameters"));
|
|
assert_eq!(actual_repl.out, "");
|
|
}
|
|
|
|
#[test]
|
|
fn err_hook_wrong_env_type_1() {
|
|
let inp = &[
|
|
r#"$env.config = {
|
|
hooks: {
|
|
env_change: {
|
|
FOO : 1
|
|
}
|
|
}
|
|
}"#,
|
|
"$env.FOO = 1",
|
|
"",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
dbg!(&actual_repl.err);
|
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
|
assert_eq!(actual_repl.out, "");
|
|
}
|
|
|
|
#[test]
|
|
fn err_hook_wrong_env_type_2() {
|
|
let inp = &[
|
|
r#"$env.config = {
|
|
hooks: {
|
|
env_change: "print spam"
|
|
}
|
|
}"#,
|
|
"",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert!(actual_repl.err.contains("type_mismatch"));
|
|
assert_eq!(actual_repl.out, "");
|
|
}
|
|
|
|
#[test]
|
|
fn err_hook_wrong_env_type_3() {
|
|
let inp = &[
|
|
r#"$env.config = {
|
|
hooks: {
|
|
env_change: {
|
|
FOO : {
|
|
code: 1
|
|
}
|
|
}
|
|
}
|
|
}"#,
|
|
"$env.FOO = 1",
|
|
"",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
|
assert_eq!(actual_repl.out, "");
|
|
}
|
|
|
|
#[test]
|
|
fn err_hook_non_boolean_condition_output() {
|
|
let inp = &[
|
|
r#"$env.config = {
|
|
hooks: {
|
|
env_change: {
|
|
FOO : {
|
|
condition: {|| "foo" }
|
|
code: "print spam"
|
|
}
|
|
}
|
|
}
|
|
}"#,
|
|
"$env.FOO = 1",
|
|
"",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
|
assert_eq!(actual_repl.out, "");
|
|
}
|
|
|
|
#[test]
|
|
fn err_hook_non_condition_not_a_block() {
|
|
let inp = &[
|
|
r#"$env.config = {
|
|
hooks: {
|
|
env_change: {
|
|
FOO : {
|
|
condition: "foo"
|
|
code: "print spam"
|
|
}
|
|
}
|
|
}
|
|
}"#,
|
|
"$env.FOO = 1",
|
|
"",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
|
assert_eq!(actual_repl.out, "");
|
|
}
|
|
|
|
#[test]
|
|
fn err_hook_parse_error() {
|
|
let inp = &[
|
|
r#"$env.config = {
|
|
hooks: {
|
|
env_change: {
|
|
FOO : {
|
|
code: "def foo { 'foo' }"
|
|
}
|
|
}
|
|
}
|
|
}"#,
|
|
"$env.FOO = 1",
|
|
"",
|
|
];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
|
assert_eq!(actual_repl.out, "");
|
|
}
|
|
|
|
#[test]
|
|
fn err_hook_dont_allow_string() {
|
|
let inp = &[&pre_prompt_hook(r#"'def foo [] { "got foo!" }'"#), "foo"];
|
|
|
|
let actual_repl = nu!(cwd: "tests/hooks", nu_repl_code(inp));
|
|
|
|
assert!(actual_repl.out.is_empty());
|
|
assert!(actual_repl.err.contains("unsupported_config_value"));
|
|
}
|