|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use git_worktree::fs; |
| 4 | +use tempfile::{tempdir, TempDir}; |
| 5 | + |
| 6 | +fn panic_on_find<'buf>(_oid: &git_hash::oid, _buf: &'buf mut Vec<u8>) -> std::io::Result<git_object::BlobRef<'buf>> { |
| 7 | + unreachable!("find should nto be called") |
| 8 | +} |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn root_is_assumed_to_exist_and_files_in_root_do_not_create_directory() -> crate::Result { |
| 12 | + let dir = tempdir()?; |
| 13 | + let mut cache = fs::Cache::new( |
| 14 | + dir.path().join("non-existing-root"), |
| 15 | + fs::cache::State::for_checkout(false, Default::default()), |
| 16 | + Default::default(), |
| 17 | + Vec::new(), |
| 18 | + Default::default(), |
| 19 | + ); |
| 20 | + assert_eq!(cache.num_mkdir_calls(), 0); |
| 21 | + |
| 22 | + let path = cache.at_path("hello", Some(false), panic_on_find)?.path(); |
| 23 | + assert!(!path.parent().unwrap().exists(), "prefix itself is never created"); |
| 24 | + assert_eq!(cache.num_mkdir_calls(), 0); |
| 25 | + Ok(()) |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn directory_paths_are_created_in_full() { |
| 30 | + let (mut cache, _tmp) = new_cache(); |
| 31 | + |
| 32 | + for (name, is_dir) in &[ |
| 33 | + ("dir", Some(true)), |
| 34 | + ("submodule", Some(true)), |
| 35 | + ("file", Some(false)), |
| 36 | + ("exe", Some(false)), |
| 37 | + ("link", None), |
| 38 | + ] { |
| 39 | + let path = cache |
| 40 | + .at_path(Path::new("dir").join(name), *is_dir, panic_on_find) |
| 41 | + .unwrap() |
| 42 | + .path(); |
| 43 | + assert!(path.parent().unwrap().is_dir(), "dir exists"); |
| 44 | + } |
| 45 | + |
| 46 | + assert_eq!(cache.num_mkdir_calls(), 3); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn existing_directories_are_fine() -> crate::Result { |
| 51 | + let (mut cache, tmp) = new_cache(); |
| 52 | + std::fs::create_dir(tmp.path().join("dir"))?; |
| 53 | + |
| 54 | + let path = cache.at_path("dir/file", Some(false), panic_on_find)?.path(); |
| 55 | + assert!(path.parent().unwrap().is_dir(), "directory is still present"); |
| 56 | + assert!(!path.exists(), "it won't create the file"); |
| 57 | + assert_eq!(cache.num_mkdir_calls(), 1); |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | + |
| 61 | +#[test] |
| 62 | +fn symlinks_or_files_in_path_are_forbidden_or_unlinked_when_forced() -> crate::Result { |
| 63 | + let (mut cache, tmp) = new_cache(); |
| 64 | + let forbidden = tmp.path().join("forbidden"); |
| 65 | + std::fs::create_dir(&forbidden)?; |
| 66 | + symlink::symlink_dir(&forbidden, tmp.path().join("link-to-dir"))?; |
| 67 | + std::fs::write(tmp.path().join("file-in-dir"), &[])?; |
| 68 | + |
| 69 | + for dirname in &["file-in-dir", "link-to-dir"] { |
| 70 | + cache.unlink_on_collision(false); |
| 71 | + let relative_path = format!("{}/file", dirname); |
| 72 | + assert_eq!( |
| 73 | + cache |
| 74 | + .at_path(&relative_path, Some(false), panic_on_find) |
| 75 | + .unwrap_err() |
| 76 | + .kind(), |
| 77 | + std::io::ErrorKind::AlreadyExists |
| 78 | + ); |
| 79 | + } |
| 80 | + assert_eq!( |
| 81 | + cache.num_mkdir_calls(), |
| 82 | + 2, |
| 83 | + "it tries to create each directory once, but it's a file" |
| 84 | + ); |
| 85 | + cache.reset_mkdir_calls(); |
| 86 | + for dirname in &["link-to-dir", "file-in-dir"] { |
| 87 | + cache.unlink_on_collision(true); |
| 88 | + let relative_path = format!("{}/file", dirname); |
| 89 | + let path = cache.at_path(&relative_path, Some(false), panic_on_find)?.path(); |
| 90 | + assert!(path.parent().unwrap().is_dir(), "directory was forcefully created"); |
| 91 | + assert!(!path.exists()); |
| 92 | + } |
| 93 | + assert_eq!( |
| 94 | + cache.num_mkdir_calls(), |
| 95 | + 4, |
| 96 | + "like before, but it unlinks what's there and tries again" |
| 97 | + ); |
| 98 | + Ok(()) |
| 99 | +} |
| 100 | + |
| 101 | +fn new_cache() -> (fs::Cache<'static>, TempDir) { |
| 102 | + let dir = tempdir().unwrap(); |
| 103 | + let cache = fs::Cache::new( |
| 104 | + dir.path(), |
| 105 | + fs::cache::State::for_checkout(false, Default::default()), |
| 106 | + Default::default(), |
| 107 | + Vec::new(), |
| 108 | + Default::default(), |
| 109 | + ); |
| 110 | + (cache, dir) |
| 111 | +} |
0 commit comments