Skip to content

Commit

Permalink
test(fs-index): add a couple of test for update_one to cover rename…
Browse files Browse the repository at this point in the history
… and move

Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Nov 11, 2024
1 parent bf2534f commit 8bb6858
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions fs-index/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions fs-index/examples/index_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
5 changes: 4 additions & 1 deletion fs-index/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,8 +497,11 @@ impl<Id: ResourceId> ResourceIndex<Id> {
/// - 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<P: AsRef<Path>>(
&mut self,
relative_path: P,
Expand Down
81 changes: 81 additions & 0 deletions fs-index/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Id> =
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<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 8bb6858

Please sign in to comment.