1
Fork 0

Move Markdown-specific doctest code into submodule

This commit is contained in:
Noah Lev 2024-05-30 22:42:32 -07:00 committed by Guillaume Gomez
parent 516010bd0f
commit 16db1a1bd0
4 changed files with 51 additions and 47 deletions

View file

@ -1,6 +1,8 @@
mod markdown;
mod rust;
pub(crate) use markdown::test as test_markdown;
use rustc_ast as ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::sync::Lrc;

View file

@ -1 +1,47 @@
//! Doctest functionality used only for doctests in `.md` Markdown files.
use std::fs::read_to_string;
use rustc_span::DUMMY_SP;
use tempfile::tempdir;
use super::{generate_args_file, Collector, GlobalTestOptions};
use crate::config::Options;
use crate::html::markdown::{find_testable_code, ErrorCodes};
/// Runs any tests/code examples in the markdown file `input`.
pub(crate) fn test(options: Options) -> Result<(), String> {
use rustc_session::config::Input;
let input_str = match &options.input {
Input::File(path) => {
read_to_string(&path).map_err(|err| format!("{}: {err}", path.display()))?
}
Input::Str { name: _, input } => input.clone(),
};
let mut opts = GlobalTestOptions::default();
opts.no_crate_inject = true;
let temp_dir =
tempdir().map_err(|error| format!("failed to create temporary directory: {error:?}"))?;
let file_path = temp_dir.path().join("rustdoc-cfgs");
generate_args_file(&file_path, &options)?;
let mut collector = Collector::new(
options.input.filestem().to_string(),
options.clone(),
true,
opts,
None,
options.input.opt_path().map(ToOwned::to_owned),
options.enable_per_target_ignores,
file_path,
);
collector.set_position(DUMMY_SP);
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
Ok(())
}

View file

@ -728,7 +728,7 @@ fn main_args(
core::new_dcx(options.error_format, None, options.diagnostic_width, &options.unstable_opts);
match (options.should_test, options.markdown_input()) {
(true, Some(_)) => return wrap_return(&diag, markdown::test(options)),
(true, Some(_)) => return wrap_return(&diag, doctest::test_markdown(options)),
(true, None) => return doctest::run(&diag, options),
(false, Some(input)) => {
let input = input.to_owned();

View file

@ -3,18 +3,12 @@ use std::fs::{create_dir_all, read_to_string, File};
use std::io::prelude::*;
use std::path::Path;
use tempfile::tempdir;
use rustc_span::edition::Edition;
use rustc_span::DUMMY_SP;
use crate::config::{Options, RenderOptions};
use crate::doctest::{generate_args_file, Collector, GlobalTestOptions};
use crate::config::RenderOptions;
use crate::html::escape::Escape;
use crate::html::markdown;
use crate::html::markdown::{
find_testable_code, ErrorCodes, HeadingOffset, IdMap, Markdown, MarkdownWithToc,
};
use crate::html::markdown::{ErrorCodes, HeadingOffset, IdMap, Markdown, MarkdownWithToc};
/// Separate any lines at the start of the file that begin with `# ` or `%`.
fn extract_leading_metadata(s: &str) -> (Vec<&str>, &str) {
@ -137,41 +131,3 @@ pub(crate) fn render<P: AsRef<Path>>(
Ok(_) => Ok(()),
}
}
/// Runs any tests/code examples in the markdown file `input`.
pub(crate) fn test(options: Options) -> Result<(), String> {
use rustc_session::config::Input;
let input_str = match &options.input {
Input::File(path) => {
read_to_string(&path).map_err(|err| format!("{}: {err}", path.display()))?
}
Input::Str { name: _, input } => input.clone(),
};
let mut opts = GlobalTestOptions::default();
opts.no_crate_inject = true;
let temp_dir =
tempdir().map_err(|error| format!("failed to create temporary directory: {error:?}"))?;
let file_path = temp_dir.path().join("rustdoc-cfgs");
generate_args_file(&file_path, &options)?;
let mut collector = Collector::new(
options.input.filestem().to_string(),
options.clone(),
true,
opts,
None,
options.input.opt_path().map(ToOwned::to_owned),
options.enable_per_target_ignores,
file_path,
);
collector.set_position(DUMMY_SP);
let codes = ErrorCodes::from(options.unstable_features.is_nightly_build());
// For markdown files, custom code classes will be disabled until the feature is enabled by default.
find_testable_code(&input_str, &mut collector, codes, options.enable_per_target_ignores, None);
crate::doctest::run_tests(options.test_args, options.nocapture, collector.tests);
Ok(())
}