Add the load-env command (#3484)

* Add the load-env command

load-env can be used to add environment variables dynamically via an
InputStream. This allows developers to create tools that output environment
variables as key-value pairs, then have the user load those variables in using
load-env. This supplants most of the need for an `eval` command, which is
mostly used in POSIX envs for setting env vars.

Fixes #3481

* fixup! Add the load-env command
This commit is contained in:
Lily Mara
2021-05-25 11:18:20 -07:00
committed by GitHub
parent 65ee7aa372
commit 1ee51f2afa
4 changed files with 160 additions and 0 deletions

View File

@ -368,6 +368,68 @@ fn proper_shadow_let_env_aliases() {
assert_eq!(actual.out, "truefalsetrue");
}
#[test]
fn load_env_variable() {
let actual = nu!(
cwd: ".",
r#"
echo [[name, value]; [TESTENVVAR, "hello world"]] | load-env
echo $nu.env.TESTENVVAR
"#
);
assert_eq!(actual.out, "hello world");
}
#[test]
fn load_env_variable_arg() {
let actual = nu!(
cwd: ".",
r#"
load-env [[name, value]; [TESTENVVAR, "hello world"]]
echo $nu.env.TESTENVVAR
"#
);
assert_eq!(actual.out, "hello world");
}
#[test]
fn load_env_variable_arg_and_stream() {
let actual = nu!(
cwd: ".",
r#"
echo [[name, value]; [TESTVARSTREAM, "true"]] | load-env [[name, value]; [TESTVARARG, "false"]]
echo $nu.env | format "{TESTVARSTREAM} {TESTVARARG}"
"#
);
assert_eq!(actual.out, "true false");
}
#[test]
fn load_env_doesnt_leak() {
let actual = nu!(
cwd: ".",
r#"
do { echo [[name, value]; [xyz, "my message"]] | load-env }; echo $nu.env.xyz
"#
);
assert!(actual.err.contains("did you mean"));
}
#[test]
fn proper_shadow_load_env_aliases() {
let actual = nu!(
cwd: ".",
r#"
let-env DEBUG = true; echo $nu.env.DEBUG | autoview; do { echo [[name, value]; [DEBUG, false]] | load-env; echo $nu.env.DEBUG } | autoview; echo $nu.env.DEBUG
"#
);
assert_eq!(actual.out, "truefalsetrue");
}
#[test]
fn proper_shadow_let_aliases() {
let actual = nu!(