Skip to content

Commit da64ca9

Browse files
committed
Auto merge of #48410 - QuietMisdreavus:beta-48327, r=alexcrichton
[beta] properly run doctests in standalone markdown files with pulldown This is a beta-specific fix for #48327, since a different fix landed in nightly (#48274) that is infeasible to backport. The nature of the issue was that when running doctests on standalone Markdown files, rustdoc names the tests based on the headings in the files. Therefore, with the following `a.md`: ``````markdown # My Cool Library This is my cool library! ## Examples Here's some cool code samples! ```rust assert_eq!(2+2, 4); ``` `````` Running this file with `rustdoc --test a.md` would show a test named `a.md - my_cool_library::examples (line 9)`. So far, this works just fine between Hoedown and Pulldown. But it gets murkier when you introduce markup into your headings. Consider the following `b.md`: ``````markdown # My Cool Library This is my cool library! ## `libcool` ```rust assert_eq!(2+2, 4); ``` `````` The code surrounding the different renderers handles this differently. Pulldown handles just the first `Text` event after seeing the header, so it names the test `b.md - my_cool_library::libcool (line 9)`. Hoedown, on the other hand, takes all the test within the heading, which Hoedown renders before handing to library code. Therefore, it will name the test `b.md - my_cool_library::_code_libcool__code_ (line 9)`. (Somewhere between rustdoc and libtest, the `</>` characters are replaced with underscores.) This causes a problem with another piece of code: The one that checks for whether Pulldown detected a code block that Hoedown didn't. The test collector groups the "old tests" listing by the full test name, but it *inserts* with the Hoedown name, and *searches* for the Pulldown name! This creates a situation where when `b.md` from above is run, it can't find a matching test from the ones Hoedown extracted, so it discards it and emits a warning. On nightly, this has been fixed by... ditching Hoedown entirely. This also removed the code that tracked the different test listings, and made it run the test anyway. Since backporting the Hoedown removal is infeasible (i'm personally relying on the change to ride the trains to give the stabilization enough time to complete), this instead chooses to group the test by the filename, instead of the full test name as before. This means that the test extractor finds the test properly, and properly runs the test.
2 parents ed61aaa + 798feb2 commit da64ca9

File tree

1 file changed

+3
-10
lines changed

1 file changed

+3
-10
lines changed

src/librustdoc/test.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ fn partition_source(s: &str) -> (String, String) {
434434
pub struct Collector {
435435
pub tests: Vec<testing::TestDescAndFn>,
436436
// to be removed when hoedown will be definitely gone
437-
pub old_tests: HashMap<String, Vec<String>>,
437+
pub old_tests: HashMap<FileName, Vec<String>>,
438438

439439
// The name of the test displayed to the user, separated by `::`.
440440
//
@@ -501,14 +501,8 @@ impl Collector {
501501
format!("{} - {} (line {})", filename, self.names.join("::"), line)
502502
}
503503

504-
// to be removed once hoedown is gone
505-
fn generate_name_beginning(&self, filename: &FileName) -> String {
506-
format!("{} - {} (line", filename, self.names.join("::"))
507-
}
508-
509504
pub fn add_old_test(&mut self, test: String, filename: FileName) {
510-
let name_beg = self.generate_name_beginning(&filename);
511-
let entry = self.old_tests.entry(name_beg)
505+
let entry = self.old_tests.entry(filename.clone())
512506
.or_insert(Vec::new());
513507
entry.push(test.trim().to_owned());
514508
}
@@ -520,10 +514,9 @@ impl Collector {
520514
let name = self.generate_name(line, &filename);
521515
// to be removed when hoedown is removed
522516
if self.render_type == RenderType::Pulldown {
523-
let name_beg = self.generate_name_beginning(&filename);
524517
let mut found = false;
525518
let test = test.trim().to_owned();
526-
if let Some(entry) = self.old_tests.get_mut(&name_beg) {
519+
if let Some(entry) = self.old_tests.get_mut(&filename) {
527520
found = entry.remove_item(&test).is_some();
528521
}
529522
if !found {

0 commit comments

Comments
 (0)