Skip to content

Commit c3d48c3

Browse files
committed
Move Markdown-specific doctest code into submodule
1 parent 68193a3 commit c3d48c3

File tree

4 files changed

+51
-47
lines changed

4 files changed

+51
-47
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
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+
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
44+
45+
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
46+
Ok(())
47+
}

src/librustdoc/lib.rs

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

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

src/librustdoc/markdown.rs

Lines changed: 2 additions & 46 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) {
@@ -137,41 +131,3 @@ pub(crate) fn render<P: AsRef<Path>>(
137131
Ok(_) => Ok(()),
138132
}
139133
}
140-
141-
/// Runs any tests/code examples in the markdown file `input`.
142-
pub(crate) fn test(options: Options) -> Result<(), String> {
143-
use rustc_session::config::Input;
144-
let input_str = match &options.input {
145-
Input::File(path) => {
146-
read_to_string(&path).map_err(|err| format!("{}: {err}", path.display()))?
147-
}
148-
Input::Str { name: _, input } => input.clone(),
149-
};
150-
151-
let mut opts = GlobalTestOptions::default();
152-
opts.no_crate_inject = true;
153-
154-
let temp_dir =
155-
tempdir().map_err(|error| format!("failed to create temporary directory: {error:?}"))?;
156-
let file_path = temp_dir.path().join("rustdoc-cfgs");
157-
generate_args_file(&file_path, &options)?;
158-
159-
let mut collector = Collector::new(
160-
options.input.filestem().to_string(),
161-
options.clone(),
162-
true,
163-
opts,
164-
None,
165-
options.input.opt_path().map(ToOwned::to_owned),
166-
options.enable_per_target_ignores,
167-
file_path,
168-
);
169-
collector.set_position(DUMMY_SP);
170-
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
171-
172-
// For markdown files, custom code classes will be disabled until the feature is enabled by default.
173-
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
174-
175-
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
176-
Ok(())
177-
}

0 commit comments

Comments
 (0)