|
| 1 | +mod checkout { |
| 2 | + #[cfg(unix)] |
| 3 | + use std::os::unix::prelude::MetadataExt; |
| 4 | + use std::{ |
| 5 | + fs, |
| 6 | + path::{Path, PathBuf}, |
| 7 | + }; |
| 8 | + use tempfile::TempDir; |
| 9 | + |
| 10 | + use git_object::bstr::ByteSlice; |
| 11 | + use git_odb::FindExt; |
| 12 | + use git_worktree::index; |
| 13 | + |
| 14 | + use crate::fixture_path; |
| 15 | + |
| 16 | + #[test] |
| 17 | + fn allow_symlinks() -> crate::Result { |
| 18 | + let opts = Default::default(); |
| 19 | + let (source_tree, destination) = setup_fixture_with_options(opts)?; |
| 20 | + |
| 21 | + assert_equality(&source_tree, &destination, opts.symlinks)?; |
| 22 | + Ok(()) |
| 23 | + } |
| 24 | + |
| 25 | + #[test] |
| 26 | + fn symlinks_become_files_if_disabled() -> crate::Result { |
| 27 | + let opts = index::checkout::Options { |
| 28 | + symlinks: false, |
| 29 | + ..Default::default() |
| 30 | + }; |
| 31 | + let (source_tree, destination) = setup_fixture_with_options(opts)?; |
| 32 | + |
| 33 | + assert_equality(&source_tree, &destination, opts.symlinks)?; |
| 34 | + |
| 35 | + Ok(()) |
| 36 | + } |
| 37 | + |
| 38 | + fn assert_equality(source_tree: &Path, destination: &TempDir, allow_symlinks: bool) -> crate::Result { |
| 39 | + let source_files = dir_structure(source_tree); |
| 40 | + let worktree_files = dir_structure(&destination); |
| 41 | + |
| 42 | + assert_eq!( |
| 43 | + stripped_prefix(source_tree, &source_files), |
| 44 | + stripped_prefix(&destination, &worktree_files), |
| 45 | + ); |
| 46 | + |
| 47 | + for (source_file, worktree_file) in source_files.iter().zip(worktree_files.iter()) { |
| 48 | + if !allow_symlinks && source_file.is_symlink() { |
| 49 | + assert!(!worktree_file.is_symlink()); |
| 50 | + assert_eq!(fs::read(worktree_file)?.to_path()?, fs::read_link(source_file)?); |
| 51 | + } else { |
| 52 | + assert_eq!(fs::read(source_file)?, fs::read(worktree_file)?); |
| 53 | + #[cfg(unix)] |
| 54 | + assert_eq!( |
| 55 | + fs::symlink_metadata(source_file)?.mode() & 0o700, |
| 56 | + fs::symlink_metadata(worktree_file)?.mode() & 0o700, |
| 57 | + "permissions of source and checked out file are comparable" |
| 58 | + ); |
| 59 | + } |
| 60 | + } |
| 61 | + Ok(()) |
| 62 | + } |
| 63 | + |
| 64 | + pub fn dir_structure<P: AsRef<std::path::Path>>(path: P) -> Vec<std::path::PathBuf> { |
| 65 | + let path = path.as_ref(); |
| 66 | + let mut files: Vec<_> = walkdir::WalkDir::new(path) |
| 67 | + .follow_links(false) |
| 68 | + .into_iter() |
| 69 | + .filter_entry(|e| e.path() == path || !e.file_name().to_string_lossy().starts_with('.')) |
| 70 | + .flatten() |
| 71 | + .filter_map(|e| (!e.path().is_dir()).then(|| e.path().to_path_buf())) |
| 72 | + .collect(); |
| 73 | + files.sort(); |
| 74 | + files |
| 75 | + } |
| 76 | + |
| 77 | + fn setup_fixture_with_options(opts: git_worktree::index::checkout::Options) -> crate::Result<(PathBuf, TempDir)> { |
| 78 | + let source_tree = fixture_path("make_repo"); |
| 79 | + let git_dir = source_tree.join(".git"); |
| 80 | + let mut index = git_index::File::at(git_dir.join("index"), Default::default())?; |
| 81 | + let odb = git_odb::at(git_dir.join("objects"))?; |
| 82 | + let destination = tempfile::tempdir()?; |
| 83 | + |
| 84 | + index::checkout( |
| 85 | + &mut index, |
| 86 | + &destination, |
| 87 | + move |oid, buf| odb.find_blob(oid, buf).ok(), |
| 88 | + opts, |
| 89 | + )?; |
| 90 | + Ok((source_tree, destination)) |
| 91 | + } |
| 92 | + |
| 93 | + fn stripped_prefix(prefix: impl AsRef<Path>, source_files: &Vec<PathBuf>) -> Vec<&Path> { |
| 94 | + source_files.iter().flat_map(|p| p.strip_prefix(&prefix)).collect() |
| 95 | + } |
| 96 | +} |
0 commit comments