From 762e528ec58ac6eedfbfc9355d33ec38c691d087 Mon Sep 17 00:00:00 2001 From: Lily Mara <6109875+lily-mara@users.noreply.github.com> Date: Fri, 30 Jul 2021 14:10:28 -0700 Subject: [PATCH] Support equals sign in shorthand environment variable values (#3869) Some environment variables, such as `RUST_LOG` include equals signs. Nushell should support this in the shorthand environment variable syntax so that developers using these variables can control them easily. We accomplish this by swapping `std::str::split` for `std::str::splitn`, which ensures that we only consider the first equals sign in the string instead of all of them, which we did previously. Closes #3867 --- crates/nu-parser/src/parse.rs | 2 +- tests/shell/environment/env.rs | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index 1a80966259..30c657c438 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -1997,7 +1997,7 @@ fn expand_shorthand_forms( if lite_pipeline.commands[0].parts[0].contains('=') && !lite_pipeline.commands[0].parts[0].starts_with('$') { - let assignment: Vec<_> = lite_pipeline.commands[0].parts[0].split('=').collect(); + let assignment: Vec<_> = lite_pipeline.commands[0].parts[0].splitn(2, '=').collect(); if assignment.len() != 2 { ( lite_pipeline.clone(), diff --git a/tests/shell/environment/env.rs b/tests/shell/environment/env.rs index 75b2f3f1e8..960c7c8843 100644 --- a/tests/shell/environment/env.rs +++ b/tests/shell/environment/env.rs @@ -14,6 +14,52 @@ fn env_shorthand() { assert_eq!(actual.out, "bar"); } +#[test] +fn env_shorthand_with_equals() { + let actual = nu!(cwd: ".", r#" + RUST_LOG=my_module=info $nu.env.RUST_LOG + "#); + assert_eq!(actual.out, "my_module=info"); +} + +#[test] +fn env_shorthand_with_comma_equals() { + let actual = nu!(cwd: ".", r#" + RUST_LOG=info,my_module=info $nu.env.RUST_LOG + "#); + assert_eq!(actual.out, "info,my_module=info"); +} + +#[test] +fn env_shorthand_with_comma_colons_equals() { + let actual = nu!(cwd: ".", r#" + RUST_LOG=info,my_module=info,lib_crate::lib_mod=trace $nu.env.RUST_LOG + "#); + assert_eq!(actual.out, "info,my_module=info,lib_crate::lib_mod=trace"); +} + +#[test] +fn env_shorthand_multi_second_with_comma_colons_equals() { + let actual = nu!(cwd: ".", r#" + FOO=bar RUST_LOG=info,my_module=info,lib_crate::lib_mod=trace $nu.env.FOO + $nu.env.RUST_LOG + "#); + assert_eq!( + actual.out, + "barinfo,my_module=info,lib_crate::lib_mod=trace" + ); +} + +#[test] +fn env_shorthand_multi_first_with_comma_colons_equals() { + let actual = nu!(cwd: ".", r#" + RUST_LOG=info,my_module=info,lib_crate::lib_mod=trace FOO=bar $nu.env.FOO + $nu.env.RUST_LOG + "#); + assert_eq!( + actual.out, + "barinfo,my_module=info,lib_crate::lib_mod=trace" + ); +} + #[test] fn env_shorthand_multi() { let actual = nu!(cwd: ".", r#"