diff options
| author | Guillaume Gomez <guillaume.gomez@huawei.com> | 2023-07-11 16:31:33 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume.gomez@huawei.com> | 2023-07-12 10:45:49 +0200 |
| commit | 18457ea47db3c923b617159640211a23f88f6fcf (patch) | |
| tree | cdaf05bb47104fd5e49132ee4ce0c75d98d7a6c9 /src | |
| parent | b12ff66f2c9daf6538fe0548b3c2ba16d9185cfb (diff) | |
| download | rust-18457ea47db3c923b617159640211a23f88f6fcf.tar.gz rust-18457ea47db3c923b617159640211a23f88f6fcf.zip | |
Allow to have `-` in the rustdoc-json test file name
Diffstat (limited to 'src')
| -rw-r--r-- | src/tools/jsondocck/src/cache.rs | 6 | ||||
| -rw-r--r-- | src/tools/jsondoclint/src/main.rs | 11 |
2 files changed, 13 insertions, 4 deletions
diff --git a/src/tools/jsondocck/src/cache.rs b/src/tools/jsondocck/src/cache.rs index f9e54232750..50697d46b8c 100644 --- a/src/tools/jsondocck/src/cache.rs +++ b/src/tools/jsondocck/src/cache.rs @@ -15,8 +15,10 @@ impl Cache { /// Create a new cache, used to read files only once and otherwise store their contents. pub fn new(config: &Config) -> Cache { let root = Path::new(&config.doc_dir); - let filename = Path::new(&config.template).file_stem().unwrap(); - let file_path = root.join(&Path::with_extension(Path::new(filename), "json")); + // `filename` needs to replace `-` with `_` to be sure the JSON path will always be valid. + 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"); Cache { diff --git a/src/tools/jsondoclint/src/main.rs b/src/tools/jsondoclint/src/main.rs index ee163ddfdd9..aaaba78cb46 100644 --- a/src/tools/jsondoclint/src/main.rs +++ b/src/tools/jsondoclint/src/main.rs @@ -1,4 +1,5 @@ use std::io::{BufWriter, Write}; +use std::path::{Path, PathBuf}; use anyhow::{bail, Result}; use clap::Parser; @@ -25,7 +26,7 @@ enum ErrorKind { #[derive(Debug, Serialize)] struct JsonOutput { - path: String, + path: PathBuf, errors: Vec<Error>, } @@ -45,6 +46,12 @@ struct Cli { fn main() -> Result<()> { 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 krate: Crate = serde_json::from_str(&contents)?; assert_eq!(krate.format_version, FORMAT_VERSION); @@ -101,7 +108,7 @@ fn main() -> Result<()> { ErrorKind::Custom(msg) => eprintln!("{}: {}", err.id.0, msg), } } - bail!("Errors validating json {path}"); + bail!("Errors validating json {}", path.display()); } Ok(()) |
