mirror of
https://github.com/nushell/nushell.git
synced 2024-11-24 17:34:00 +01:00
f6faf73e02
# Description Adds the `AddEnvVar` plugin call, which allows plugins to set environment variables in the caller's scope. This is the first engine call that mutates the caller's stack, and opens the door to more operations like this if needed. This also comes with an extra benefit: in doing this, I needed to refactor how context was handled, and I was able to avoid cloning `EngineInterface` / `Stack` / `Call` in most cases that plugin calls are used. They now only need to be cloned if the plugin call returns a stream. The performance increase is welcome (5.5x faster on `inc`!): ```nushell # Before > timeit { 1..100 | each { |i| $"2.0.($i)" | inc -p } } 405ms 941µs 952ns # After > timeit { 1..100 | each { |i| $"2.0.($i)" | inc -p } } 73ms 68µs 749ns ``` # User-Facing Changes - New engine call: `add_env_var()` - Performance enhancement for plugin calls # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting - [x] Document env manipulation in plugins guide - [x] Document `AddEnvVar` in plugin protocol
56 lines
1.3 KiB
Rust
56 lines
1.3 KiB
Rust
use nu_test_support::nu_with_plugins;
|
|
|
|
#[test]
|
|
fn get_env_by_name() {
|
|
let result = nu_with_plugins!(
|
|
cwd: ".",
|
|
plugin: ("nu_plugin_example"),
|
|
r#"
|
|
$env.FOO = bar
|
|
nu-example-env FOO | print
|
|
$env.FOO = baz
|
|
nu-example-env FOO | print
|
|
"#
|
|
);
|
|
assert!(result.status.success());
|
|
assert_eq!("barbaz", result.out);
|
|
}
|
|
|
|
#[test]
|
|
fn get_envs() {
|
|
let result = nu_with_plugins!(
|
|
cwd: ".",
|
|
plugin: ("nu_plugin_example"),
|
|
"$env.BAZ = foo; nu-example-env | get BAZ"
|
|
);
|
|
assert!(result.status.success());
|
|
assert_eq!("foo", result.out);
|
|
}
|
|
|
|
#[test]
|
|
fn get_current_dir() {
|
|
let cwd = std::env::current_dir()
|
|
.expect("failed to get current dir")
|
|
.join("tests")
|
|
.to_string_lossy()
|
|
.into_owned();
|
|
let result = nu_with_plugins!(
|
|
cwd: ".",
|
|
plugin: ("nu_plugin_example"),
|
|
"cd tests; nu-example-env --cwd"
|
|
);
|
|
assert!(result.status.success());
|
|
assert_eq!(cwd, result.out);
|
|
}
|
|
|
|
#[test]
|
|
fn set_env() {
|
|
let result = nu_with_plugins!(
|
|
cwd: ".",
|
|
plugin: ("nu_plugin_example"),
|
|
"nu-example-env NUSHELL_OPINION --set=rocks; $env.NUSHELL_OPINION"
|
|
);
|
|
assert!(result.status.success());
|
|
assert_eq!("rocks", result.out);
|
|
}
|