tests: run the whole hg-core/path_auditor test in a clean temp dir
This makes the whole test happen in a clean temporary directory in
`$TMPDIR/$random`, and simplifies the test a bit by eliminating unnecessarily
dynamic path elements computations.
Before this patch, the first part of the test was happening in `/tmp` itself.
This allowed coincidentally named files placed in that directory to impact the
outcome of the test. Additionally, this made the second part of the test fail
on systems on which `$TMPDIR != /tmp`, because the inspected directory was
different from the one in which the mock files were being written. This fully
fixes the issue only partially solved in
db2bc9e667a1.
Differential Revision: https://phab.mercurial-scm.org/D11738
--- a/rust/hg-core/src/utils/path_auditor.rs Thu Nov 04 09:55:37 2021 +0100
+++ b/rust/hg-core/src/utils/path_auditor.rs Tue Nov 09 02:00:25 2021 +0100
@@ -180,12 +180,14 @@
#[cfg(test)]
mod tests {
use super::*;
- use crate::utils::files::get_path_from_bytes;
- use crate::utils::hg_path::path_to_hg_path_buf;
+ use std::fs::{create_dir, File};
+ use tempfile::tempdir;
#[test]
fn test_path_auditor() {
- let auditor = PathAuditor::new(get_path_from_bytes(b"/tmp"));
+ let base_dir = tempdir().unwrap();
+ let base_dir_path = base_dir.path();
+ let auditor = PathAuditor::new(base_dir_path);
let path = HgPath::new(b".hg/00changelog.i");
assert_eq!(
@@ -201,32 +203,20 @@
})
);
- use std::fs::{create_dir, File};
- use tempfile::tempdir;
-
- let base_dir = tempdir().unwrap();
- let base_dir_path = base_dir.path();
- let skip = base_dir_path.components().count() - 1;
- let a = base_dir_path.join("a");
- let b = base_dir_path.join("b");
- create_dir(&a).unwrap();
- let in_a_path = a.join("in_a");
- File::create(in_a_path).unwrap();
-
+ create_dir(&base_dir_path.join("realdir")).unwrap();
+ File::create(&base_dir_path.join("realdir/realfile")).unwrap();
// TODO make portable
- std::os::unix::fs::symlink(&a, &b).unwrap();
-
- let buf = b.join("in_a").components().skip(skip).collect::<PathBuf>();
- eprintln!("buf: {}", buf.display());
- let path = path_to_hg_path_buf(buf).unwrap();
+ std::os::unix::fs::symlink(
+ &base_dir_path.join("realdir"),
+ &base_dir_path.join("symlink"),
+ )
+ .unwrap();
+ let path = HgPath::new(b"symlink/realfile");
assert_eq!(
- auditor.audit_path(&path),
+ auditor.audit_path(path),
Err(HgPathError::TraversesSymbolicLink {
- path: path,
- symlink: path_to_hg_path_buf(
- b.components().skip(2).collect::<PathBuf>()
- )
- .unwrap()
+ path: path.to_owned(),
+ symlink: HgPathBuf::from_bytes(b"symlink"),
})
);
}