tests: call stats() on specific implementation

This removes the remaining calls to `Index::stats()`.
This commit is contained in:
Martin von Zweigbergk 2023-03-12 10:37:20 -07:00 committed by Martin von Zweigbergk
parent 59b40d8380
commit 5423feb8e1
3 changed files with 33 additions and 10 deletions

View file

@ -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<dyn Index> = if on_disk {
let index_segment: Box<dyn IndexSegment> = 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<dyn Index> = if on_disk {
let index_segment: Box<dyn IndexSegment> = 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<dyn Index> = if on_disk {
let index_segment: Box<dyn IndexSegment> = 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<dyn Index> = if on_disk {
let index_segment: Box<dyn IndexSegment> = 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();

View file

@ -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;

View file

@ -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<ReadonlyRepo>) -> &ReadonlyIndexImpl {
repo.readonly_index()
.as_index()
.as_any()
.downcast_ref::<ReadonlyIndexImpl>()
.unwrap()
}
fn commits_by_level(repo: &Arc<ReadonlyRepo>) -> Vec<u32> {
repo.index()
as_index_impl(repo)
.stats()
.levels
.iter()