Skip to content

Commit cc8cea2

Browse files
Create DocTestInfo to improve API of run_test
1 parent b42e9a3 commit cc8cea2

File tree

3 files changed

+43
-24
lines changed

3 files changed

+43
-24
lines changed

src/librustdoc/doctest.rs

+36-22
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,21 @@ fn wrapped_rustc_command(rustc_wrappers: &[PathBuf], rustc_binary: &Path) -> Com
366366
command
367367
}
368368

369+
pub(crate) struct DocTestInfo {
370+
line_offset: usize,
371+
line: usize,
372+
path: PathBuf,
373+
}
374+
369375
fn run_test(
370376
test: String,
371377
supports_color: bool,
372-
line_offset: usize,
373-
line: usize,
378+
test_info: Option<DocTestInfo>,
374379
rustdoc_options: Arc<IndividualTestOptions>,
375380
is_multiple_tests: bool,
376381
outdir: Arc<DirState>,
377382
mut lang_string: LangString,
378383
edition: Edition,
379-
path: PathBuf,
380384
report_unused_externs: impl Fn(UnusedExterns),
381385
) -> Result<(), TestFailure> {
382386
// Make sure we emit well-formed executable names for our target.
@@ -396,8 +400,13 @@ fn run_test(
396400
}
397401

398402
compiler.arg("--edition").arg(&edition.to_string());
399-
compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", path);
400-
compiler.env("UNSTABLE_RUSTDOC_TEST_LINE", format!("{}", line as isize - line_offset as isize));
403+
if let Some(test_info) = test_info {
404+
compiler.env("UNSTABLE_RUSTDOC_TEST_PATH", test_info.path);
405+
compiler.env(
406+
"UNSTABLE_RUSTDOC_TEST_LINE",
407+
format!("{}", test_info.line as isize - test_info.line_offset as isize),
408+
);
409+
}
401410
compiler.arg("-o").arg(&output_file);
402411
if lang_string.test_harness {
403412
compiler.arg("--test");
@@ -576,11 +585,14 @@ pub(crate) struct DocTest {
576585
name: String,
577586
lang_string: LangString,
578587
line: usize,
588+
// Path that will be displayed if the test failed to compile.
589+
path: PathBuf,
579590
file: String,
580591
failed_ast: bool,
581592
test_id: String,
582593
outdir: Arc<DirState>,
583594
rustdoc_test_options: Arc<IndividualTestOptions>,
595+
no_run: bool,
584596
}
585597

586598
impl DocTest {
@@ -698,12 +710,11 @@ impl DocTest {
698710
mut self,
699711
opts: &GlobalTestOptions,
700712
edition: Edition,
701-
path: PathBuf,
702713
unused_externs: Arc<Mutex<Vec<UnusedExterns>>>,
703714
) -> TestDescAndFn {
704715
let (code, line_offset) =
705716
self.generate_unique_doctest(self.lang_string.test_harness, opts, Some(&self.test_id));
706-
let Self { supports_color, rustdoc_test_options, lang_string, outdir, .. } = self;
717+
let Self { supports_color, rustdoc_test_options, lang_string, outdir, path, .. } = self;
707718
TestDescAndFn {
708719
desc: test::TestDesc {
709720
name: test::DynTestName(std::mem::replace(&mut self.name, String::new())),
@@ -727,14 +738,12 @@ impl DocTest {
727738
let res = run_test(
728739
code,
729740
supports_color,
730-
line_offset,
731-
self.line,
741+
Some(DocTestInfo { line_offset, line: self.line, path }),
732742
rustdoc_test_options,
733743
false,
734744
outdir,
735745
lang_string,
736746
edition,
737-
path,
738747
report_unused_externs,
739748
);
740749

@@ -849,7 +858,7 @@ pub const TEST: test::TestDescAndFn = test::TestDescAndFn {{
849858
should_panic = if self.lang_string.should_panic { "Yes" } else { "No" },
850859
// Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply
851860
// don't give it the function to run.
852-
runner = if self.lang_string.no_run { "Ok::<(), String>(())" } else { "self::main()" },
861+
runner = if self.no_run { "Ok::<(), String>(())" } else { "self::main()" },
853862
)
854863
.unwrap();
855864
test_id
@@ -869,6 +878,8 @@ pub(crate) fn make_test(
869878
rustdoc_test_options: Arc<IndividualTestOptions>,
870879
test_id: String,
871880
target_str: &str,
881+
path: PathBuf,
882+
no_run: bool,
872883
) -> DocTest {
873884
let outdir = Arc::new(if let Some(ref path) = rustdoc_test_options.persist_doctests {
874885
let mut path = path.clone();
@@ -1004,6 +1015,8 @@ pub(crate) fn make_test(
10041015
rustdoc_test_options,
10051016
outdir,
10061017
test_id,
1018+
path,
1019+
no_run,
10071020
};
10081021
};
10091022

@@ -1041,6 +1054,8 @@ pub(crate) fn make_test(
10411054
rustdoc_test_options,
10421055
outdir,
10431056
test_id,
1057+
path,
1058+
no_run,
10441059
}
10451060
}
10461061

@@ -1255,7 +1270,6 @@ impl DocTestKinds {
12551270
doctest: DocTest,
12561271
opts: &GlobalTestOptions,
12571272
edition: Edition,
1258-
path: PathBuf,
12591273
unused_externs: &Arc<Mutex<Vec<UnusedExterns>>>,
12601274
) {
12611275
if doctest.failed_ast
@@ -1267,7 +1281,6 @@ impl DocTestKinds {
12671281
self.standalone.push(doctest.generate_test_desc_and_fn(
12681282
opts,
12691283
edition,
1270-
path,
12711284
Arc::clone(unused_externs),
12721285
));
12731286
} else {
@@ -1327,32 +1340,31 @@ fn main() {{
13271340
if let Err(TestFailure::CompileError) = run_test(
13281341
output,
13291342
supports_color,
1330-
0,
1331-
0,
1343+
None,
13321344
rustdoc_test_options,
13331345
true,
13341346
outdir,
13351347
LangString::empty_for_test(),
13361348
edition,
1337-
PathBuf::from(format!("doctest_edition_{edition}.rs")),
13381349
|_: UnusedExterns| {},
13391350
) {
13401351
// We failed to compile all compatible tests as one so we push them into the
13411352
// "standalone" doctests.
13421353
debug!("Failed to compile compatible doctests for edition {edition} all at once");
1343-
for (pos, doctest) in doctests.into_iter().enumerate() {
1354+
for doctest in doctests {
13441355
standalone.push(doctest.generate_test_desc_and_fn(
13451356
&opts,
13461357
edition,
1347-
format!("doctest_{edition}_{pos}").into(),
13481358
Arc::clone(unused_externs),
13491359
));
13501360
}
13511361
}
13521362
}
13531363

1354-
standalone.sort_by(|a, b| a.desc.name.as_slice().cmp(&b.desc.name.as_slice()));
1355-
test::test_main(&test_args, standalone, None);
1364+
if !standalone.is_empty() {
1365+
standalone.sort_by(|a, b| a.desc.name.as_slice().cmp(&b.desc.name.as_slice()));
1366+
test::test_main(&test_args, standalone, None);
1367+
}
13561368
}
13571369
}
13581370

@@ -1465,7 +1477,7 @@ impl Tester for Collector {
14651477
let opts = self.opts.clone();
14661478
let edition = config.edition.unwrap_or(self.rustdoc_options.edition);
14671479
let target_str = self.rustdoc_options.target.to_string();
1468-
// let no_run = config.no_run || self.rustdoc_options.no_run;
1480+
let no_run = config.no_run || self.rustdoc_options.no_run;
14691481
if !config.compile_fail {
14701482
self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
14711483
}
@@ -1510,8 +1522,10 @@ impl Tester for Collector {
15101522
Arc::clone(&self.rustdoc_test_options),
15111523
test_id,
15121524
&target_str,
1525+
path,
1526+
no_run,
15131527
);
1514-
self.tests.add_doctest(doctest, &opts, edition, path, &self.unused_extern_reports);
1528+
self.tests.add_doctest(doctest, &opts, edition, &self.unused_extern_reports);
15151529
}
15161530

15171531
fn get_line(&self) -> usize {

src/librustdoc/doctest/tests.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{DocTest, GlobalTestOptions, IndividualTestOptions};
22
use crate::html::markdown::LangString;
33
use rustc_span::edition::DEFAULT_EDITION;
4+
use std::path::PathBuf;
45
use std::sync::Arc;
56

67
fn make_test(input: String, krate: Option<&str>) -> DocTest {
@@ -13,8 +14,10 @@ fn make_test(input: String, krate: Option<&str>) -> DocTest {
1314
0, // line
1415
String::new(), // file name
1516
Arc::new(IndividualTestOptions::empty()),
16-
String::new(), // test id
17-
"", // target_str
17+
String::new(), // test id
18+
"", // target_str
19+
PathBuf::new(), // path
20+
true, // no_run
1821
)
1922
}
2023

src/librustdoc/html/markdown.rs

+2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
317317
Arc::new(IndividualTestOptions::empty()),
318318
String::new(),
319319
"",
320+
"doctest.rs".into(),
321+
true,
320322
)
321323
.generate_unique_doctest(false, &opts, None);
322324
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };

0 commit comments

Comments
 (0)