Skip to content

Commit

Permalink
fix: improve error handling in ResourceIndex and remove outdated test
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Nov 20, 2024
1 parent 33d37c2 commit 00b8d2e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 41 deletions.
8 changes: 5 additions & 3 deletions fs-index/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,10 +523,12 @@ impl<Id: ResourceId> ResourceIndex<Id> {
"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() {
Expand All @@ -552,7 +554,7 @@ impl<Id: ResourceId> ResourceIndex<Id> {
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);
}

Expand Down
38 changes: 0 additions & 38 deletions fs-index/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Id> =
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);
});
}

0 comments on commit 00b8d2e

Please sign in to comment.