use std::collections::HashMap;
use std::time::{Duration, Instant};
use rpi_tools::{OsExecutionEnv, Shell, ShellExecOptions};
fn scratch() -> std::path::PathBuf {
let tmp = std::env::temp_dir().join(format!("pi-tools-noninteractive-{}", std::process::id()));
let _ = std::fs::create_dir_all(&tmp);
tmp
}
struct EnvGuard(&'static str);
impl Drop for EnvGuard {
fn drop(&mut self) {
std::env::remove_var(self.0);
}
}
const PRINT_ENV: &str = r#"printf 'git=[%s] gcm=[%s] ssh=[%s]\n' "$GIT_TERMINAL_PROMPT" "$GCM_INTERACTIVE" "$SSH_ASKPASS_REQUIRE""#;
async fn shell_or_skip() -> Option<OsExecutionEnv> {
let env = OsExecutionEnv::with_cwd(scratch());
match env.exec("true", ShellExecOptions::default()).await {
Ok(_) => Some(env),
Err(error) => {
eprintln!("skipping: no usable shell ({error})");
None
}
}
}
#[tokio::test]
async fn shell_children_are_non_interactive_by_default_and_can_opt_back_in() {
for key in [
"GIT_TERMINAL_PROMPT",
"GCM_INTERACTIVE",
"SSH_ASKPASS_REQUIRE",
] {
std::env::remove_var(key);
}
let Some(_probe) = shell_or_skip().await else {
return;
};
let defaults = OsExecutionEnv::with_cwd(scratch())
.exec(PRINT_ENV, ShellExecOptions::default())
.await
.expect("exec");
assert_eq!(
defaults.stdout.trim(),
"git=[0] gcm=[Never] ssh=[never]",
"a spawned shell must not be able to ask the terminal for credentials"
);
let mut base = HashMap::new();
base.insert("GIT_TERMINAL_PROMPT".to_string(), "1".to_string());
let base_env = OsExecutionEnv::with_cwd(scratch()).with_shell_env(base);
let out = base_env
.exec(PRINT_ENV, ShellExecOptions::default())
.await
.expect("exec");
assert!(
out.stdout.contains("git=[1]"),
"shell_env must override the default: {:?}",
out.stdout
);
let mut per_call = HashMap::new();
per_call.insert("GIT_TERMINAL_PROMPT".to_string(), "2".to_string());
let out = base_env
.exec(
PRINT_ENV,
ShellExecOptions {
env: Some(per_call),
..Default::default()
},
)
.await
.expect("exec");
assert!(
out.stdout.contains("git=[2]"),
"per-call env must override shell_env: {:?}",
out.stdout
);
let out = OsExecutionEnv::with_cwd(scratch())
.exec(
PRINT_ENV,
ShellExecOptions {
inherit_env: false,
..Default::default()
},
)
.await
.expect("exec");
assert_eq!(
out.stdout.trim(),
"git=[0] gcm=[Never] ssh=[never]",
"the defaults are not part of the inherited environment"
);
let _guard = EnvGuard("GIT_TERMINAL_PROMPT");
std::env::set_var("GIT_TERMINAL_PROMPT", "3");
let out = OsExecutionEnv::with_cwd(scratch())
.exec(PRINT_ENV, ShellExecOptions::default())
.await
.expect("exec");
assert!(
out.stdout.contains("git=[3]"),
"an explicit host export must win: {:?}",
out.stdout
);
drop(_guard);
let git_available = std::process::Command::new("git")
.arg("--version")
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.is_ok();
if !git_available {
eprintln!("skipping git credential check: git not on PATH");
return;
}
let started = Instant::now();
let result = tokio::time::timeout(
Duration::from_secs(15),
OsExecutionEnv::with_cwd(scratch()).exec(
"printf 'protocol=https\\nhost=example.invalid\\n\\n' | git -c credential.helper= credential fill 2>&1; echo \"rc=$?\"",
ShellExecOptions {
timeout: Some(10.0),
..Default::default()
},
),
)
.await;
let elapsed = started.elapsed();
let output = match result {
Ok(Ok(out)) => out,
Ok(Err(error)) => panic!("git credential fill failed as a tool error: {error:?}"),
Err(_) => panic!("git credential fill hung: interactive prompts were not disabled"),
};
assert!(
output.stdout.contains("terminal prompts disabled"),
"the credential request must fail fast in captured stderr, not paint over the TUI: {:?}",
output.stdout
);
assert!(
elapsed < Duration::from_secs(10),
"git waited on a terminal for {elapsed:?}"
);
}