Skip to content

Commit

Permalink
test(fs-index): print index in case of assert failure
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Jul 18, 2024
1 parent b503ebc commit d22b9e2
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions fs-index/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ fn test_store_and_load_index<H: ResourceId>() {

let index: ResourceIndex<H> =
ResourceIndex::build(root_path).expect("Failed to build index");
assert_eq!(index.len(), 1);
assert_eq!(index.len(), 1, "{:?}", index);
index.store().expect("Failed to store index");

let loaded_index =
load_or_build_index(root_path, false).expect("Failed to load index");

assert_eq!(index, loaded_index);
assert_eq!(index, loaded_index, "{:?} != {:?}", index, loaded_index);
}

/// Test storing and loading the resource index with collisions.
Expand Down Expand Up @@ -155,15 +155,15 @@ fn test_store_and_load_index_with_collisions<H: ResourceId>() {
let index: ResourceIndex<H> =
ResourceIndex::build(root_path).expect("Failed to build index");
let checksum = H::from_path(&file_path).expect("Failed to get checksum");
assert_eq!(index.len(), 4);
assert_eq!(index.collisions().len(), 1);
assert_eq!(index.collisions()[&checksum].len(), 4);
assert_eq!(index.len(), 4, "{:?}", index);
assert_eq!(index.collisions().len(), 1, "{:?}", index);
assert_eq!(index.collisions()[&checksum].len(), 4, "{:?}", index);
index.store().expect("Failed to store index");

let loaded_index =
load_or_build_index(root_path, false).expect("Failed to load index");

assert_eq!(index, loaded_index);
assert_eq!(index, loaded_index, "{:?} != {:?}", index, loaded_index);
}

/// Test building an index with a file.
Expand All @@ -186,12 +186,16 @@ fn test_build_index_with_file<H: ResourceId>() {
.expect("Failed to get indexed resource");

let index = ResourceIndex::build(root_path).expect("Failed to build index");
assert_eq!(index.len(), 1);
assert_eq!(index.len(), 1, "{:?}", index);

let resource = index
.get_resource_by_path("file.txt")
.expect("Failed to get resource");
assert_eq!(resource, &expected_resource);
assert_eq!(
resource, &expected_resource,
"{:?} != {:?}",
resource, expected_resource
);
}

/// Test building an index with an empty file.
Expand All @@ -214,7 +218,7 @@ fn test_build_index_with_empty_file<H: ResourceId>() {

let index: ResourceIndex<H> =
ResourceIndex::build(root_path).expect("Failed to build index");
assert_eq!(index.len(), 1);
assert_eq!(index.len(), 1, "{:?}", index);
}

/// Test building an index with a directory.
Expand All @@ -240,12 +244,16 @@ fn test_build_index_with_directory<H: ResourceId>() {
.expect("Failed to get indexed resource");

let index = ResourceIndex::build(root_path).expect("Failed to build index");
assert_eq!(index.len(), 1);
assert_eq!(index.len(), 1, "{:?}", index);

let resource = index
.get_resource_by_path("dir/file.txt")
.expect("Failed to get resource");
assert_eq!(resource, &expected_resource);
assert_eq!(
resource, &expected_resource,
"{:?} != {:?}",
resource, expected_resource
);
}

/// Test building an index with multiple files.
Expand Down Expand Up @@ -275,17 +283,17 @@ fn test_build_index_with_multiple_files<H: ResourceId>() {
.expect("Failed to get indexed resource");

let index = ResourceIndex::build(root_path).expect("Failed to build index");
assert_eq!(index.len(), 2);
assert_eq!(index.len(), 2, "{:?}", index);

let resource = index
.get_resource_by_path("file1.txt")
.expect("Failed to get resource");
assert_eq!(resource, &expected_resource1);
assert_eq!(resource, &expected_resource1, "{:?}", resource);

let resource = index
.get_resource_by_path("file2.txt")
.expect("Failed to get resource");
assert_eq!(resource, &expected_resource2);
assert_eq!(resource, &expected_resource2, "{:?}", resource);
}

