Skip to content

Commit b27b97a

Browse files
authored
Rollup merge of rust-lang#113613 - GuillaumeGomez:allow-dash-in-file-name, r=notriddle
Allow to have `-` in rustdoc-json test file name I extracted this commit from rust-lang#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`
2 parents e9faff8 + 18457ea commit b27b97a

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/tools/jsondocck/src/cache.rs

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

2224
Cache {

src/tools/jsondoclint/src/main.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::io::{BufWriter, Write};
2+
use std::path::{Path, PathBuf};
23

34
use anyhow::{bail, Result};
45
use clap::Parser;
@@ -25,7 +26,7 @@ enum ErrorKind {
2526

2627
#[derive(Debug, Serialize)]
2728
struct JsonOutput {
28-
path: String,
29+
path: PathBuf,
2930
errors: Vec<Error>,
3031
}
3132

@@ -45,6 +46,12 @@ struct Cli {
4546
fn main() -> Result<()> {
4647
let Cli { path, verbose, json_output } = Cli::parse();
4748

49+
// We convert `-` into `_` for the file name to be sure the JSON path will always be correct.
50+
let path = Path::new(&path);
51+
let filename = path.file_name().unwrap().to_str().unwrap().replace('-', "_");
52+
let parent = path.parent().unwrap();
53+
let path = parent.join(&filename);
54+
4855
let contents = fs::read_to_string(&path)?;
4956
let krate: Crate = serde_json::from_str(&contents)?;
5057
assert_eq!(krate.format_version, FORMAT_VERSION);
@@ -101,7 +108,7 @@ fn main() -> Result<()> {
101108
ErrorKind::Custom(msg) => eprintln!("{}: {}", err.id.0, msg),
102109
}
103110
}
104-
bail!("Errors validating json {path}");
111+
bail!("Errors validating json {}", path.display());
105112
}
106113

107114
Ok(())

0 commit comments

Comments
 (0)