From 26b84eec4f446b39e9dcfa64509c544fff490d6b Mon Sep 17 00:00:00 2001 From: Predrag Gruevski Date: Sun, 1 Dec 2024 21:44:31 +0000 Subject: [PATCH] Share built indexes across all test runs to speed up tests. Saves about 10s per `cargo test` run. --- src/query.rs | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/query.rs b/src/query.rs index e91c4222..fa1df2af 100644 --- a/src/query.rs +++ b/src/query.rs @@ -351,12 +351,23 @@ mod tests { static TEST_CRATE_RUSTDOCS: OnceLock> = OnceLock::new(); + /// Mapping test crate (pair) name -> (old index, new index). + static TEST_CRATE_INDEXES: OnceLock, VersionedIndex<'static>)>> = OnceLock::new(); + fn get_test_crate_names() -> &'static [String] { TEST_CRATE_NAMES.get_or_init(initialize_test_crate_names) } - fn get_test_crate_rustdocs(test_crate: &str) -> &'static (VersionedStorage, VersionedStorage) { - &TEST_CRATE_RUSTDOCS.get_or_init(initialize_test_crate_rustdocs)[test_crate] + fn get_all_test_crates() -> &'static BTreeMap { + TEST_CRATE_RUSTDOCS.get_or_init(initialize_test_crate_rustdocs) + } + + fn get_all_test_crate_indexes() -> &'static BTreeMap, VersionedIndex<'static>)> { + TEST_CRATE_INDEXES.get_or_init(initialize_test_crate_indexes) + } + + fn get_test_crate_indexes(test_crate: &str) -> &'static (VersionedIndex<'static>, VersionedIndex<'static>) { + &get_all_test_crate_indexes()[test_crate] } fn initialize_test_crate_names() -> Vec { @@ -412,6 +423,17 @@ mod tests { .collect() } + fn initialize_test_crate_indexes() -> BTreeMap, VersionedIndex<'static>)> { + get_all_test_crates() + .iter() + .map(|(key, (old_crate, new_crate))| { + let old_index = VersionedIndex::from_storage(old_crate); + let new_index = VersionedIndex::from_storage(new_crate); + (key.clone(), (old_index, new_index)) + }) + .collect() + } + fn load_pregenerated_rustdoc(crate_pair: &str, crate_version: &str) -> VersionedStorage { let rustdoc_path = format!("./localdata/test_data/{crate_pair}/{crate_version}/rustdoc.json"); @@ -427,10 +449,9 @@ mod tests { #[test] fn all_queries_are_valid() { - let (_baseline_crate, current_crate) = get_test_crate_rustdocs("template"); - let indexed_crate = VersionedIndex::from_storage(current_crate); + let (_baseline, current) = get_test_crate_indexes("template"); - let adapter = VersionedRustdocAdapter::new(&indexed_crate, Some(&indexed_crate)) + let adapter = VersionedRustdocAdapter::new(current, Some(current)) .expect("failed to create adapter"); for semver_query in SemverQuery::all_queries().into_values() { let _ = adapter @@ -441,8 +462,7 @@ mod tests { #[test] fn pub_use_handling() { - let (_baseline_crate, current_crate) = get_test_crate_rustdocs("pub_use_handling"); - let current = VersionedIndex::from_storage(current_crate); + let (_baseline, current) = get_test_crate_indexes("pub_use_handling"); let query = r#" { @@ -466,7 +486,7 @@ mod tests { arguments.insert("struct", "CheckPubUseHandling"); let adapter = - VersionedRustdocAdapter::new(¤t, None).expect("could not create adapter"); + VersionedRustdocAdapter::new(current, None).expect("could not create adapter"); let results_iter = adapter .run_query(query, arguments) @@ -599,21 +619,19 @@ mod tests { let mut query_execution_results: TestOutput = get_test_crate_names() .iter() .map(|crate_pair_name| { - let (crate_old, crate_new) = get_test_crate_rustdocs(crate_pair_name); - let indexed_crate_old = VersionedIndex::from_storage(crate_old); - let indexed_crate_new = VersionedIndex::from_storage(crate_new); + let (baseline, current) = get_test_crate_indexes(crate_pair_name); assert_no_false_positives_in_nonchanged_crate( query_name, &semver_query, - &indexed_crate_new, + current, crate_pair_name, "new", ); assert_no_false_positives_in_nonchanged_crate( query_name, &semver_query, - &indexed_crate_old, + baseline, crate_pair_name, "old", ); @@ -621,8 +639,8 @@ mod tests { run_query_on_crate_pair( &semver_query, crate_pair_name, - &indexed_crate_new, - &indexed_crate_old, + current, + baseline, ) }) .filter(|(_crate_pair_name, output)| !output.is_empty())