From 30479d6a504d6d5efdf687aab8d43c2a68e7e6a1 Mon Sep 17 00:00:00 2001 From: Predrag Gruevski <2348618+obi1kenobi@users.noreply.github.com> Date: Mon, 10 Apr 2023 18:45:59 -0400 Subject: [PATCH] Move test utils to their own file. (#132) (#135) --- src/indexed_crate.rs | 47 +------------------------------------------- src/lib.rs | 3 +++ src/test_util.rs | 42 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 46 deletions(-) create mode 100644 src/test_util.rs diff --git a/src/indexed_crate.rs b/src/indexed_crate.rs index 8d46d4a..ccf98fa 100644 --- a/src/indexed_crate.rs +++ b/src/indexed_crate.rs @@ -471,58 +471,13 @@ fn get_typedef_equivalent_reexport_target<'a>( #[cfg(test)] mod tests { - use std::fs::read_to_string; - - use anyhow::Context; - use rustdoc_types::Crate; - - #[derive(serde::Deserialize)] - struct RustdocFormatVersion { - format_version: u32, - } - - fn detect_rustdoc_format_version(file_data: &str) -> anyhow::Result { - let version = serde_json::from_str::(file_data) - .with_context(|| "file does not appear to be a rustdoc JSON format".to_string())?; - - Ok(version.format_version) - } - - fn load_pregenerated_rustdoc(crate_name: &str) -> Crate { - let path = format!("./localdata/test_data/{crate_name}/rustdoc.json"); - let content = read_to_string(&path) - .with_context(|| format!("Could not load {path} file, did you forget to run ./scripts/regenerate_test_rustdocs.sh ?")) - .expect("failed to load rustdoc"); - serde_json::from_str(&content) - .with_context(|| { - let format_version = detect_rustdoc_format_version(&content); - match format_version { - Ok(format_version) => { - format!( - "Failed to parse {path} file: it is rustdoc version {format_version} but expected {}. \ - Did you forget to run ./scripts/regenerate_test_rustdocs.sh ?", - rustdoc_types::FORMAT_VERSION, - ) - } - Err(..) => { - format!( - "Failed to parse {path} file: it didn't seem to be valid rustdoc JSON. \ - Did you forget to run ./scripts/regenerate_test_rustdocs.sh ?" - ) - } - } - }).expect("failed to parse rustdoc JSON") - } - mod reexports { use std::collections::{BTreeMap, BTreeSet}; use itertools::Itertools; use maplit::{btreemap, btreeset}; - use crate::IndexedCrate; - - use super::load_pregenerated_rustdoc; + use crate::{test_util::load_pregenerated_rustdoc, IndexedCrate}; fn assert_exported_items_match( test_crate: &str, diff --git a/src/lib.rs b/src/lib.rs index 12c856a..30277b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,9 @@ mod adapter; mod attributes; mod indexed_crate; +#[cfg(test)] +pub(crate) mod test_util; + // Re-export the Crate type so we can deserialize it. pub use rustdoc_types::Crate; diff --git a/src/test_util.rs b/src/test_util.rs new file mode 100644 index 0000000..6f7ff16 --- /dev/null +++ b/src/test_util.rs @@ -0,0 +1,42 @@ +use std::fs::read_to_string; + +use anyhow::Context; +use rustdoc_types::Crate; + +#[derive(serde::Deserialize)] +struct RustdocFormatVersion { + format_version: u32, +} + +pub(crate) fn detect_rustdoc_format_version(file_data: &str) -> anyhow::Result { + let version = serde_json::from_str::(file_data) + .with_context(|| "file does not appear to be a rustdoc JSON format".to_string())?; + + Ok(version.format_version) +} + +pub(crate) fn load_pregenerated_rustdoc(crate_name: &str) -> Crate { + let path = format!("./localdata/test_data/{crate_name}/rustdoc.json"); + let content = read_to_string(&path) + .with_context(|| format!("Could not load {path} file, did you forget to run ./scripts/regenerate_test_rustdocs.sh ?")) + .expect("failed to load rustdoc"); + serde_json::from_str(&content) + .with_context(|| { + let format_version = detect_rustdoc_format_version(&content); + match format_version { + Ok(format_version) => { + format!( + "Failed to parse {path} file: it is rustdoc version {format_version} but expected {}. \ + Did you forget to run ./scripts/regenerate_test_rustdocs.sh ?", + rustdoc_types::FORMAT_VERSION, + ) + } + Err(..) => { + format!( + "Failed to parse {path} file: it didn't seem to be valid rustdoc JSON. \ + Did you forget to run ./scripts/regenerate_test_rustdocs.sh ?" + ) + } + } + }).expect("failed to parse rustdoc JSON") +}