1
Fork 0

Rollup merge of #113613 - GuillaumeGomez:allow-dash-in-file-name, r=notriddle

Allow to have `-` in rustdoc-json test file name

I extracted this commit from https://github.com/rust-lang/rust/pull/113574.

When I added the test, it kept saying that the JSON file couldn't be found. After investigating for a while, I discovered that we were expecting files to always use `_`, which is quite bad. So I added support for `-` in file names.

r? ``@notriddle``
This commit is contained in:
Matthias Krüger 2023-07-13 12:19:24 +02:00 committed by GitHub
commit 253a4ca43f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View file

@ -15,8 +15,10 @@ impl Cache {
/// Create a new cache, used to read files only once and otherwise store their contents. /// Create a new cache, used to read files only once and otherwise store their contents.
pub fn new(config: &Config) -> Cache { pub fn new(config: &Config) -> Cache {
let root = Path::new(&config.doc_dir); let root = Path::new(&config.doc_dir);
let filename = Path::new(&config.template).file_stem().unwrap(); // `filename` needs to replace `-` with `_` to be sure the JSON path will always be valid.
let file_path = root.join(&Path::with_extension(Path::new(filename), "json")); let filename =
Path::new(&config.template).file_stem().unwrap().to_str().unwrap().replace('-', "_");
let file_path = root.join(&Path::with_extension(Path::new(&filename), "json"));
let content = fs::read_to_string(&file_path).expect("failed to read JSON file"); let content = fs::read_to_string(&file_path).expect("failed to read JSON file");
Cache { Cache {

View file

@ -1,4 +1,5 @@
use std::io::{BufWriter, Write}; use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use clap::Parser; use clap::Parser;
@ -25,7 +26,7 @@ enum ErrorKind {
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
struct JsonOutput { struct JsonOutput {
path: String, path: PathBuf,
errors: Vec<Error>, errors: Vec<Error>,
} }
@ -45,6 +46,12 @@ struct Cli {
fn main() -> Result<()> { fn main() -> Result<()> {
let Cli { path, verbose, json_output } = Cli::parse(); let Cli { path, verbose, json_output } = Cli::parse();
// We convert `-` into `_` for the file name to be sure the JSON path will always be correct.
let path = Path::new(&path);
let filename = path.file_name().unwrap().to_str().unwrap().replace('-', "_");
let parent = path.parent().unwrap();
let path = parent.join(&filename);
let contents = fs::read_to_string(&path)?; let contents = fs::read_to_string(&path)?;
let krate: Crate = serde_json::from_str(&contents)?; let krate: Crate = serde_json::from_str(&contents)?;
assert_eq!(krate.format_version, FORMAT_VERSION); assert_eq!(krate.format_version, FORMAT_VERSION);
@ -101,7 +108,7 @@ fn main() -> Result<()> {
ErrorKind::Custom(msg) => eprintln!("{}: {}", err.id.0, msg), ErrorKind::Custom(msg) => eprintln!("{}: {}", err.id.0, msg),
} }
} }
bail!("Errors validating json {path}"); bail!("Errors validating json {}", path.display());
} }
Ok(()) Ok(())