diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-31 09:57:38 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2021-01-31 09:57:38 +0000 |
commit | 429a24b63b9bbb9021f6e6be701d75a093f34b07 (patch) | |
tree | 5bd747d091bf521eddd28b78e3e3a0ea1e5cea6f /subplotlib | |
parent | 6b82498a88d0cade19d5fb5da4536ec79e20bd55 (diff) | |
download | subplot-429a24b63b9bbb9021f6e6be701d75a093f34b07.tar.gz |
subplotlib: Update runcmd for better PATH management
In preparation for supporting external environment inputs,
process PATH more effectively.
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'subplotlib')
-rw-r--r-- | subplotlib/src/steplibrary/runcmd.rs | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/subplotlib/src/steplibrary/runcmd.rs b/subplotlib/src/steplibrary/runcmd.rs index 02ec186..8aabffc 100644 --- a/subplotlib/src/steplibrary/runcmd.rs +++ b/subplotlib/src/steplibrary/runcmd.rs @@ -6,7 +6,8 @@ pub use super::datadir::Datadir; pub use crate::prelude::*; use std::collections::HashMap; -use std::ffi::OsString; +use std::env; +use std::ffi::{OsStr, OsString}; use std::io::Write; use std::path::PathBuf; use std::process::{Command, Stdio}; @@ -22,12 +23,24 @@ pub struct Runcmd { stderr: Vec<u8>, } +#[cfg(not(windows))] +static DEFAULT_PATHS: &[&str] = &["/usr/bin", "/bin"]; + +// Note, this comes from https://www.computerhope.com/issues/ch000549.htm#defaultpath +#[cfg(windows)] +static DEFAULT_PATHS: &[&str] = &[ + r"%SystemRoot%\system32", + r"%SystemRoot%", + r"%SystemRoot%\System32\Wbem", +]; + impl ContextElement for Runcmd { fn scenario_starts(&mut self) -> StepResult { self.env.drain(); self.paths.drain(..); - self.prepend_to_path("/usr/bin"); - self.prepend_to_path("/bin"); + self.env.insert("SHELL".into(), "/bin/sh".into()); + self.env + .insert("PATH".into(), env::join_paths(DEFAULT_PATHS.iter())?); Ok(()) } } @@ -79,23 +92,31 @@ pub fn try_to_run(context: &ScenarioContext, argv0: &str, args: &str) { proc.args(&shell_words::split(args)?); proc.current_dir(&datadir); proc.env_clear(); - proc.env("SHELL", "/bin/sh"); proc.env("HOME", &datadir); proc.env("TMPDIR", &datadir); - let path = context.with( - |runcmd: &Runcmd| Ok(std::env::join_paths(runcmd.paths.iter().rev())?), - false, - )?; - proc.env("PATH", path); - context.with( + let curpath = context.with( |runcmd: &Runcmd| { + let mut curpath = None; for (k, v) in runcmd.env.iter() { proc.env(k, v); + if k == "PATH" { + curpath = Some(v.to_owned()); + } } - Ok(()) + Ok(curpath) }, false, )?; + let path = context.with( + |runcmd: &Runcmd| { + Ok(env::join_paths( + env::split_paths(curpath.as_deref().unwrap_or_else(|| OsStr::new(""))) + .chain(runcmd.paths.iter().rev().map(PathBuf::from)), + )?) + }, + false, + )?; + proc.env("PATH", path); proc.stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); |