From 5423feb8e1a498881a5d431df1a22bb5e48aba1a Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sun, 12 Mar 2023 10:37:20 -0700 Subject: [PATCH] tests: call stats() on specific implementation This removes the remaining calls to `Index::stats()`. --- lib/src/default_index_store.rs | 20 ++++++++++++++++---- lib/src/index.rs | 2 ++ lib/tests/test_index.rs | 21 +++++++++++++++------ 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/src/default_index_store.rs b/lib/src/default_index_store.rs index af00dfe50..794a9cc1c 100644 --- a/lib/src/default_index_store.rs +++ b/lib/src/default_index_store.rs @@ -678,6 +678,10 @@ impl MutableIndexImpl { } impl Index for MutableIndexImpl { + fn as_any(&self) -> &dyn Any { + self + } + fn num_commits(&self) -> u32 { CompositeIndex(self).num_commits() } @@ -1747,6 +1751,10 @@ impl ReadonlyIndexImpl { } impl Index for ReadonlyIndexImpl { + fn as_any(&self) -> &dyn Any { + self + } + fn num_commits(&self) -> u32 { CompositeIndex(self).num_commits() } @@ -1819,12 +1827,13 @@ mod tests { fn index_empty(on_disk: bool) { let temp_dir = testutils::new_temp_dir(); let index = MutableIndexImpl::full(3, 16); - let index: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { Box::new(index) }; + let index = CompositeIndex(index_segment.as_ref()); // Stats are as expected let stats = index.stats(); @@ -1849,12 +1858,13 @@ mod tests { let id_0 = CommitId::from_hex("000000"); let change_id0 = new_change_id(); index.add_commit_data(id_0.clone(), change_id0.clone(), &[]); - let index: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { Box::new(index) }; + let index = CompositeIndex(index_segment.as_ref()); // Stats are as expected let stats = index.stats(); @@ -1930,12 +1940,13 @@ mod tests { index.add_commit_data(id_3.clone(), change_id3.clone(), &[id_2.clone()]); index.add_commit_data(id_4.clone(), change_id4, &[id_1.clone()]); index.add_commit_data(id_5.clone(), change_id5, &[id_4.clone(), id_2.clone()]); - let index: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { Box::new(index) }; + let index = CompositeIndex(index_segment.as_ref()); // Stats are as expected let stats = index.stats(); @@ -2020,12 +2031,13 @@ mod tests { new_change_id(), &[id_1, id_2, id_3, id_4, id_5], ); - let index: Box = if on_disk { + let index_segment: Box = if on_disk { let saved_index = index.save_in(temp_dir.path().to_owned()).unwrap(); Box::new(Arc::try_unwrap(saved_index).unwrap()) } else { Box::new(index) }; + let index = CompositeIndex(index_segment.as_ref()); // Stats are as expected let stats = index.stats(); diff --git a/lib/src/index.rs b/lib/src/index.rs index 79b45e6ad..0884edcb6 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -46,6 +46,8 @@ pub trait IndexStore: Send + Sync + Debug { } pub trait Index { + fn as_any(&self) -> &dyn Any; + fn num_commits(&self) -> u32; fn stats(&self) -> IndexStats; diff --git a/lib/tests/test_index.rs b/lib/tests/test_index.rs index 5a18b08b1..f9dc949a2 100644 --- a/lib/tests/test_index.rs +++ b/lib/tests/test_index.rs @@ -17,6 +17,7 @@ use std::sync::Arc; use jujutsu_lib::backend::CommitId; use jujutsu_lib::commit::Commit; use jujutsu_lib::commit_builder::CommitBuilder; +use jujutsu_lib::default_index_store::ReadonlyIndexImpl; use jujutsu_lib::index::Index; use jujutsu_lib::repo::{MutableRepo, ReadonlyRepo, Repo}; use jujutsu_lib::settings::UserSettings; @@ -86,7 +87,7 @@ fn test_index_commits_standard_cases(use_git: bool) { let commit_h = graph_builder.commit_with_parents(&[&commit_e]); let repo = tx.commit(); - let index = repo.index(); + let index = as_index_impl(&repo); // There should be the root commit, plus 8 more assert_eq!(index.num_commits(), 1 + 8); @@ -146,7 +147,7 @@ fn test_index_commits_criss_cross(use_git: bool) { } let repo = tx.commit(); - let index = repo.index(); + let index = as_index_impl(&repo); // There should the root commit, plus 2 for each generation assert_eq!(index.num_commits(), 1 + 2 * (num_generations as u32)); @@ -292,7 +293,7 @@ fn test_index_commits_previous_operations(use_git: bool) { std::fs::create_dir(&index_operations_dir).unwrap(); let repo = load_repo_at_head(&settings, repo.repo_path()); - let index = repo.index(); + let index = as_index_impl(&repo); // There should be the root commit, plus 3 more assert_eq!(index.num_commits(), 1 + 3); @@ -343,7 +344,7 @@ fn test_index_commits_incremental(use_git: bool) { tx.commit(); let repo = load_repo_at_head(&settings, repo.repo_path()); - let index = repo.index(); + let index = as_index_impl(&repo); // There should be the root commit, plus 3 more assert_eq!(index.num_commits(), 1 + 3); @@ -388,7 +389,7 @@ fn test_index_commits_incremental_empty_transaction(use_git: bool) { repo.start_transaction(&settings, "test").commit(); let repo = load_repo_at_head(&settings, repo.repo_path()); - let index = repo.index(); + let index = as_index_impl(&repo); // There should be the root commit, plus 1 more assert_eq!(index.num_commits(), 1 + 1); @@ -445,8 +446,16 @@ fn create_n_commits( tx.commit() } +fn as_index_impl(repo: &Arc) -> &ReadonlyIndexImpl { + repo.readonly_index() + .as_index() + .as_any() + .downcast_ref::() + .unwrap() +} + fn commits_by_level(repo: &Arc) -> Vec { - repo.index() + as_index_impl(repo) .stats() .levels .iter()