Skip to content

Commit

Permalink
Share built indexes across all test runs to speed up tests.
Browse files Browse the repository at this point in the history
Saves about 10s per `cargo test` run.
  • Loading branch information
obi1kenobi committed Dec 1, 2024
1 parent e6bed4d commit 26b84ee
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,23 @@ mod tests {
static TEST_CRATE_RUSTDOCS: OnceLock<BTreeMap<String, (VersionedStorage, VersionedStorage)>> =
OnceLock::new();

Check warning on line 352 in src/query.rs

View workflow job for this annotation

GitHub Actions / Check lint and rustfmt

Diff in /home/runner/work/cargo-semver-checks/cargo-semver-checks/semver/src/query.rs

/// Mapping test crate (pair) name -> (old index, new index).
static TEST_CRATE_INDEXES: OnceLock<BTreeMap<String, (VersionedIndex<'static>, 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<String, (VersionedStorage, VersionedStorage)> {
TEST_CRATE_RUSTDOCS.get_or_init(initialize_test_crate_rustdocs)

Check warning on line 362 in src/query.rs

View workflow job for this annotation

GitHub Actions / Check lint and rustfmt

Diff in /home/runner/work/cargo-semver-checks/cargo-semver-checks/semver/src/query.rs
}

fn get_all_test_crate_indexes() -> &'static BTreeMap<String, (VersionedIndex<'static>, 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>) {

Check warning on line 369 in src/query.rs

View workflow job for this annotation

GitHub Actions / Check lint and rustfmt

Diff in /home/runner/work/cargo-semver-checks/cargo-semver-checks/semver/src/query.rs
&get_all_test_crate_indexes()[test_crate]
}

fn initialize_test_crate_names() -> Vec<String> {
Expand Down Expand Up @@ -412,6 +423,17 @@ mod tests {
.collect()

Check warning on line 423 in src/query.rs

View workflow job for this annotation

GitHub Actions / Check lint and rustfmt

Diff in /home/runner/work/cargo-semver-checks/cargo-semver-checks/semver/src/query.rs
}

fn initialize_test_crate_indexes() -> BTreeMap<String, (VersionedIndex<'static>, 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");
Expand All @@ -427,10 +449,9 @@ mod tests {

#[test]
fn all_queries_are_valid() {

Check warning on line 451 in src/query.rs

View workflow job for this annotation

GitHub Actions / Check lint and rustfmt

Diff in /home/runner/work/cargo-semver-checks/cargo-semver-checks/semver/src/query.rs
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
Expand All @@ -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#"
{
Expand All @@ -466,7 +486,7 @@ mod tests {
arguments.insert("struct", "CheckPubUseHandling");

let adapter =
VersionedRustdocAdapter::new(&current, None).expect("could not create adapter");
VersionedRustdocAdapter::new(current, None).expect("could not create adapter");

let results_iter = adapter
.run_query(query, arguments)
Expand Down Expand Up @@ -599,30 +619,28 @@ 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",

Check warning on line 636 in src/query.rs

View workflow job for this annotation

GitHub Actions / Check lint and rustfmt

Diff in /home/runner/work/cargo-semver-checks/cargo-semver-checks/semver/src/query.rs
);

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())
Expand Down

0 comments on commit 26b84ee

Please sign in to comment.