|
| 1 | +use crate::{dir_structure, fixture_path}; |
| 2 | +use anyhow::Result; |
| 3 | +use git_odb::FindExt; |
| 4 | +use git_worktree::{copy_index, Options}; |
| 5 | +use std::fs; |
| 6 | + |
| 7 | +#[test] |
| 8 | +fn test_copy_index() -> Result<()> { |
| 9 | + let path = fixture_path("make_repo"); |
| 10 | + let path_git = path.join(".git"); |
| 11 | + let file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?; |
| 12 | + let output_dir = tempfile::tempdir()?; |
| 13 | + let output = output_dir.path(); |
| 14 | + let odb_handle = git_odb::at(path_git.join("objects")).unwrap(); |
| 15 | + |
| 16 | + let res = copy_index( |
| 17 | + &file, |
| 18 | + &output, |
| 19 | + move |oid, buf| odb_handle.find(oid, buf).ok(), |
| 20 | + Options::default(), |
| 21 | + ); |
| 22 | + |
| 23 | + let repo_files = dir_structure(&path); |
| 24 | + let copy_files = dir_structure(output); |
| 25 | + |
| 26 | + let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect(); |
| 27 | + let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect(); |
| 28 | + assert!(srepo_files == scopy_files); |
| 29 | + |
| 30 | + for (file1, file2) in repo_files.iter().zip(copy_files.iter()) { |
| 31 | + assert!(fs::metadata(file1)?.file_type() == fs::metadata(file2)?.file_type()); |
| 32 | + assert!(fs::read(file1)? == fs::read(file2)?); |
| 33 | + } |
| 34 | + |
| 35 | + res |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn test_copy_index_without_symlinks() -> Result<()> { |
| 40 | + let path = fixture_path("make_repo"); |
| 41 | + let path_git = path.join(".git"); |
| 42 | + let file = git_index::File::at(path_git.join("index"), git_index::decode::Options::default())?; |
| 43 | + let output_dir = tempfile::tempdir()?; |
| 44 | + let output = output_dir.path(); |
| 45 | + let odb_handle = git_odb::at(path_git.join("objects")).unwrap(); |
| 46 | + |
| 47 | + let res = copy_index( |
| 48 | + &file, |
| 49 | + &output, |
| 50 | + move |oid, buf| odb_handle.find(oid, buf).ok(), |
| 51 | + Options { symlinks: false }, |
| 52 | + ); |
| 53 | + |
| 54 | + let repo_files = dir_structure(&path); |
| 55 | + let copy_files = dir_structure(output); |
| 56 | + |
| 57 | + let srepo_files: Vec<_> = repo_files.iter().flat_map(|p| p.strip_prefix(&path)).collect(); |
| 58 | + let scopy_files: Vec<_> = copy_files.iter().flat_map(|p| p.strip_prefix(output)).collect(); |
| 59 | + assert!(srepo_files == scopy_files); |
| 60 | + |
| 61 | + for (file1, file2) in repo_files.iter().zip(copy_files.iter()) { |
| 62 | + if file2.is_symlink() { |
| 63 | + assert!(!file1.is_symlink()); |
| 64 | + } else { |
| 65 | + assert!(fs::metadata(file1)?.file_type() == fs::metadata(file2)?.file_type()); |
| 66 | + } |
| 67 | + assert!(fs::read(file1)? == fs::read(file2)?); |
| 68 | + } |
| 69 | + |
| 70 | + res |
| 71 | +} |
0 commit comments