Unify naming of DocTest

This commit is contained in:
Guillaume Gomez 2024-07-09 12:07:18 +02:00
parent 4b1db071d1
commit ab3d90e037
6 changed files with 46 additions and 42 deletions

View File

@ -163,7 +163,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<()
let args_path = temp_dir.path().join("rustdoc-cfgs"); let args_path = temp_dir.path().join("rustdoc-cfgs");
crate::wrap_return(dcx, generate_args_file(&args_path, &options))?; crate::wrap_return(dcx, generate_args_file(&args_path, &options))?;
let CreateRunnableDoctests { let CreateRunnableDocTests {
standalone_tests, standalone_tests,
mergeable_tests, mergeable_tests,
rustdoc_options, rustdoc_options,
@ -179,7 +179,7 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, options: RustdocOptions) -> Result<()
let opts = scrape_test_config(crate_name, crate_attrs, args_path); let opts = scrape_test_config(crate_name, crate_attrs, args_path);
let enable_per_target_ignores = options.enable_per_target_ignores; let enable_per_target_ignores = options.enable_per_target_ignores;
let mut collector = CreateRunnableDoctests::new(options, opts); let mut collector = CreateRunnableDocTests::new(options, opts);
let hir_collector = HirCollector::new( let hir_collector = HirCollector::new(
&compiler.sess, &compiler.sess,
tcx.hir(), tcx.hir(),
@ -250,7 +250,7 @@ pub(crate) fn run_tests(
rustdoc_options: &Arc<RustdocOptions>, rustdoc_options: &Arc<RustdocOptions>,
unused_extern_reports: &Arc<Mutex<Vec<UnusedExterns>>>, unused_extern_reports: &Arc<Mutex<Vec<UnusedExterns>>>,
mut standalone_tests: Vec<test::TestDescAndFn>, mut standalone_tests: Vec<test::TestDescAndFn>,
mergeable_tests: FxHashMap<Edition, Vec<(DocTestBuilder, ScrapedDoctest)>>, mergeable_tests: FxHashMap<Edition, Vec<(DocTestBuilder, ScrapedDocTest)>>,
) { ) {
let mut test_args = Vec::with_capacity(rustdoc_options.test_args.len() + 1); let mut test_args = Vec::with_capacity(rustdoc_options.test_args.len() + 1);
test_args.insert(0, "rustdoctest".to_string()); test_args.insert(0, "rustdoctest".to_string());
@ -432,8 +432,13 @@ fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Com
command command
} }
/// This struct contains information needed for running a doctest. /// Information needed for running a bundle of doctests.
struct RunnableDoctest { ///
/// This data structure contains the "full" test code, including the wrappers
/// (if multiple doctests are merged), `main` function,
/// and everything needed to calculate the compiler's command-line arguments.
/// The `# ` prefix on boring lines has also been stripped.
struct RunnableDocTest {
full_test_code: String, full_test_code: String,
full_test_line_offset: usize, full_test_line_offset: usize,
test_opts: IndividualTestOptions, test_opts: IndividualTestOptions,
@ -444,14 +449,14 @@ struct RunnableDoctest {
no_run: bool, no_run: bool,
} }
impl RunnableDoctest { impl RunnableDocTest {
fn path_for_merged_doctest(&self) -> PathBuf { fn path_for_merged_doctest(&self) -> PathBuf {
self.test_opts.outdir.path().join(&format!("doctest_{}.rs", self.edition)) self.test_opts.outdir.path().join(&format!("doctest_{}.rs", self.edition))
} }
} }
fn run_test( fn run_test(
doctest: RunnableDoctest, doctest: RunnableDocTest,
rustdoc_options: &RustdocOptions, rustdoc_options: &RustdocOptions,
supports_color: bool, supports_color: bool,
is_multiple_tests: bool, is_multiple_tests: bool,
@ -700,7 +705,7 @@ fn new(options: &RustdocOptions, test_id: &Option<String>, test_path: PathBuf) -
} }
/// A doctest scraped from the code, ready to be turned into a runnable test. /// A doctest scraped from the code, ready to be turned into a runnable test.
pub(crate) struct ScrapedDoctest { pub(crate) struct ScrapedDocTest {
filename: FileName, filename: FileName,
line: usize, line: usize,
langstr: LangString, langstr: LangString,
@ -708,7 +713,7 @@ pub(crate) struct ScrapedDoctest {
name: String, name: String,
} }
impl ScrapedDoctest { impl ScrapedDocTest {
fn new( fn new(
filename: FileName, filename: FileName,
line: usize, line: usize,
@ -748,14 +753,14 @@ fn path(&self) -> PathBuf {
} }
} }
pub(crate) trait DoctestVisitor { pub(crate) trait DocTestVisitor {
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine); fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine);
fn visit_header(&mut self, _name: &str, _level: u32) {} fn visit_header(&mut self, _name: &str, _level: u32) {}
} }
struct CreateRunnableDoctests { struct CreateRunnableDocTests {
standalone_tests: Vec<test::TestDescAndFn>, standalone_tests: Vec<test::TestDescAndFn>,
mergeable_tests: FxHashMap<Edition, Vec<(DocTestBuilder, ScrapedDoctest)>>, mergeable_tests: FxHashMap<Edition, Vec<(DocTestBuilder, ScrapedDocTest)>>,
rustdoc_options: Arc<RustdocOptions>, rustdoc_options: Arc<RustdocOptions>,
opts: GlobalTestOptions, opts: GlobalTestOptions,
@ -765,10 +770,10 @@ struct CreateRunnableDoctests {
can_merge_doctests: bool, can_merge_doctests: bool,
} }
impl CreateRunnableDoctests { impl CreateRunnableDocTests {
fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnableDoctests { fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnableDocTests {
let can_merge_doctests = rustdoc_options.edition >= Edition::Edition2024; let can_merge_doctests = rustdoc_options.edition >= Edition::Edition2024;
CreateRunnableDoctests { CreateRunnableDocTests {
standalone_tests: Vec::new(), standalone_tests: Vec::new(),
mergeable_tests: FxHashMap::default(), mergeable_tests: FxHashMap::default(),
rustdoc_options: Arc::new(rustdoc_options), rustdoc_options: Arc::new(rustdoc_options),
@ -780,7 +785,7 @@ fn new(rustdoc_options: RustdocOptions, opts: GlobalTestOptions) -> CreateRunnab
} }
} }
fn add_test(&mut self, scraped_test: ScrapedDoctest) { fn add_test(&mut self, scraped_test: ScrapedDocTest) {
// For example `module/file.rs` would become `module_file_rs` // For example `module/file.rs` would become `module_file_rs`
let file = scraped_test let file = scraped_test
.filename .filename
@ -829,7 +834,7 @@ fn add_test(&mut self, scraped_test: ScrapedDoctest) {
fn generate_test_desc_and_fn( fn generate_test_desc_and_fn(
&mut self, &mut self,
test: DocTestBuilder, test: DocTestBuilder,
scraped_test: ScrapedDoctest, scraped_test: ScrapedDocTest,
) -> test::TestDescAndFn { ) -> test::TestDescAndFn {
if !scraped_test.langstr.compile_fail { if !scraped_test.langstr.compile_fail {
self.compiling_test_count.fetch_add(1, Ordering::SeqCst); self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
@ -847,7 +852,7 @@ fn generate_test_desc_and_fn(
fn generate_test_desc_and_fn( fn generate_test_desc_and_fn(
test: DocTestBuilder, test: DocTestBuilder,
scraped_test: ScrapedDoctest, scraped_test: ScrapedDocTest,
opts: GlobalTestOptions, opts: GlobalTestOptions,
rustdoc_options: Arc<RustdocOptions>, rustdoc_options: Arc<RustdocOptions>,
unused_externs: Arc<Mutex<Vec<UnusedExterns>>>, unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
@ -894,7 +899,7 @@ fn doctest_run_fn(
test_opts: IndividualTestOptions, test_opts: IndividualTestOptions,
global_opts: GlobalTestOptions, global_opts: GlobalTestOptions,
doctest: DocTestBuilder, doctest: DocTestBuilder,
scraped_test: ScrapedDoctest, scraped_test: ScrapedDocTest,
rustdoc_options: Arc<RustdocOptions>, rustdoc_options: Arc<RustdocOptions>,
unused_externs: Arc<Mutex<Vec<UnusedExterns>>>, unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
) -> Result<(), String> { ) -> Result<(), String> {
@ -907,7 +912,7 @@ fn doctest_run_fn(
&global_opts, &global_opts,
Some(&global_opts.crate_name), Some(&global_opts.crate_name),
); );
let runnable_test = RunnableDoctest { let runnable_test = RunnableDocTest {
full_test_code, full_test_code,
full_test_line_offset, full_test_line_offset,
test_opts, test_opts,
@ -980,7 +985,7 @@ fn doctest_run_fn(
} }
#[cfg(test)] // used in tests #[cfg(test)] // used in tests
impl DoctestVisitor for Vec<usize> { impl DocTestVisitor for Vec<usize> {
fn visit_test(&mut self, _test: String, _config: LangString, rel_line: MdRelLine) { fn visit_test(&mut self, _test: String, _config: LangString, rel_line: MdRelLine) {
self.push(1 + rel_line.offset()); self.push(1 + rel_line.offset());
} }

View File

@ -7,23 +7,23 @@
use tempfile::tempdir; use tempfile::tempdir;
use super::{ use super::{
generate_args_file, CreateRunnableDoctests, DoctestVisitor, GlobalTestOptions, ScrapedDoctest, generate_args_file, CreateRunnableDocTests, DocTestVisitor, GlobalTestOptions, ScrapedDocTest,
}; };
use crate::config::Options; use crate::config::Options;
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString, MdRelLine}; use crate::html::markdown::{find_testable_code, ErrorCodes, LangString, MdRelLine};
struct MdCollector { struct MdCollector {
tests: Vec<ScrapedDoctest>, tests: Vec<ScrapedDocTest>,
cur_path: Vec<String>, cur_path: Vec<String>,
filename: FileName, filename: FileName,
} }
impl DoctestVisitor for MdCollector { impl DocTestVisitor for MdCollector {
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) { fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) {
let filename = self.filename.clone(); let filename = self.filename.clone();
// First line of Markdown is line 1. // First line of Markdown is line 1.
let line = 1 + rel_line.offset(); let line = 1 + rel_line.offset();
self.tests.push(ScrapedDoctest::new(filename, line, self.cur_path.clone(), config, test)); self.tests.push(ScrapedDocTest::new(filename, line, self.cur_path.clone(), config, test));
} }
fn visit_header(&mut self, name: &str, level: u32) { fn visit_header(&mut self, name: &str, level: u32) {
@ -113,9 +113,9 @@ pub(crate) fn test(options: Options) -> Result<(), String> {
None, None,
); );
let mut collector = CreateRunnableDoctests::new(options.clone(), opts); let mut collector = CreateRunnableDocTests::new(options.clone(), opts);
md_collector.tests.into_iter().for_each(|t| collector.add_test(t)); md_collector.tests.into_iter().for_each(|t| collector.add_test(t));
let CreateRunnableDoctests { opts, rustdoc_options, standalone_tests, mergeable_tests, .. } = let CreateRunnableDocTests { opts, rustdoc_options, standalone_tests, mergeable_tests, .. } =
collector; collector;
crate::doctest::run_tests( crate::doctest::run_tests(
opts, opts,

View File

@ -4,8 +4,8 @@
use std::fmt::Write; use std::fmt::Write;
use crate::doctest::{ use crate::doctest::{
run_test, DocTestBuilder, GlobalTestOptions, IndividualTestOptions, RunnableDoctest, run_test, DocTestBuilder, GlobalTestOptions, IndividualTestOptions, RunnableDocTest,
RustdocOptions, ScrapedDoctest, TestFailure, UnusedExterns, RustdocOptions, ScrapedDocTest, TestFailure, UnusedExterns,
}; };
use crate::html::markdown::{Ignore, LangString}; use crate::html::markdown::{Ignore, LangString};
@ -32,7 +32,7 @@ pub(crate) fn new() -> Self {
pub(crate) fn add_test( pub(crate) fn add_test(
&mut self, &mut self,
doctest: &DocTestBuilder, doctest: &DocTestBuilder,
scraped_test: &ScrapedDoctest, scraped_test: &ScrapedDocTest,
target_str: &str, target_str: &str,
) { ) {
let ignore = match scraped_test.langstr.ignore { let ignore = match scraped_test.langstr.ignore {
@ -175,7 +175,7 @@ fn main() -> std::process::ExitCode {{
ids = self.ids, ids = self.ids,
) )
.expect("failed to generate test code"); .expect("failed to generate test code");
let runnable_test = RunnableDoctest { let runnable_test = RunnableDocTest {
full_test_code: code, full_test_code: code,
full_test_line_offset: 0, full_test_line_offset: 0,
test_opts: test_options, test_opts: test_options,
@ -199,7 +199,7 @@ fn main() -> std::process::ExitCode {{
/// Push new doctest content into `output`. Returns the test ID for this doctest. /// Push new doctest content into `output`. Returns the test ID for this doctest.
fn generate_mergeable_doctest( fn generate_mergeable_doctest(
doctest: &DocTestBuilder, doctest: &DocTestBuilder,
scraped_test: &ScrapedDoctest, scraped_test: &ScrapedDocTest,
ignore: bool, ignore: bool,
id: usize, id: usize,
output: &mut String, output: &mut String,

View File

@ -14,14 +14,13 @@
use rustc_span::source_map::SourceMap; use rustc_span::source_map::SourceMap;
use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP}; use rustc_span::{BytePos, FileName, Pos, Span, DUMMY_SP};
use super::{DoctestVisitor, ScrapedDoctest}; use super::{DocTestVisitor, ScrapedDocTest};
use crate::clean::types::AttributesExt; use crate::clean::{types::AttributesExt, Attributes};
use crate::clean::Attributes;
use crate::html::markdown::{self, ErrorCodes, LangString, MdRelLine}; use crate::html::markdown::{self, ErrorCodes, LangString, MdRelLine};
struct RustCollector { struct RustCollector {
source_map: Lrc<SourceMap>, source_map: Lrc<SourceMap>,
tests: Vec<ScrapedDoctest>, tests: Vec<ScrapedDocTest>,
cur_path: Vec<String>, cur_path: Vec<String>,
position: Span, position: Span,
} }
@ -48,10 +47,10 @@ fn get_base_line(&self) -> usize {
} }
} }
impl DoctestVisitor for RustCollector { impl DocTestVisitor for RustCollector {
fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) { fn visit_test(&mut self, test: String, config: LangString, rel_line: MdRelLine) {
let line = self.get_base_line() + rel_line.offset(); let line = self.get_base_line() + rel_line.offset();
self.tests.push(ScrapedDoctest::new( self.tests.push(ScrapedDocTest::new(
self.get_filename(), self.get_filename(),
line, line,
self.cur_path.clone(), self.cur_path.clone(),
@ -89,7 +88,7 @@ pub fn new(
Self { sess, map, codes, enable_per_target_ignores, tcx, collector } Self { sess, map, codes, enable_per_target_ignores, tcx, collector }
} }
pub fn collect_crate(mut self) -> Vec<ScrapedDoctest> { pub fn collect_crate(mut self) -> Vec<ScrapedDocTest> {
let tcx = self.tcx; let tcx = self.tcx;
self.visit_testable("".to_string(), CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| { self.visit_testable("".to_string(), CRATE_DEF_ID, tcx.hir().span(CRATE_HIR_ID), |this| {
tcx.hir().walk_toplevel_module(this) tcx.hir().walk_toplevel_module(this)

View File

@ -738,7 +738,7 @@ pub(crate) const fn offset(self) -> usize {
} }
} }
pub(crate) fn find_testable_code<T: doctest::DoctestVisitor>( pub(crate) fn find_testable_code<T: doctest::DocTestVisitor>(
doc: &str, doc: &str,
tests: &mut T, tests: &mut T,
error_codes: ErrorCodes, error_codes: ErrorCodes,
@ -748,7 +748,7 @@ pub(crate) fn find_testable_code<T: doctest::DoctestVisitor>(
find_codes(doc, tests, error_codes, enable_per_target_ignores, extra_info, false) find_codes(doc, tests, error_codes, enable_per_target_ignores, extra_info, false)
} }
pub(crate) fn find_codes<T: doctest::DoctestVisitor>( pub(crate) fn find_codes<T: doctest::DocTestVisitor>(
doc: &str, doc: &str,
tests: &mut T, tests: &mut T,
error_codes: ErrorCodes, error_codes: ErrorCodes,

View File

@ -45,7 +45,7 @@ pub(crate) struct Tests {
pub(crate) found_tests: usize, pub(crate) found_tests: usize,
} }
impl crate::doctest::DoctestVisitor for Tests { impl crate::doctest::DocTestVisitor for Tests {
fn visit_test(&mut self, _: String, config: LangString, _: MdRelLine) { fn visit_test(&mut self, _: String, config: LangString, _: MdRelLine) {
if config.rust && config.ignore == Ignore::None { if config.rust && config.ignore == Ignore::None {
self.found_tests += 1; self.found_tests += 1;