From 09ff1410f2b55bbbe2216e6d47529d290dcfa43b Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Tue, 19 Sep 2023 09:00:42 -0400 Subject: [PATCH 1/2] Adding `env` key to config --- docs/config.md | 12 ++++++++++++ examples/envs/.config/ya.yml | 5 +++++ src/cmd.rs | 7 +++++++ src/config.rs | 27 ++++++++++++++++++++++++++- tests/env.rs | 19 +++++++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 examples/envs/.config/ya.yml create mode 100644 tests/env.rs diff --git a/docs/config.md b/docs/config.md index 85bca0f..0fba0f1 100644 --- a/docs/config.md +++ b/docs/config.md @@ -256,3 +256,15 @@ release: ``` Note that subcommands can be defined as any valid command that `ya` supports, including other subcommands. + +### Environment Variables + +If you'd like to explicitly list the environment variables that should be passed to a command, you can do so using the `env` key. For example, you might want to pass the `RUST_LOG` environment variable to a command. You can do that like so: + +```yml +debug: + prog: cargo + args: ["run"] + env: + RUST_LOG: debug +``` diff --git a/examples/envs/.config/ya.yml b/examples/envs/.config/ya.yml new file mode 100644 index 0000000..078b7ba --- /dev/null +++ b/examples/envs/.config/ya.yml @@ -0,0 +1,5 @@ +envs: + cmd: | + echo "$CUSTOM_ENV" + env: + CUSTOM_ENV: "custom value" diff --git a/src/cmd.rs b/src/cmd.rs index 1a800b8..0ee6063 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -98,6 +98,7 @@ impl Runnable for FullCommand { let prog = &self.prog.clone(); let cmd = &self.cmd; let chdir = &self.chdir; + let envs = &self.envs; let mut command = Command::new(prog); @@ -116,6 +117,12 @@ impl Runnable for FullCommand { command.current_dir(chdir); } + if let Some(envs) = envs { + for (key, value) in envs { + command.env(key, value); + } + } + let result = command.spawn()?.wait()?; if !result.success() { diff --git a/src/config.rs b/src/config.rs index 05b90fd..46f920e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -89,6 +89,7 @@ pub struct FullCommand { pub args: Vec, pub cmd: Option, pub chdir: Option, + pub envs: Option>, } impl Validatable for FullCommand { @@ -96,7 +97,7 @@ impl Validatable for FullCommand { if ! run_command_flags.quiet { let keys = m.keys().filter(|k| { let k = k.as_str().unwrap_or(""); - k != "prog" && k != "args" && k != "cmd" && k != "chdir" + k != "prog" && k != "args" && k != "cmd" && k != "chdir" && k != "env" }).collect::>(); if ! keys.is_empty() { @@ -157,12 +158,36 @@ impl TryFrom for FullCommand { }) .transpose()?; + let envs = m.get("env") + .map(|v| { + v.as_mapping() + .ok_or(anyhow::anyhow!("Invalid FullCommand: `env` is not a mapping")) + }) + .transpose()? + .map(|m| { + m.iter() + .map(|(k, v)| { + let k = k.as_str() + .ok_or(anyhow::anyhow!("Invalid FullCommand: `env` key is not a string"))? + .to_string(); + + let v = v.as_str() + .ok_or(anyhow::anyhow!("Invalid FullCommand: `env` value is not a string"))? + .to_string(); + + Ok((k, v)) + }) + .collect::>>() + }) + .transpose()?; + Ok(FullCommand { prog, args, cmd, chdir: chdir.map(|s| s.to_string()), + envs, }) } } diff --git a/tests/env.rs b/tests/env.rs new file mode 100644 index 0000000..7193e4f --- /dev/null +++ b/tests/env.rs @@ -0,0 +1,19 @@ +#[cfg(test)] +mod envs { + use anyhow::Result; + use assert_cmd::Command; + fn ya() -> Command { + Command::cargo_bin(env!("CARGO_PKG_NAME")).expect("Error invoking ya") + } + + #[test] + fn envs() -> Result<()> { + ya().args(["envs"]) + .current_dir("examples/envs") + .assert() + .success() + .stdout("custom value\n"); + + Ok(()) + } +} From e36f510e9bc2b8af0468cad3da6cea1652fbd029 Mon Sep 17 00:00:00 2001 From: Yousif Akbar <11247449+yhakbar@users.noreply.github.com> Date: Tue, 19 Sep 2023 09:07:25 -0400 Subject: [PATCH 2/2] Making template tests less brittle --- src/template.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/template.rs b/src/template.rs index 00716e9..0b313d9 100644 --- a/src/template.rs +++ b/src/template.rs @@ -767,8 +767,12 @@ mod get_templates { let templates = get_templates(&templates_dir)?; assert_eq!(templates.len(), 2); - assert_eq!(templates[0], "test.hbs"); - assert_eq!(templates[1], "test2.hbs"); + + let filename1 = "test.hbs".to_owned(); + let filename2 = "test2.hbs".to_owned(); + + assert!(templates.contains(&filename1)); + assert!(templates.contains(&filename2)); Ok(()) }