Skip to content

Commit

Permalink
make prelude configurable (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
untitaker authored Jun 9, 2023
1 parent 829e06d commit e485b6a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
57 changes: 27 additions & 30 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
Expand Down
13 changes: 11 additions & 2 deletions tests/acceptance_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use tempfile::TempDir;

pub struct Harness {
pub env: BTreeMap<OsString, OsString>,
pub home: TempDir,
pub home: Option<TempDir>,
pub cwd: PathBuf,
}

Expand Down Expand Up @@ -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<Harness, Error> {
let home = tempfile::tempdir()?;
// on macos, /tmp is a symlink to /private/..., so sometimes the path reported by tmpdir is not
Expand All @@ -82,7 +90,7 @@ pub fn setup() -> Result<Harness, Error> {
create_dir_all(&cwd)?;
let mut harness = Harness {
env: BTreeMap::new(),
home,
home: Some(home),
cwd,
};

Expand Down Expand Up @@ -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)*
Expand Down

0 comments on commit e485b6a

Please sign in to comment.