Patchwork D11738: tests: run the whole hg-core/path_auditor test in a clean temp dir

login
register
mail settings
Submitter phabricator
Date Nov. 9, 2021, 1:18 p.m.
Message ID <differential-rev-PHID-DREV-l6nfj34eliqs5gkhz3ma-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50063/
State Superseded
Headers show

Comments

phabricator - Nov. 9, 2021, 1:18 p.m.
Alphare created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  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 <https://phab.mercurial-scm.org/rHGdb2bc9e667a1250d7a1d26ad17bbc03a3079779f>.

REPOSITORY
  rHG Mercurial

BRANCH
  stable

REVISION DETAIL
  https://phab.mercurial-scm.org/D11738

AFFECTED FILES
  rust/hg-core/src/utils/path_auditor.rs

CHANGE DETAILS




To: Alphare, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/hg-core/src/utils/path_auditor.rs b/rust/hg-core/src/utils/path_auditor.rs
--- a/rust/hg-core/src/utils/path_auditor.rs
+++ b/rust/hg-core/src/utils/path_auditor.rs
@@ -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"),
             })
         );
     }