summaryrefslogtreecommitdiff
path: root/subplotlib
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2021-01-31 11:24:16 +0000
committerDaniel Silverstone <dsilvers@digital-scurf.org>2021-01-31 11:24:16 +0000
commit12550ed45e4828e544c7d5afdf48d657e5bd3021 (patch)
tree132c88e2908653d2ff32207e44cdea40666b70e2 /subplotlib
parent429a24b63b9bbb9021f6e6be701d75a093f34b07 (diff)
downloadsubplot-12550ed45e4828e544c7d5afdf48d657e5bd3021.tar.gz
subplotlib: Add external environment override for runcmd
In order to support runcmd environment overrides, we insert them during scenario startup to match how the Python test runtime does so. Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'subplotlib')
-rw-r--r--subplotlib/src/steplibrary/runcmd.rs18
1 files changed, 17 insertions, 1 deletions
diff --git a/subplotlib/src/steplibrary/runcmd.rs b/subplotlib/src/steplibrary/runcmd.rs
index 8aabffc..c215f60 100644
--- a/subplotlib/src/steplibrary/runcmd.rs
+++ b/subplotlib/src/steplibrary/runcmd.rs
@@ -1,5 +1,6 @@
//! Step library for running subprocesses as part of scenarios
+use env::vars_os;
use regex::RegexBuilder;
pub use super::datadir::Datadir;
@@ -23,6 +24,10 @@ pub struct Runcmd {
stderr: Vec<u8>,
}
+// Note, this prefix requires that the injection env vars must have
+// names which are valid unicode (and ideally ASCII)
+const ENV_INJECTION_PREFIX: &str = "SUBPLOT_ENV_";
+
#[cfg(not(windows))]
static DEFAULT_PATHS: &[&str] = &["/usr/bin", "/bin"];
@@ -41,6 +46,15 @@ impl ContextElement for Runcmd {
self.env.insert("SHELL".into(), "/bin/sh".into());
self.env
.insert("PATH".into(), env::join_paths(DEFAULT_PATHS.iter())?);
+ // Having assembled the 'default' environment, override it with injected
+ // content from the calling environment.
+ for (k, v) in env::vars_os() {
+ if let Some(k) = k.to_str() {
+ if let Some(k) = k.strip_prefix(ENV_INJECTION_PREFIX) {
+ self.env.insert(k.into(), v);
+ }
+ }
+ }
Ok(())
}
}
@@ -94,7 +108,8 @@ pub fn try_to_run(context: &ScenarioContext, argv0: &str, args: &str) {
proc.env_clear();
proc.env("HOME", &datadir);
proc.env("TMPDIR", &datadir);
- let curpath = context.with(
+
+ let mut curpath = context.with(
|runcmd: &Runcmd| {
let mut curpath = None;
for (k, v) in runcmd.env.iter() {
@@ -107,6 +122,7 @@ pub fn try_to_run(context: &ScenarioContext, argv0: &str, args: &str) {
},
false,
)?;
+
let path = context.with(
|runcmd: &Runcmd| {
Ok(env::join_paths(