Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
86833ee
Clean up rustdoc `make_test` function code
GuillaumeGomez Apr 10, 2024
587c522
Create DocTest type to handle generation of doctests
GuillaumeGomez Apr 11, 2024
4867760
Split doctests into categories
GuillaumeGomez Apr 12, 2024
78171c6
Compile all (compatible) doctests as one
GuillaumeGomez Apr 13, 2024
e022387
Remove `ignore_to_bool` function
GuillaumeGomez Apr 15, 2024
cce04bb
Set `ignore` value directly into `make_test`
GuillaumeGomez Apr 15, 2024
b8f0f2c
Add fallback in case the "all in one" doctests failed to compile
GuillaumeGomez Apr 15, 2024
56acb5d
Create `DocTestInfo` to improve API of `run_test`
GuillaumeGomez Apr 15, 2024
882d119
Correctly handle test args and move `ignore`d doctests into the one file
GuillaumeGomez Apr 15, 2024
45b075f
Fix case where AST failed to parse to give better errors
GuillaumeGomez Apr 15, 2024
cdb59a1
Correctly handle exit status
GuillaumeGomez Apr 15, 2024
e6f38c5
Bless rustdoc ui tests
GuillaumeGomez Apr 15, 2024
2c27d9c
Run doctests by batches instead of running them all at once when ther…
GuillaumeGomez Apr 16, 2024
76f70fa
Don't set `test_harness` to `true` by default as it would generate in…
GuillaumeGomez Apr 16, 2024
f235cbf
Ignore test using link which creates segfault
GuillaumeGomez Apr 16, 2024
b5ca844
Improve handling of `--persist-doctests` with combined doctests and u…
GuillaumeGomez Apr 17, 2024
e4b74f4
Run merged doctests as one instead of using batches
GuillaumeGomez Apr 29, 2024
7ea8909
Allow to merge doctests only starting the 2024 edition
GuillaumeGomez Apr 29, 2024
5a3d239
Add standalone doctest attribute
GuillaumeGomez Apr 30, 2024
847664a
Add documentation for the doctest `standalone` attribute
GuillaumeGomez Apr 30, 2024
24a23e1
Improve code
GuillaumeGomez May 3, 2024
88cbb78
Improve code and rustdoc book
GuillaumeGomez May 12, 2024
222f7dc
Add equivalent of rustdoc-ui test `failed-doctest-should-panic` for t…
GuillaumeGomez May 12, 2024
2e3c2aa
If there is any AST error with a doctest, we make it a standalone test
GuillaumeGomez May 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve code
  • Loading branch information
