Fix let-env (#3057)

This commit is contained in:
Jonathan Turner 2021-02-15 20:58:51 +13:00 committed by GitHub
parent b202951c1d
commit 48a90fea70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 39 deletions

View file

@ -184,7 +184,7 @@ pub async fn process_script(
trace!("{:#?}", block); trace!("{:#?}", block);
let env = ctx.get_env(); let env = ctx.get_env();
ctx.scope.add_env(env); ctx.scope.add_env_to_base(env);
let result = run_block(&block, ctx, input_stream).await; let result = run_block(&block, ctx, input_stream).await;
match result { match result {
Ok(input) => { Ok(input) => {

View file

@ -186,6 +186,12 @@ impl Scope {
frame.env.extend(env_vars) frame.env.extend(env_vars)
} }
} }
pub fn add_env_to_base(&self, env_vars: IndexMap<String, String>) {
if let Some(frame) = self.frames.lock().first_mut() {
frame.env.extend(env_vars)
}
}
} }
impl ParserScope for Scope { impl ParserScope for Scope {

View file

@ -220,50 +220,50 @@ fn autoenv() {
}) })
} }
// #[cfg(feature = "which")] #[cfg(feature = "which")]
// #[test] #[test]
// fn nu_let_env_overwrites() { fn nu_let_env_overwrites() {
// Playground::setup("syncs_env_test_1", |dirs, sandbox| { Playground::setup("syncs_env_test_1", |dirs, sandbox| {
// sandbox.with_files(vec![FileWithContent( sandbox.with_files(vec![FileWithContent(
// "configuration.toml", "configuration.toml",
// r#" r#"
// [env] [env]
// SHELL = "/usr/bin/you_already_made_the_nu_choice" SHELL = "/usr/bin/you_already_made_the_nu_choice"
// "#, "#,
// )]); )]);
// let mut file = dirs.test().clone(); let mut file = dirs.test().clone();
// file.push("configuration.toml"); file.push("configuration.toml");
// let fake_config = FakeConfig::new(&file); let fake_config = FakeConfig::new(&file);
// let mut actual = EnvironmentSyncer::new(); let mut actual = EnvironmentSyncer::new();
// actual.set_config(Box::new(fake_config)); actual.set_config(Box::new(fake_config));
// // Here, the environment variables from the current session // Here, the environment variables from the current session
// // are cleared since we will load and set them from the // are cleared since we will load and set them from the
// // configuration file (if any) // configuration file (if any)
// actual.clear_env_vars(&mut ctx); actual.clear_env_vars(&mut ctx);
// // Nu loads the environment variables from the configuration file (if any) // Nu loads the environment variables from the configuration file (if any)
// actual.load_environment(); actual.load_environment();
// // By this point, Nu has already loaded the environment variables // By this point, Nu has already loaded the environment variables
// // stored in the configuration file. Before continuing we check // stored in the configuration file. Before continuing we check
// // if any new environment variables have been added from the ones loaded // if any new environment variables have been added from the ones loaded
// // in the configuration file. // in the configuration file.
// // //
// // Nu sees the missing "USER" variable and accounts for it. // Nu sees the missing "USER" variable and accounts for it.
// actual.sync_env_vars(&mut ctx); actual.sync_env_vars(&mut ctx);
// let actual = nu!( let actual = nu!(
// cwd: dirs.test(), cwd: dirs.test(),
// r#"let-env SHELL = bob r#"let-env SHELL = bob
// echo $nu.env.SHELL echo $nu.env.SHELL
// "# "#
// ); );
// assert!(actual.out.ends_with("set_in_foo")) assert!(actual.out.ends_with("set_in_foo"))
// }); });
// } }
#[test] #[test]
fn invocation_properly_redirects() { fn invocation_properly_redirects() {