Skip to content

Commit ec6416b

Browse files
committed
Move Markdown-specific doctest code into submodule
1 parent 7e75e7c commit ec6416b

File tree

4 files changed

+59
-54
lines changed

4 files changed

+59
-54
lines changed

src/librustdoc/doctest.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod markdown;
22
mod rust;
33

4+
pub(crate) use markdown::test as test_markdown;
5+
46
use rustc_ast as ast;
57
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
68
use rustc_data_structures::sync::Lrc;

src/librustdoc/doctest/markdown.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
11
//! Doctest functionality used only for doctests in `.md` Markdown files.
2+
3+
use std::fs::read_to_string;
4+
5+
use rustc_span::DUMMY_SP;
6+
use tempfile::tempdir;
7+
8+
use super::{generate_args_file, Collector, GlobalTestOptions};
9+
use crate::config::Options;
10+
use crate::html::markdown::{find_testable_code, ErrorCodes};
11+
12+
/// Runs any tests/code examples in the markdown file `input`.
13+
pub(crate) fn test(options: Options) -> Result<(), String> {
14+
use rustc_session::config::Input;
15+
let input_str = match &options.input {
16+
Input::File(path) => {
17+
read_to_string(&path).map_err(|err| format!("{}: {err}", path.display()))?
18+
}
19+
Input::Str { name: _, input } => input.clone(),
20+
};
21+
22+
let mut opts = GlobalTestOptions::default();
23+
opts.no_crate_inject = true;
24+
25+
let temp_dir =
26+
tempdir().map_err(|error| format!("failed to create temporary directory: {error:?}"))?;
27+
let file_path = temp_dir.path().join("rustdoc-cfgs");
28+
generate_args_file(&file_path, &options)?;
29+
30+
let mut collector = Collector::new(
31+
options.input.filestem().to_string(),
32+
options.clone(),
33+
true,
34+
opts,
35+
None,
36+
options.input.opt_path().map(ToOwned::to_owned),
37+
options.enable_per_target_ignores,
38+
file_path,
39+
);
40+
collector.set_position(DUMMY_SP);
41+
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
42+
43+
// For markdown files, custom code classes will be disabled until the feature is enabled by default.
44+
find_testable_code(
45+
&input_str,
46+
&mut collector,
47+
codes,
48+
options.enable_per_target_ignores,
49+
None,
50+
false,
51+
);
52+
53+
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
54+
Ok(())
55+
}

src/librustdoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ fn main_args(
729729
core::new_dcx(options.error_format, None, options.diagnostic_width, &options.unstable_opts);
730730

731731
match (options.should_test, options.markdown_input()) {
732-
(true, Some(_)) => return wrap_return(&diag, markdown::test(options)),
732+
(true, Some(_)) => return wrap_return(&diag, doctest::test_markdown(options)),
733733
(true, None) => return doctest::run(&diag, options),
734734
(false, Some(input)) => {
735735
let input = input.to_owned();

src/librustdoc/markdown.rs

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ use std::fs::{create_dir_all, read_to_string, File};
33
use std::io::prelude::*;
44
use std::path::Path;
55

6-
use tempfile::tempdir;
7-
86
use rustc_span::edition::Edition;
9-
use rustc_span::DUMMY_SP;
107

11-
use crate::config::{Options, RenderOptions};
12-
use crate::doctest::{generate_args_file, Collector, GlobalTestOptions};
8+
use crate::config::RenderOptions;
139
use crate::html::escape::Escape;
1410
use crate::html::markdown;
15-
use crate::html::markdown::{
16-
find_testable_code, ErrorCodes, HeadingOffset, IdMap, Markdown, MarkdownWithToc,
17-
};
11+
use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, MarkdownWithToc};
1812

1913
/// Separate any lines at the start of the file that begin with `# ` or `%`.
2014
fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
@@ -141,48 +135,3 @@ pub(crate) fn render<P: AsRef<Path>>(
141135
Ok(_) => Ok(()),
142136
}
143137
}
144-
145-
/// Runs any tests/code examples in the markdown file `input`.
146-
pub(crate) fn test(options: Options) -> Result<(), String> {
147-
use rustc_session::config::Input;
148-
let input_str = match &options.input {
149-
Input::File(path) => {
150-
read_to_string(&path).map_err(|err| format!("{}: {err}", path.display()))?
151-
}
152-
Input::Str { name: _, input } => input.clone(),
153-
};
154-
155-
let mut opts = GlobalTestOptions::default();
156-
opts.no_crate_inject = true;
157-
158-
let temp_dir =
159-
tempdir().map_err(|error| format!("failed to create temporary directory: {error:?}"))?;
160-
let file_path = temp_dir.path().join("rustdoc-cfgs");
161-
generate_args_file(&file_path, &options)?;
162-
163-
let mut collector = Collector::new(
164-
options.input.filestem().to_string(),
165-
options.clone(),
166-
true,
167-
opts,
168-
None,
169-
options.input.opt_path().map(ToOwned::to_owned),
170-
options.enable_per_target_ignores,
171-
file_path,
172-
);
173-
collector.set_position(DUMMY_SP);
174-
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
175-
176-
// For markdown files, custom code classes will be disabled until the feature is enabled by default.
177-
find_testable_code(
178-
&input_str,
179-
&mut collector,
180-
codes,
181-
options.enable_per_target_ignores,
182-
None,
183-
false,
184-
);
185-
186-
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
187-
Ok(())
188-
}

0 commit comments

Comments
 (0)