Skip to content

Commit 112ed0b

Browse files
committed
Add test for json output
This also fixes a few bugs in jsondocck and makes some of its errors easier to read.
1 parent edb73eb commit 112ed0b

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

src/rustdoc-json-types/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ pub struct Item {
6969
/// so this field is needed to differentiate.
7070
pub visibility: Visibility,
7171
/// The full markdown docstring of this item.
72+
///
73+
/// This field has already been pre-parsed, including combining lines and de-indenting
74+
/// whitespace. Each line is delimited by a newline, but the docs will not end with a trailing
75+
/// newline (unless added explicitly by the user with `#[doc = "\n"]`).
7276
pub docs: String,
7377
/// This mapping resolves [intra-doc links](https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md) from the docstring to their IDs
7478
pub links: HashMap<String, Id>,

src/test/rustdoc-json/empty-lines.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @has empty_lines.json "$.index[*][?(@.name == 'foo')]"
2+
// @has - "$.index[*][?(@.name == 'foo')].docs" \"\\n\\n\"
3+
///
4+
///
5+
///
6+
// ^ note that the above line does *not* include a trailing new line in the docs
7+
pub fn foo() {}
8+
9+
// @has empty_lines.json "$.index[*][?(@.name == 'bar')].docs" "\"first line\\nsecond line \""
10+
#[doc = "\n first line"]
11+
#[doc = "\n second line \n"]
12+
pub fn bar() {}

src/tools/compiletest/src/runtest.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2401,8 +2401,6 @@ impl<'test> TestCx<'test> {
24012401
}
24022402

24032403
let root = self.config.find_rust_src_root().unwrap();
2404-
let mut json_out = out_dir.join(self.testpaths.file.file_stem().unwrap());
2405-
json_out.set_extension("json");
24062404
let res = self.cmd2procres(
24072405
Command::new(self.config.jsondocck_path.as_ref().unwrap())
24082406
.arg("--doc-dir")
@@ -2415,7 +2413,9 @@ impl<'test> TestCx<'test> {
24152413
self.fatal_proc_rec("jsondocck failed!", &res)
24162414
}
24172415

2418-
let mut json_out = out_dir.join(self.testpaths.file.file_stem().unwrap());
2416+
let crate_name =
2417+
self.testpaths.file.file_stem().unwrap().to_str().unwrap().replace('-', "_");
2418+
let mut json_out = out_dir.join(crate_name);
24192419
json_out.set_extension("json");
24202420
let res = self.cmd2procres(
24212421
Command::new(&self.config.docck_python)

src/tools/jsondocck/src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ impl CommandKind {
6262
};
6363

6464
if !count {
65-
print_err(&format!("Incorrect number of arguments to `@{}`", self), lineno);
65+
print_err(
66+
&format!("Incorrect number of arguments to `@{}` (got {})", self, args.len()),
67+
lineno,
68+
);
6669
return false;
6770
}
6871

@@ -317,6 +320,8 @@ fn string_to_value<'a>(s: &str, cache: &'a Cache) -> Cow<'a, Value> {
317320
panic!("No variable: `{}`. Current state: `{:?}`", &s[1..], cache.variables)
318321
}))
319322
} else {
320-
Cow::Owned(serde_json::from_str(s).unwrap())
323+
Cow::Owned(serde_json::from_str(s).unwrap_or_else(|err| {
324+
panic!("failed to parse {:?} as a string: {}", s, err);
325+
}))
321326
}
322327
}

0 commit comments

Comments
 (0)