diff --git a/src/main.rs b/src/main.rs index 2e66aee..473148f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,7 @@ use crate::core::resolve_envrc_context; QUICKENV_NO_SHIM=1 to disable loading of .envrc, and effectively disable shims QUICKENV_SHIM_EXEC=1 to directly exec() shims instead of spawning them as subprocess. This can help with attaching debuggers. QUICKENV_NO_SHIM_WARNINGS=1 to disable nags about running 'quickenv shim' everytime a new binary is added + QUICKENV_PRELUDE='eval \"$(direnv stdlib)\"' can be overridden to something else to get rid of the direnv stdlib and therefore direnv dependency, or to inject additional code before executing each envrc. " )] struct Args { @@ -275,49 +276,45 @@ fn compute_envvars(quickenv_home: &Path) -> Result<(), Error> { })?; let mut temp_script = tempfile::NamedTempFile::new_in(&ctx.root) .with_context(|| format!("failed to create temporary file at {}", ctx.root.display()))?; + let temp_script_path = temp_script.path().to_owned(); - temp_script - .write_all( - br#" + let write_failure = || { + format!( + "failed to write to temporary file at {}", + temp_script_path.display() + ) + }; + + let prelude = std::env::var("QUICKENV_PRELUDE") + .unwrap_or_else(|_| r#"eval "$(direnv stdlib)""#.to_owned()); + + write!( + temp_script, + r##" echo '// BEGIN QUICKENV-BEFORE' env echo '// END QUICKENV-BEFORE' -eval "$(direnv stdlib)" -"#, - ) - .with_context(|| { - format!( - "failed to write to temporary file at {}", - temp_script.path().display() - ) - })?; +{prelude} +"##, + ) + .with_context(write_failure)?; - io::copy(&mut ctx.envrc, &mut temp_script).with_context(|| { - format!( - "failed to write to temporary file at {}", - temp_script.path().display() - ) - })?; + io::copy(&mut ctx.envrc, &mut temp_script).with_context(write_failure)?; - temp_script - .write_all( - br#" + write!( + temp_script, + r##" echo '// BEGIN QUICKENV-AFTER' env echo '// END QUICKENV-AFTER' -"#, - ) - .with_context(|| { - format!( - "failed to write to temporary file at {}", - temp_script.path().display() - ) - })?; +"## + ) + .with_context(write_failure)?; signals::pass_control_to_shim(); let mut cmd = process::Command::new("bash") - .arg(temp_script.path()) + .arg(&temp_script_path) .env("QUICKENV_NO_SHIM", "1") .stdin(Stdio::inherit()) .stdout(Stdio::piped()) diff --git a/tests/acceptance_helpers.rs b/tests/acceptance_helpers.rs index 1aef7a1..f5b8476 100644 --- a/tests/acceptance_helpers.rs +++ b/tests/acceptance_helpers.rs @@ -11,7 +11,7 @@ use tempfile::TempDir; pub struct Harness { pub env: BTreeMap, - pub home: TempDir, + pub home: Option, pub cwd: PathBuf, } @@ -73,6 +73,14 @@ impl Harness { } } +impl Drop for Harness { + fn drop(&mut self) { + if std::env::var("QUICKENV_TEST_LEAK_FILES").unwrap_or_default() == "1" { + std::mem::forget(self.home.take()); + } + } +} + pub fn setup() -> Result { let home = tempfile::tempdir()?; // on macos, /tmp is a symlink to /private/..., so sometimes the path reported by tmpdir is not @@ -82,7 +90,7 @@ pub fn setup() -> Result { create_dir_all(&cwd)?; let mut harness = Harness { env: BTreeMap::new(), - home, + home: Some(home), cwd, }; @@ -114,6 +122,7 @@ macro_rules! assert_cmd { insta_cmd::assert_cmd_snapshot!( Command::new($harness.which(stringify!($program_name))?) .current_dir(&$harness.cwd) + .env_remove("QUICKENV_PRELUDE") .envs(&$harness.env) $(.arg($arg))*, $($insta_args)*