Skip to content

Commit

Permalink
Merge branch 'rustdoc-v21' into cp_rustdoc-v21_e25d3dd
Browse files Browse the repository at this point in the history
  • Loading branch information
obi1kenobi authored Apr 10, 2023
2 parents ecefff0 + 30479d6 commit e568de9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
47 changes: 1 addition & 46 deletions src/indexed_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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")
}

0 comments on commit e568de9

Please sign in to comment.