diff --git a/fs-index/src/index.rs b/fs-index/src/index.rs index f8986fc..5cc2aa1 100644 --- a/fs-index/src/index.rs +++ b/fs-index/src/index.rs @@ -523,10 +523,12 @@ impl ResourceIndex { "Caller must ensure that the resource exists in the index: {:?}", path ); - let id = self.path_to_id.remove(path).unwrap(); + let id = self.path_to_id.remove(path).ok_or_else( + || anyhow::anyhow!("Resource with path {} is neither in the index nor in the file system. Make sure the index is up-to-date.", path.display()) + )?; self.id_to_paths .get_mut(&id.item) - .unwrap() + .expect("Resource ID not found in the ID to paths map") .remove(path); // If the ID has no paths, remove it from the ID to paths map if self.id_to_paths[&id.item].is_empty() { @@ -552,7 +554,7 @@ impl ResourceIndex { if let Some(prev_id) = self.path_to_id.get(path) { self.id_to_paths .get_mut(&prev_id.item) - .unwrap() + .expect("Resource ID not found in the ID to paths map") .remove(path); } diff --git a/fs-index/src/tests.rs b/fs-index/src/tests.rs index e6d0a78..e4685f2 100644 --- a/fs-index/src/tests.rs +++ b/fs-index/src/tests.rs @@ -956,41 +956,3 @@ fn test_track_move_to_subdirectory() { 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); - }); -}