#![allow(clippy::unwrap_used)]
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::Path;
use yog::cli_outbound::Cli;
use yog::git_env::INHERITED;
fn probe(dir: &Path) -> std::path::PathBuf {
let path = dir.join("probe");
fs::write(
&path,
"#!/bin/sh\nenv | sed -n 's/^\\(GIT_[A-Z_]*\\)=.*/\\1/p' | sort > \"$1/leaked\"\n\
git init -q --bare \"$1/probe.git\"\n\
git -C \"$1/probe.git\" rev-parse --absolute-git-dir > \"$1/aimed\"\n",
)
.unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o755)).unwrap();
path
}
#[test]
fn a_spawned_child_inherits_none_of_the_ambient_git_environment() {
let dir = tempfile::tempdir().unwrap();
let dir = dir.path().canonicalize().unwrap();
let decoy = dir.join("decoy.git");
let init = yog::git_env::git()
.args(["init", "-q", "--bare"])
.arg(&decoy)
.status()
.unwrap();
assert!(init.success(), "decoy repo");
for (var, value) in [
("GIT_DIR", decoy.clone()),
("GIT_INDEX_FILE", decoy.join("index")),
] {
unsafe { std::env::set_var(var, value) };
}
let work = dir.join("work");
fs::create_dir(&work).unwrap();
let stream = Cli::new(probe(&dir))
.run(&[work.to_str().unwrap()])
.unwrap();
stream.count();
let leaked = fs::read_to_string(work.join("leaked")).unwrap();
let aimers: Vec<&str> = leaked.lines().filter(|v| INHERITED.contains(v)).collect();
assert!(
aimers.is_empty(),
"the child inherited repo-aiming git vars: {aimers:?}"
);
assert_eq!(
fs::read_to_string(work.join("aimed")).unwrap().trim(),
work.join("probe.git").to_str().unwrap(),
"the git the child forked answered about the decoy, not its own fixture"
);
}