/// Test building an index with multiple directories.
Expand Down Expand Up @@ -321,17 +329,17 @@ fn test_build_index_with_multiple_directories<H: ResourceId>() {
.expect("Failed to get indexed resource");

let index = ResourceIndex::build(root_path).expect("Failed to build index");
assert_eq!(index.len(), 2);
assert_eq!(index.len(), 2, "{:?}", index);

let resource = index
.get_resource_by_path("dir1/file1.txt")
.expect("Resource not found");
assert_eq!(resource, &expected_resource1);
assert_eq!(resource, &expected_resource1, "{:?}", resource);

let resource = index
.get_resource_by_path("dir2/file2.txt")
.expect("Resource not found");
assert_eq!(resource, &expected_resource2);
assert_eq!(resource, &expected_resource2, "{:?}", resource);
}

/// Test updating the resource index.
Expand Down Expand Up @@ -360,7 +368,7 @@ fn test_resource_index_update<H: ResourceId>() {
let mut index: ResourceIndex<H> =
ResourceIndex::build(root_path).expect("Failed to build index");
index.store().expect("Failed to store index");
assert_eq!(index.len(), 2);
assert_eq!(index.len(), 2, "{:?}", index);

// create new file
let new_file_path = root_path.join("new_file.txt");
Expand All @@ -378,21 +386,25 @@ fn test_resource_index_update<H: ResourceId>() {
.update_all()
.expect("Failed to update index");
// Index now contains 2 resources (file.txt and new_file.txt)
assert_eq!(index.len(), 2);
assert_eq!(index.len(), 2, "{:?}", index);

let resource = index
.get_resource_by_path("file.txt")
.expect("Resource not found");
let expected_resource =
get_indexed_resource_from_file(&file_path, &root_path.to_path_buf())
.expect("Failed to get indexed resource");
assert_eq!(resource, &expected_resource);
assert_eq!(resource, &expected_resource, "{:?}", resource);

let _resource = index
.get_resource_by_path("new_file.txt")
.expect("Resource not found");

assert!(index.get_resource_by_path("image.png").is_none());
assert!(
index.get_resource_by_path("image.png").is_none(),
"{:?}",
index
);
}

/// Test adding colliding files to the index.
Expand All @@ -417,7 +429,7 @@ fn test_add_colliding_files<H: ResourceId>() {
let mut index: ResourceIndex<H> =
ResourceIndex::build(root_path).expect("Failed to build index");
index.store().expect("Failed to store index");
assert_eq!(index.len(), 1);
assert_eq!(index.len(), 1, "{:?}", index);

let new_file_path = root_path.join("new_file.txt");
fs::write(&new_file_path, "file content").expect("Failed to write to file");
Expand All @@ -426,8 +438,8 @@ fn test_add_colliding_files<H: ResourceId>() {
.update_all()
.expect("Failed to update index");

assert_eq!(index.len(), 2);
assert_eq!(index.collisions().len(), 1);
assert_eq!(index.len(), 2, "{:?}", index);
assert_eq!(index.collisions().len(), 1, "{:?}", index);
}

/// Test `ResourceIndex::num_collisions()` method.
Expand All @@ -451,7 +463,7 @@ fn test_num_collisions<H: ResourceId>() {
let mut index: ResourceIndex<H> =
ResourceIndex::build(root_path).expect("Failed to build index");
index.store().expect("Failed to store index");
assert_eq!(index.len(), 1);
assert_eq!(index.len(), 1, "{:?}", index);

let new_file_path = root_path.join("new_file.txt");
fs::write(&new_file_path, "file content").expect("Failed to write to file");
Expand All @@ -464,8 +476,8 @@ fn test_num_collisions<H: ResourceId>() {
.update_all()
.expect("Failed to update index");

assert_eq!(index.len(), 3);
assert_eq!(index.num_collisions(), 3);
assert_eq!(index.len(), 3, "{:?}", index);
assert_eq!(index.num_collisions(), 3, "{:?}", index);
}

/// Test that we don't index hidden files.
Expand All @@ -486,5 +498,5 @@ fn test_hidden_files<H: ResourceId>() {
let index: ResourceIndex<H> =
ResourceIndex::build(root_path).expect("Failed to build index");
index.store().expect("Failed to store index");
assert_eq!(index.len(), 0);
assert_eq!(index.len(), 0, "{:?}", index);
}

0 comments on commit d22b9e2

Please sign in to comment.