rust/hg-core/src/utils/path_auditor.rs
branchstable
changeset 48307 ab2dfc993b5c
parent 46245 db2bc9e667a1
child 51120 532e74ad3ff6
equal deleted inserted replaced
48300:809e780c72e5 48307:ab2dfc993b5c
   178 }
   178 }
   179 
   179 
   180 #[cfg(test)]
   180 #[cfg(test)]
   181 mod tests {
   181 mod tests {
   182     use super::*;
   182     use super::*;
   183     use crate::utils::files::get_path_from_bytes;
   183     use std::fs::{create_dir, File};
   184     use crate::utils::hg_path::path_to_hg_path_buf;
   184     use tempfile::tempdir;
   185 
   185 
   186     #[test]
   186     #[test]
   187     fn test_path_auditor() {
   187     fn test_path_auditor() {
   188         let auditor = PathAuditor::new(get_path_from_bytes(b"/tmp"));
   188         let base_dir = tempdir().unwrap();
       
   189         let base_dir_path = base_dir.path();
       
   190         let auditor = PathAuditor::new(base_dir_path);
   189 
   191 
   190         let path = HgPath::new(b".hg/00changelog.i");
   192         let path = HgPath::new(b".hg/00changelog.i");
   191         assert_eq!(
   193         assert_eq!(
   192             auditor.audit_path(path),
   194             auditor.audit_path(path),
   193             Err(HgPathError::InsideDotHg(path.to_owned()))
   195             Err(HgPathError::InsideDotHg(path.to_owned()))
   199                 path: path.to_owned(),
   201                 path: path.to_owned(),
   200                 nested_repo: HgPathBuf::from_bytes(b"this/is/nested")
   202                 nested_repo: HgPathBuf::from_bytes(b"this/is/nested")
   201             })
   203             })
   202         );
   204         );
   203 
   205 
   204         use std::fs::{create_dir, File};
   206         create_dir(&base_dir_path.join("realdir")).unwrap();
   205         use tempfile::tempdir;
   207         File::create(&base_dir_path.join("realdir/realfile")).unwrap();
   206 
       
   207         let base_dir = tempdir().unwrap();
       
   208         let base_dir_path = base_dir.path();
       
   209         let skip = base_dir_path.components().count() - 1;
       
   210         let a = base_dir_path.join("a");
       
   211         let b = base_dir_path.join("b");
       
   212         create_dir(&a).unwrap();
       
   213         let in_a_path = a.join("in_a");
       
   214         File::create(in_a_path).unwrap();
       
   215 
       
   216         // TODO make portable
   208         // TODO make portable
   217         std::os::unix::fs::symlink(&a, &b).unwrap();
   209         std::os::unix::fs::symlink(
   218 
   210             &base_dir_path.join("realdir"),
   219         let buf = b.join("in_a").components().skip(skip).collect::<PathBuf>();
   211             &base_dir_path.join("symlink"),
   220         eprintln!("buf: {}", buf.display());
   212         )
   221         let path = path_to_hg_path_buf(buf).unwrap();
   213         .unwrap();
       
   214         let path = HgPath::new(b"symlink/realfile");
   222         assert_eq!(
   215         assert_eq!(
   223             auditor.audit_path(&path),
   216             auditor.audit_path(path),
   224             Err(HgPathError::TraversesSymbolicLink {
   217             Err(HgPathError::TraversesSymbolicLink {
   225                 path: path,
   218                 path: path.to_owned(),
   226                 symlink: path_to_hg_path_buf(
   219                 symlink: HgPathBuf::from_bytes(b"symlink"),
   227                     b.components().skip(2).collect::<PathBuf>()
       
   228                 )
       
   229                 .unwrap()
       
   230             })
   220             })
   231         );
   221         );
   232     }
   222     }
   233 }
   223 }