clap-host tests: create the garbage-file test directory beside the test binary, not under TMPDIR

This commit is contained in:
Dylan M. Taylor
2026-11-02 21:23:07 -05:00
parent 5afc2aab66
commit 57f8e788cb
+17 -6
View File
@@ -222,15 +222,26 @@ mod tests {
assert!(plugin_info("reverb").is_none());
}
/// A new directory under the temp dir that this test alone created, readable only by its user
/// on Unix. Creation fails if the path already exists: the test then loads native libraries
/// from it, so it must never reuse a directory someone else could have put a library in.
/// A new directory that this test alone created, readable only by its user on Unix. Creation
/// fails if the path already exists: the test then loads native libraries from it, so it must
/// never reuse a directory someone else could have put a library in. It lives next to the test
/// binary (in the build directory), not under the temp dir: `TMPDIR`/`TEMP` can name a shared
/// directory without a sticky bit or with inheritable write access, where another user could
/// rename or populate it. Anyone who can write to the build directory can already replace the
/// test binary itself, so this parent adds no new trust.
fn fresh_private_dir(prefix: &str) -> std::path::PathBuf {
let exe = std::env::current_exe().expect("test binary path");
let parent = exe.parent().expect("test binary has a parent directory");
let nanos = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |t| t.as_nanos());
let d = std::env::temp_dir().join(format!("{prefix}-{}-{nanos}", std::process::id()));
let mut builder = std::fs::DirBuilder::new();
let d = parent.join(format!("{prefix}-{}-{nanos}", std::process::id()));
#[cfg(unix)]
std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
let builder = {
let mut b = std::fs::DirBuilder::new();
std::os::unix::fs::DirBuilderExt::mode(&mut b, 0o700);
b
};
#[cfg(not(unix))]
let builder = std::fs::DirBuilder::new();
builder.create(&d).unwrap_or_else(|e| panic!("cannot create a fresh test directory {}: {e}", d.display()));
d
}