From 8bb68584d0605333f588098315fa6341f100ee80 Mon Sep 17 00:00:00 2001 From: Tarek Date: Mon, 11 Nov 2024 17:04:18 +0200 Subject: [PATCH] test(fs-index): add a couple of test for `update_one` to cover rename and move Signed-off-by: Tarek --- fs-index/Cargo.toml | 1 + fs-index/examples/index_watch.rs | 2 + fs-index/src/index.rs | 5 +- fs-index/src/tests.rs | 81 ++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) diff --git a/fs-index/Cargo.toml b/fs-index/Cargo.toml index 4057f8aa..03393d49 100644 --- a/fs-index/Cargo.toml +++ b/fs-index/Cargo.toml @@ -33,6 +33,7 @@ watch = ["notify", "notify-debouncer-full", "futures", "async-stream", "tokio"] [dev-dependencies] uuid = { version = "1.6.1", features = ["v4"] } +env_logger = "0.11" # benchmarking criterion = { version = "0.5", features = ["html_reports"] } tempfile = "3.10" diff --git a/fs-index/examples/index_watch.rs b/fs-index/examples/index_watch.rs index f105a3eb..ef01c5b8 100644 --- a/fs-index/examples/index_watch.rs +++ b/fs-index/examples/index_watch.rs @@ -11,6 +11,8 @@ use fs_index::{watch_index, WatchEvent}; /// changed files. #[tokio::main] async fn main() -> Result<()> { + env_logger::init(); + // Change this to the path of the directory you want to watch let root = Path::new("test-assets"); diff --git a/fs-index/src/index.rs b/fs-index/src/index.rs index e50eab82..156d965c 100644 --- a/fs-index/src/index.rs +++ b/fs-index/src/index.rs @@ -497,8 +497,11 @@ impl ResourceIndex { /// - The index is up-to-date with the file system except for the updated /// resource /// - In case of a addition, the resource was not already in the index - /// - In case of a modification or removal, the resource was already in the + /// - In case of a removal, the resource was already in the /// index + /// - In case of a move or rename, `update_one()` should be called twice: + /// once with the old path to remove the previous entry, and once with the + /// new path to add the updated entry pub fn update_one>( &mut self, relative_path: P, diff --git a/fs-index/src/tests.rs b/fs-index/src/tests.rs index bd15ac6f..e6d0a786 100644 --- a/fs-index/src/tests.rs +++ b/fs-index/src/tests.rs @@ -913,3 +913,84 @@ fn test_track_modification_with_collision() { assert_eq!(index.collisions().len(), 1, "{:?}", index); }); } + +/// Test for calling `update_one()` on a file that was moved from the root +/// directory to a subdirectory. +/// +/// ## Test scenario: +/// - Create a file within the temporary directory. +/// - Build a resource index in the temporary directory. +/// - Move the file to a subdirectory. +/// - Call `update_one()` 2 times with the relative path of the moved file. +/// - Assert that the index contains the expected number of entries with the +/// correct IDs and paths after the move. +#[test] +fn test_track_move_to_subdirectory() { + for_each_type!(Crc32, Blake3 => { + let temp_dir = TempDir::with_prefix("ark_test_track_move_to_subdirectory") + .expect("Failed to create temp dir"); + let root_path = temp_dir.path(); + + let file_path = root_path.join("file.txt"); + fs::write(&file_path, "file content").expect("Failed to write to file"); + let file_id = Id::from_path(&file_path).expect("Failed to get checksum"); + + let mut index: ResourceIndex = + ResourceIndex::build(root_path).expect("Failed to build index"); + + let subdirectory_path = root_path.join("subdirectory"); + fs::create_dir(&subdirectory_path).expect("Failed to create subdirectory"); + + let moved_file_path = subdirectory_path.join("file.txt"); + fs::rename(&file_path, &moved_file_path).expect("Failed to move file"); + + // We need to call `update_one()` 2 times because the file was moved to a + // subdirectory. + index.update_one("file.txt").expect("Failed to update index"); + index.update_one("subdirectory/file.txt").expect("Failed to update index"); + + assert_eq!(index.len(), 1, "{:?}", index); + let resource_by_path = index + .get_resource_by_path("subdirectory/file.txt") + .expect("Failed to get resource"); + assert_eq!(*resource_by_path.id(), file_id); + }); +} + +/// Test for calling `update_one()` on a file that was renamed. +/// +/// ## Test scenario: +/// - Create a file within the temporary directory. +/// - Build a resource index in the temporary directory. +/// - Rename the file. +/// - Call `update_one()` 2 times with the relative path of the renamed file. +/// - Assert that the index contains the expected number of entries with the +/// correct IDs and paths after the rename. +#[test] +fn test_track_rename() { + for_each_type!(Crc32, Blake3 => { + let temp_dir = TempDir::with_prefix("ark_test_track_rename") + .expect("Failed to create temp dir"); + let root_path = temp_dir.path(); + + let file_path = root_path.join("file.txt"); + fs::write(&file_path, "file content").expect("Failed to write to file"); + let file_id = Id::from_path(&file_path).expect("Failed to get checksum"); + + let mut index: ResourceIndex = + ResourceIndex::build(root_path).expect("Failed to build index"); + + let renamed_file_path = root_path.join("renamed_file.txt"); + fs::rename(&file_path, &renamed_file_path).expect("Failed to rename file"); + + // We need to call `update_one()` 2 times because the file was renamed. + index.update_one("file.txt").expect("Failed to update index"); + index.update_one("renamed_file.txt").expect("Failed to update index"); + + assert_eq!(index.len(), 1, "{:?}", index); + let resource_by_path = index + .get_resource_by_path("renamed_file.txt") + .expect("Failed to get resource"); + assert_eq!(*resource_by_path.id(), file_id); + }); +}