GuillaumeGomez committed May 18, 2024
commit 24a23e13c98390d026cd17bce9a2d7d912b02f26
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ pub fn init() {
If you run `rustdoc --test` on this code, it'll panic on the second doctest being
run because `IS_INIT` value is not `false` anymore.

This is where the `standalone` attribute comes in: ittells `rustdoc` that a doctest
This is where the `standalone` attribute comes in: it tells `rustdoc` that a doctest
should not be merged with the others and should be run in its own process. So the
previous code should use it:

Expand Down
69 changes: 43 additions & 26 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,22 +922,38 @@ pub const TEST: test::TestDescAndFn = test::TestDescAndFn {{
}
}

pub(crate) struct MakeTestArgs<'a, 'b> {
pub source_code: String,
pub crate_name: Option<Cow<'a, str>>,
pub edition: Edition,
pub name: String,
pub lang_string: LangString,
pub line: usize,
pub file: String,
pub rustdoc_test_options: Arc<IndividualTestOptions>,
pub test_id: String,
pub target_str: &'b str,
pub path: PathBuf,
pub no_run: bool,
}

/// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
/// lines before the test code begins as well as if the output stream supports colors or not.
pub(crate) fn make_test(
s: String,
crate_name: Option<Cow<'_, str>>,
edition: Edition,
name: String,
lang_string: LangString,
line: usize,
file: String,
rustdoc_test_options: Arc<IndividualTestOptions>,
test_id: String,
target_str: &str,
path: PathBuf,
no_run: bool,
) -> DocTest {
pub(crate) fn make_test(test_args: MakeTestArgs<'_, '_>) -> DocTest {
let MakeTestArgs {
source_code,
crate_name,
edition,
name,
lang_string,
line,
file,
rustdoc_test_options,
test_id,
target_str,
path,
no_run,
} = test_args;
let outdir = Arc::new(if let Some(ref path) = rustdoc_test_options.persist_doctests {
let mut path = path.clone();
path.push(&test_id);
Expand All @@ -950,7 +966,7 @@ pub(crate) fn make_test(
// FIXME: This partition source is pretty bad. Something like
// <https://github.com/rust-lang/rust/commit/7eb5a994673092ebbcb07afc28be3a251c6c94c2> would be
// a much better approach.
let (crate_attrs, everything_else, crates) = partition_source(&s, edition);
let (crate_attrs, everything_else, crates) = partition_source(&source_code, edition);
let mut supports_color = false;
let crate_name = crate_name.map(|c| c.into_owned());

Expand All @@ -963,7 +979,7 @@ pub(crate) fn make_test(
use rustc_parse::parser::ForceCollect;
use rustc_span::source_map::FilePathMapping;

let filename = FileName::anon_source_code(&s);
let filename = FileName::anon_source_code(&source_code);
let source = format!("{crates}{everything_else}");

// Any errors in parsing should also appear when the doctest is compiled for real, so just
Expand Down Expand Up @@ -1053,7 +1069,7 @@ pub(crate) fn make_test(
// If the parser panicked due to a fatal error, pass the test code through unchanged.
// The error will be reported during compilation.
return DocTest {
test_code: s,
test_code: source_code,
supports_color: false,
main_fn_span: None,
crate_attrs,
Expand Down Expand Up @@ -1081,7 +1097,8 @@ pub(crate) fn make_test(
// https://github.com/rust-lang/rust/issues/56898
if found_macro
&& main_fn_span.is_none()
&& s.lines()
&& source_code
.lines()
.map(|line| {
let comment = line.find("//");
if let Some(comment_begins) = comment { &line[0..comment_begins] } else { line }
Expand All @@ -1092,7 +1109,7 @@ pub(crate) fn make_test(
}

DocTest {
test_code: s,
test_code: source_code,
supports_color,
main_fn_span,
crate_attrs,
Expand Down Expand Up @@ -1677,20 +1694,20 @@ impl Tester for Collector {
);

debug!("creating test {name}: {test}");
let doctest = make_test(
test,
Some(crate_name),
let doctest = make_test(MakeTestArgs {
source_code: test,
crate_name: Some(crate_name),
edition,
name,
config,
lang_string: config,
line,
file,
Arc::clone(&self.rustdoc_test_options),
rustdoc_test_options: Arc::clone(&self.rustdoc_test_options),
test_id,
&target_str,
target_str: &target_str,
path,
no_run,
);
});
self.tests.add_doctest(
doctest,
&opts,
Expand Down
28 changes: 14 additions & 14 deletions src/librustdoc/doctest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ use std::path::PathBuf;
use std::sync::Arc;

fn make_test(input: String, krate: Option<&str>) -> DocTest {
super::make_test(
input,
krate.map(|k| k.into()),
DEFAULT_EDITION,
String::new(), // test name
LangString::empty_for_test(),
0, // line
String::new(), // file name
Arc::new(IndividualTestOptions::empty()),
String::new(), // test id
"", // target_str
PathBuf::new(), // path
true, // no_run
)
super::make_test(crate::doctest::MakeTestArgs {
source_code: input,
crate_name: krate.map(|k| k.into()),
edition: DEFAULT_EDITION,
name: String::new(),
lang_string: LangString::empty_for_test(),
line: 0,
file: String::new(),
rustdoc_test_options: Arc::new(IndividualTestOptions::empty()),
test_id: String::new(),
target_str: "",
path: PathBuf::new(),
no_run: true,
})
}

#[test]
Expand Down
26 changes: 13 additions & 13 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,20 +305,20 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {

let mut opts: GlobalTestOptions = Default::default();
opts.insert_indent_space = true;
let (test, _) = doctest::make_test(
test,
krate,
let (test, _) = doctest::make_test(crate::doctest::MakeTestArgs {
source_code: test,
crate_name: krate,
edition,
String::new(),
LangString::empty_for_test(),
0,
String::new(),
Arc::new(IndividualTestOptions::empty()),
String::new(),
"",
"doctest.rs".into(),
true,
)
name: String::new(),
lang_string: LangString::empty_for_test(),
line: 0,
file: String::new(),
rustdoc_test_options: Arc::new(IndividualTestOptions::empty()),
test_id: String::new(),
target_str: "",
path: "doctest.rs".into(),
no_run: true,
})
.generate_unique_doctest(false, &opts, None);
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };

Expand Down