Improve code readability

This commit is contained in:
Guillaume Gomez 2024-06-24 13:26:02 +02:00
parent 0fbc32ce42
commit a3bc2c7478
4 changed files with 19 additions and 5 deletions

View File

@ -448,6 +448,12 @@ struct RunnableDoctest {
no_run: bool,
}
impl RunnableDoctest {
fn path_for_merged_doctest(&self) -> PathBuf {
self.test_opts.outdir.path().join(&format!("doctest_{}.rs", self.edition))
}
}
fn run_test(
doctest: RunnableDoctest,
rustdoc_options: &RustdocOptions,
@ -528,8 +534,7 @@ fn run_test(
if is_multiple_tests {
// It makes the compilation failure much faster if it is for a combined doctest.
compiler.arg("--error-format=short");
let input_file =
doctest.test_opts.outdir.path().join(&format!("doctest_{}.rs", doctest.edition));
let input_file = doctest.path_for_merged_doctest();
if std::fs::write(&input_file, &doctest.full_test_code).is_err() {
// If we cannot write this file for any reason, we leave. All combined tests will be
// tested as standalone tests.
@ -809,9 +814,9 @@ impl CreateRunnableDoctests {
edition,
self.can_merge_doctests,
Some(test_id),
Some(&scraped_test.langstr),
);
let is_standalone = !self.can_merge_doctests
|| !doctest.can_be_merged
let is_standalone = !doctest.can_be_merged
|| scraped_test.langstr.compile_fail
|| scraped_test.langstr.test_harness
|| scraped_test.langstr.standalone

View File

@ -1,6 +1,8 @@
//! Logic for transforming the raw code given by the user into something actually
//! runnable, e.g. by adding a `main` function if it doesn't already exist.
use crate::html::markdown::LangString;
use std::io;
use rustc_ast as ast;
@ -42,7 +44,13 @@ impl DocTestBuilder {
can_merge_doctests: bool,
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
test_id: Option<String>,
lang_str: Option<&LangString>,
) -> Self {
let can_merge_doctests = can_merge_doctests
&& lang_str.is_some_and(|lang_str| {
!lang_str.compile_fail && !lang_str.test_harness && !lang_str.standalone
});
let SourceInfo { crate_attrs, maybe_crate_attrs, crates, everything_else } =
partition_source(source, edition);

View File

@ -16,6 +16,7 @@ fn make_test(
DEFAULT_EDITION,
false,
test_id.map(|s| s.to_string()),
None,
);
let (code, line_offset) =
doctest.generate_unique_doctest(test_code, dont_insert_main, opts, crate_name);

View File

@ -297,7 +297,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
attrs: vec![],
args_file: PathBuf::new(),
};
let doctest = doctest::DocTestBuilder::new(&test, krate, edition, false, None);
let doctest = doctest::DocTestBuilder::new(&test, krate, edition, false, None, None);
let (test, _) = doctest.generate_unique_doctest(&test, false, &opts, krate);
let channel = if test.contains("#![feature(") { "&amp;version=nightly" } else { "" };