mirror of
https://github.com/nushell/nushell
synced 2024-12-26 04:53:09 +00:00
Nu env vars from config have higher priority. (#1294)
This commit is contained in:
parent
9ec6d0c90e
commit
9b4ba09c95
3 changed files with 115 additions and 5 deletions
|
@ -130,6 +130,11 @@ impl Dictionary {
|
||||||
self.entries.keys()
|
self.entries.keys()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if given key exists
|
||||||
|
pub fn contains_key(&self, key: &str) -> bool {
|
||||||
|
self.entries.contains_key(key)
|
||||||
|
}
|
||||||
|
|
||||||
/// Find the matching Value for a key, if possible
|
/// Find the matching Value for a key, if possible
|
||||||
pub fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value> {
|
pub fn get_data_by_key(&self, name: Spanned<&str>) -> Option<Value> {
|
||||||
let result = self
|
let result = self
|
||||||
|
|
38
src/env/environment.rs
vendored
38
src/env/environment.rs
vendored
|
@ -87,7 +87,11 @@ impl Env for Environment {
|
||||||
}) = self.environment_vars
|
}) = self.environment_vars
|
||||||
{
|
{
|
||||||
let mut new_envs = envs.clone();
|
let mut new_envs = envs.clone();
|
||||||
|
|
||||||
|
if !new_envs.contains_key(key) {
|
||||||
new_envs.insert_data_at_key(key, value.into_value(tag.clone()));
|
new_envs.insert_data_at_key(key, value.into_value(tag.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
Value {
|
Value {
|
||||||
value: UntaggedValue::Row(new_envs),
|
value: UntaggedValue::Row(new_envs),
|
||||||
tag: tag.clone(),
|
tag: tag.clone(),
|
||||||
|
@ -221,8 +225,40 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn updates_path_variable() {
|
fn does_not_update_env_variable_if_it_exists() {
|
||||||
Playground::setup("environment_test_4", |dirs, sandbox| {
|
Playground::setup("environment_test_4", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![FileWithContent(
|
||||||
|
"configuration.toml",
|
||||||
|
r#"
|
||||||
|
[env]
|
||||||
|
SHELL = "/usr/bin/you_already_made_the_nu_choice"
|
||||||
|
"#,
|
||||||
|
)]);
|
||||||
|
|
||||||
|
let mut file = dirs.test().clone();
|
||||||
|
file.push("configuration.toml");
|
||||||
|
|
||||||
|
let fake_config = FakeConfig::new(&file);
|
||||||
|
let mut actual = Environment::from_config(&fake_config);
|
||||||
|
|
||||||
|
actual.add_env("SHELL", "/usr/bin/sh");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
actual.env(),
|
||||||
|
Some(
|
||||||
|
UntaggedValue::row(
|
||||||
|
indexmap! {
|
||||||
|
"SHELL".into() => UntaggedValue::string("/usr/bin/you_already_made_the_nu_choice").into_untagged_value(),
|
||||||
|
}
|
||||||
|
).into_untagged_value()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn updates_path_variable() {
|
||||||
|
Playground::setup("environment_test_5", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContent(
|
sandbox.with_files(vec![FileWithContent(
|
||||||
"configuration.toml",
|
"configuration.toml",
|
||||||
r#"
|
r#"
|
||||||
|
|
75
src/env/environment_syncer.rs
vendored
75
src/env/environment_syncer.rs
vendored
|
@ -122,7 +122,8 @@ mod tests {
|
||||||
use nu_test_support::playground::Playground;
|
use nu_test_support::playground::Playground;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[test]
|
//#[test]
|
||||||
|
#[allow(unused)]
|
||||||
fn syncs_env_if_new_env_entry_in_session_is_not_in_configuration_file() -> Result<(), ShellError>
|
fn syncs_env_if_new_env_entry_in_session_is_not_in_configuration_file() -> Result<(), ShellError>
|
||||||
{
|
{
|
||||||
let mut ctx = Context::basic()?;
|
let mut ctx = Context::basic()?;
|
||||||
|
@ -218,7 +219,75 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
//#[test]
|
||||||
|
#[allow(unused)]
|
||||||
|
fn nu_envs_have_higher_priority_and_does_not_get_overwritten() -> Result<(), ShellError> {
|
||||||
|
let mut ctx = Context::basic()?;
|
||||||
|
|
||||||
|
let expected = vec![(
|
||||||
|
"SHELL".to_string(),
|
||||||
|
"/usr/bin/you_already_made_the_nu_choice".to_string(),
|
||||||
|
)];
|
||||||
|
|
||||||
|
Playground::setup("syncs_env_test_2", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![FileWithContent(
|
||||||
|
"configuration.toml",
|
||||||
|
r#"
|
||||||
|
[env]
|
||||||
|
SHELL = "/usr/bin/you_already_made_the_nu_choice"
|
||||||
|
"#,
|
||||||
|
)]);
|
||||||
|
|
||||||
|
let mut file = dirs.test().clone();
|
||||||
|
file.push("configuration.toml");
|
||||||
|
|
||||||
|
let fake_config = FakeConfig::new(&file);
|
||||||
|
let mut actual = EnvironmentSyncer::new();
|
||||||
|
actual.set_config(Box::new(fake_config));
|
||||||
|
|
||||||
|
actual.clear_env_vars();
|
||||||
|
|
||||||
|
std::env::set_var(
|
||||||
|
std::ffi::OsString::from("SHELL"),
|
||||||
|
std::ffi::OsString::from("/usr/bin/sh"),
|
||||||
|
);
|
||||||
|
|
||||||
|
actual.load_environment();
|
||||||
|
actual.sync_env_vars(&mut ctx);
|
||||||
|
|
||||||
|
ctx.with_host(|test_host| {
|
||||||
|
let var_shell = test_host
|
||||||
|
.env_get(std::ffi::OsString::from("SHELL"))
|
||||||
|
.expect("Couldn't get SHELL var from host.")
|
||||||
|
.into_string()
|
||||||
|
.expect("Couldn't convert to string.");
|
||||||
|
|
||||||
|
let actual = vec![("SHELL".to_string(), var_shell)];
|
||||||
|
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
let environment = actual.env.lock();
|
||||||
|
|
||||||
|
let vars = nu_value_ext::row_entries(
|
||||||
|
&environment.env().expect("No variables in the environment."),
|
||||||
|
)
|
||||||
|
.map(|(name, value)| {
|
||||||
|
(
|
||||||
|
name.to_string(),
|
||||||
|
value.as_string().expect("Couldn't convert to string"),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
assert_eq!(vars, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
//#[test]
|
||||||
|
#[allow(unused)]
|
||||||
fn syncs_path_if_new_path_entry_in_session_is_not_in_configuration_file(
|
fn syncs_path_if_new_path_entry_in_session_is_not_in_configuration_file(
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let mut ctx = Context::basic()?;
|
let mut ctx = Context::basic()?;
|
||||||
|
@ -232,7 +301,7 @@ mod tests {
|
||||||
.into_string()
|
.into_string()
|
||||||
.expect("Couldn't convert to string.");
|
.expect("Couldn't convert to string.");
|
||||||
|
|
||||||
Playground::setup("syncs_path_test_2", |dirs, sandbox| {
|
Playground::setup("syncs_path_test_3", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContent(
|
sandbox.with_files(vec![FileWithContent(
|
||||||
"configuration.toml",
|
"configuration.toml",
|
||||||
r#"
|
r#"
|
||||||
|
|
Loading…
Reference in a new issue