Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move test utils to their own file. #132

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions src/indexed_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,58 +632,13 @@ fn create_manually_inlined_builtin_traits(crate_: &Crate) -> HashMap<Id, Item> {

#[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<u32> {
let version = serde_json::from_str::<RustdocFormatVersion>(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,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
42 changes: 42 additions & 0 deletions src/test_util.rs
Original file line number Diff line number Diff line change
@@ -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<u32> {
let version = serde_json::from_str::<RustdocFormatVersion>(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")
}