forked from extern/nushell
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)
329 lines
9.2 KiB
Rust
329 lines
9.2 KiB
Rust
use nu_test_support::fs::AbsolutePath;
|
|
use nu_test_support::fs::Stub::{FileWithContent, FileWithContentToBeTrimmed};
|
|
use nu_test_support::nu;
|
|
use nu_test_support::pipeline;
|
|
use nu_test_support::playground::Playground;
|
|
|
|
#[should_panic]
|
|
#[test]
|
|
fn sources_also_files_under_custom_lib_dirs_path() {
|
|
Playground::setup("source_test_1", |dirs, nu| {
|
|
let file = AbsolutePath::new(dirs.test().join("config.toml"));
|
|
let library_path = AbsolutePath::new(dirs.test().join("lib"));
|
|
|
|
nu.with_config(&file);
|
|
nu.with_files(vec![FileWithContent(
|
|
"config.toml",
|
|
&format!(
|
|
r#"
|
|
lib_dirs = ["{library_path}"]
|
|
skip_welcome_message = true
|
|
"#
|
|
),
|
|
)]);
|
|
|
|
nu.within("lib").with_files(vec![FileWithContent(
|
|
"my_library.nu",
|
|
r#"
|
|
source-env my_library/main.nu
|
|
"#,
|
|
)]);
|
|
nu.within("lib/my_library").with_files(vec![FileWithContent(
|
|
"main.nu",
|
|
r#"
|
|
$env.hello = "hello nu"
|
|
"#,
|
|
)]);
|
|
|
|
let actual = nu!(
|
|
cwd: ".", pipeline(
|
|
r#"
|
|
source-env my_library.nu ;
|
|
|
|
hello
|
|
"#
|
|
));
|
|
|
|
assert_eq!(actual.out, "hello nu");
|
|
})
|
|
}
|
|
|
|
fn try_source_foo_with_double_quotes_in(testdir: &str, playdir: &str) {
|
|
Playground::setup(playdir, |dirs, sandbox| {
|
|
let testdir = String::from(testdir);
|
|
let mut foo_file = testdir.clone();
|
|
foo_file.push_str("/foo.nu");
|
|
|
|
sandbox.mkdir(&testdir);
|
|
sandbox.with_files(vec![FileWithContent(&foo_file, "echo foo")]);
|
|
|
|
let cmd = String::from("source-env ") + r#"""# + foo_file.as_str() + r#"""#;
|
|
|
|
let actual = nu!(cwd: dirs.test(), &cmd);
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
});
|
|
}
|
|
|
|
fn try_source_foo_with_single_quotes_in(testdir: &str, playdir: &str) {
|
|
Playground::setup(playdir, |dirs, sandbox| {
|
|
let testdir = String::from(testdir);
|
|
let mut foo_file = testdir.clone();
|
|
foo_file.push_str("/foo.nu");
|
|
|
|
sandbox.mkdir(&testdir);
|
|
sandbox.with_files(vec![FileWithContent(&foo_file, "echo foo")]);
|
|
|
|
let cmd = String::from("source-env ") + r#"'"# + foo_file.as_str() + r#"'"#;
|
|
|
|
let actual = nu!(cwd: dirs.test(), &cmd);
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
});
|
|
}
|
|
|
|
fn try_source_foo_without_quotes_in(testdir: &str, playdir: &str) {
|
|
Playground::setup(playdir, |dirs, sandbox| {
|
|
let testdir = String::from(testdir);
|
|
let mut foo_file = testdir.clone();
|
|
foo_file.push_str("/foo.nu");
|
|
|
|
sandbox.mkdir(&testdir);
|
|
sandbox.with_files(vec![FileWithContent(&foo_file, "echo foo")]);
|
|
|
|
let cmd = String::from("source-env ") + foo_file.as_str();
|
|
|
|
let actual = nu!(cwd: dirs.test(), &cmd);
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn sources_unicode_file_in_normal_dir() {
|
|
try_source_foo_with_single_quotes_in("foo", "source_test_1");
|
|
try_source_foo_with_double_quotes_in("foo", "source_test_2");
|
|
try_source_foo_without_quotes_in("foo", "source_test_3");
|
|
}
|
|
|
|
#[test]
|
|
fn sources_unicode_file_in_unicode_dir_without_spaces_1() {
|
|
try_source_foo_with_single_quotes_in("🚒", "source_test_4");
|
|
try_source_foo_with_double_quotes_in("🚒", "source_test_5");
|
|
try_source_foo_without_quotes_in("🚒", "source_test_6");
|
|
}
|
|
|
|
#[cfg(not(windows))] // ':' is not allowed in Windows paths
|
|
#[test]
|
|
fn sources_unicode_file_in_unicode_dir_without_spaces_2() {
|
|
try_source_foo_with_single_quotes_in(":fire_engine:", "source_test_7");
|
|
try_source_foo_with_double_quotes_in(":fire_engine:", "source_test_8");
|
|
try_source_foo_without_quotes_in(":fire_engine:", "source_test_9");
|
|
}
|
|
|
|
#[test]
|
|
fn sources_unicode_file_in_unicode_dir_with_spaces_1() {
|
|
// this one fails
|
|
try_source_foo_with_single_quotes_in("e-$ èрт🚒♞中片-j", "source_test_8");
|
|
// this one passes
|
|
try_source_foo_with_double_quotes_in("e-$ èрт🚒♞中片-j", "source_test_9");
|
|
}
|
|
|
|
#[cfg(not(windows))] // ':' is not allowed in Windows paths
|
|
#[test]
|
|
fn sources_unicode_file_in_unicode_dir_with_spaces_2() {
|
|
try_source_foo_with_single_quotes_in("e-$ èрт:fire_engine:♞中片-j", "source_test_10");
|
|
try_source_foo_with_double_quotes_in("e-$ èрт:fire_engine:♞中片-j", "source_test_11");
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn sources_unicode_file_in_non_utf8_dir() {
|
|
// How do I create non-UTF-8 path???
|
|
}
|
|
|
|
#[ignore]
|
|
#[test]
|
|
fn can_source_dynamic_path() {
|
|
Playground::setup("can_source_dynamic_path", |dirs, sandbox| {
|
|
let foo_file = "foo.nu";
|
|
|
|
sandbox.with_files(vec![FileWithContent(foo_file, "echo foo")]);
|
|
|
|
let cmd = format!("let file = `{foo_file}`; source-env $file");
|
|
let actual = nu!(cwd: dirs.test(), &cmd);
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn source_env_eval_export_env() {
|
|
Playground::setup("source_env_eval_export_env", |dirs, sandbox| {
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
"spam.nu",
|
|
r#"
|
|
export-env { $env.FOO = 'foo' }
|
|
"#,
|
|
)]);
|
|
|
|
let inp = &[r#"source-env spam.nu"#, r#"$env.FOO"#];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn source_env_eval_export_env_hide() {
|
|
Playground::setup("source_env_eval_export_env", |dirs, sandbox| {
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
"spam.nu",
|
|
r#"
|
|
export-env { hide-env FOO }
|
|
"#,
|
|
)]);
|
|
|
|
let inp = &[
|
|
r#"$env.FOO = 'foo'"#,
|
|
r#"source-env spam.nu"#,
|
|
r#"$env.FOO"#,
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert!(actual.err.contains("not_found"));
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn source_env_do_cd() {
|
|
Playground::setup("source_env_do_cd", |dirs, sandbox| {
|
|
sandbox
|
|
.mkdir("test1/test2")
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
"test1/test2/spam.nu",
|
|
r#"
|
|
cd test1/test2
|
|
"#,
|
|
)]);
|
|
|
|
let inp = &[
|
|
r#"source-env test1/test2/spam.nu"#,
|
|
r#"$env.PWD | path basename"#,
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert_eq!(actual.out, "test2");
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn source_env_do_cd_file_relative() {
|
|
Playground::setup("source_env_do_cd_file_relative", |dirs, sandbox| {
|
|
sandbox
|
|
.mkdir("test1/test2")
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
"test1/test2/spam.nu",
|
|
r#"
|
|
cd ($env.FILE_PWD | path join '..')
|
|
"#,
|
|
)]);
|
|
|
|
let inp = &[
|
|
r#"source-env test1/test2/spam.nu"#,
|
|
r#"$env.PWD | path basename"#,
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert_eq!(actual.out, "test1");
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn source_env_dont_cd_overlay() {
|
|
Playground::setup("source_env_dont_cd_overlay", |dirs, sandbox| {
|
|
sandbox
|
|
.mkdir("test1/test2")
|
|
.with_files(vec![FileWithContentToBeTrimmed(
|
|
"test1/test2/spam.nu",
|
|
r#"
|
|
overlay new spam
|
|
cd test1/test2
|
|
overlay hide spam
|
|
"#,
|
|
)]);
|
|
|
|
let inp = &[
|
|
r#"source-env test1/test2/spam.nu"#,
|
|
r#"$env.PWD | path basename"#,
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert_eq!(actual.out, "source_env_dont_cd_overlay");
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn source_env_is_scoped() {
|
|
Playground::setup("source_env_is_scoped", |dirs, sandbox| {
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
"spam.nu",
|
|
r#"
|
|
def no-name-similar-to-this [] { 'no-name-similar-to-this' }
|
|
alias nor-similar-to-this = echo 'nor-similar-to-this'
|
|
"#,
|
|
)]);
|
|
|
|
let inp = &[r#"source-env spam.nu"#, r#"no-name-similar-to-this"#];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert!(actual.err.contains("executable was not found"));
|
|
|
|
let inp = &[r#"source-env spam.nu"#, r#"nor-similar-to-this"#];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert!(actual.err.contains("executable was not found"));
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn source_env_const_file() {
|
|
Playground::setup("source_env_const_file", |dirs, sandbox| {
|
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
|
"spam.nu",
|
|
r#"
|
|
$env.FOO = 'foo'
|
|
"#,
|
|
)]);
|
|
|
|
let inp = &[
|
|
r#"const file = 'spam.nu'"#,
|
|
r#"source-env $file"#,
|
|
r#"$env.FOO"#,
|
|
];
|
|
|
|
let actual = nu!(cwd: dirs.test(), pipeline(&inp.join("; ")));
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn source_respects_early_return() {
|
|
let actual = nu!(
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
r#"
|
|
source early_return.nu
|
|
"#
|
|
));
|
|
|
|
assert!(actual.err.is_empty());
|
|
}
|