about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2024-06-04 01:05:50 -0700
committerGuillaume Gomez <guillaume.gomez@huawei.com>2024-06-07 17:53:45 +0200
commit3670ad59ade784226515327f2d776f02a6613f2c (patch)
tree618f1ad10e63121fead154a9041941b8360a67d1
parent815c447680a984c953d9c00a774aa151651f075f (diff)
downloadrust-3670ad59ade784226515327f2d776f02a6613f2c.tar.gz
rust-3670ad59ade784226515327f2d776f02a6613f2c.zip
Fix broken rustdoc unit tests
-rw-r--r--src/librustdoc/doctest/tests.rs59
1 files changed, 37 insertions, 22 deletions
diff --git a/src/librustdoc/doctest/tests.rs b/src/librustdoc/doctest/tests.rs
index 9629acb31eb..9124ec63267 100644
--- a/src/librustdoc/doctest/tests.rs
+++ b/src/librustdoc/doctest/tests.rs
@@ -1,10 +1,23 @@
+use std::path::PathBuf;
+
 use super::{make_test, GlobalTestOptions};
 use rustc_span::edition::DEFAULT_EDITION;
 
+/// Default [`GlobalTestOptions`] for these unit tests.
+fn default_global_opts(crate_name: impl Into<String>) -> GlobalTestOptions {
+    GlobalTestOptions {
+        crate_name: crate_name.into(),
+        no_crate_inject: false,
+        insert_indent_space: false,
+        attrs: vec![],
+        args_file: PathBuf::new(),
+    }
+}
+
 #[test]
 fn make_test_basic() {
     //basic use: wraps with `fn main`, adds `#![allow(unused)]`
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("");
     let input = "assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
 fn main() {
@@ -19,7 +32,7 @@ assert_eq!(2+2, 4);
 fn make_test_crate_name_no_use() {
     // If you give a crate name but *don't* use it within the test, it won't bother inserting
     // the `extern crate` statement.
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("asdf");
     let input = "assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
 fn main() {
@@ -34,7 +47,7 @@ assert_eq!(2+2, 4);
 fn make_test_crate_name() {
     // If you give a crate name and use it within the test, it will insert an `extern crate`
     // statement before `fn main`.
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("asdf");
     let input = "use asdf::qwop;
 assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
@@ -53,8 +66,7 @@ assert_eq!(2+2, 4);
 fn make_test_no_crate_inject() {
     // Even if you do use the crate within the test, setting `opts.no_crate_inject` will skip
     // adding it anyway.
-    let opts =
-        GlobalTestOptions { no_crate_inject: true, attrs: vec![], insert_indent_space: false };
+    let opts = GlobalTestOptions { no_crate_inject: true, ..default_global_opts("asdf") };
     let input = "use asdf::qwop;
 assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
@@ -72,7 +84,7 @@ fn make_test_ignore_std() {
     // Even if you include a crate name, and use it in the doctest, we still won't include an
     // `extern crate` statement if the crate is "std" -- that's included already by the
     // compiler!
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("std");
     let input = "use std::*;
 assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
@@ -89,7 +101,7 @@ assert_eq!(2+2, 4);
 fn make_test_manual_extern_crate() {
     // When you manually include an `extern crate` statement in your doctest, `make_test`
     // assumes you've included one for your own crate too.
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("asdf");
     let input = "extern crate asdf;
 use asdf::qwop;
 assert_eq!(2+2, 4);";
@@ -106,7 +118,7 @@ assert_eq!(2+2, 4);
 
 #[test]
 fn make_test_manual_extern_crate_with_macro_use() {
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("asdf");
     let input = "#[macro_use] extern crate asdf;
 use asdf::qwop;
 assert_eq!(2+2, 4);";
@@ -125,7 +137,7 @@ assert_eq!(2+2, 4);
 fn make_test_opts_attrs() {
     // If you supplied some doctest attributes with `#![doc(test(attr(...)))]`, it will use
     // those instead of the stock `#![allow(unused)]`.
-    let mut opts = GlobalTestOptions::default();
+    let mut opts = default_global_opts("asdf");
     opts.attrs.push("feature(sick_rad)".to_string());
     let input = "use asdf::qwop;
 assert_eq!(2+2, 4);";
@@ -159,7 +171,7 @@ assert_eq!(2+2, 4);
 fn make_test_crate_attrs() {
     // Including inner attributes in your doctest will apply them to the whole "crate", pasting
     // them outside the generated main function.
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("");
     let input = "#![feature(sick_rad)]
 assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
@@ -175,7 +187,7 @@ assert_eq!(2+2, 4);
 #[test]
 fn make_test_with_main() {
     // Including your own `fn main` wrapper lets the test use it verbatim.
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("");
     let input = "fn main() {
     assert_eq!(2+2, 4);
 }";
@@ -191,7 +203,7 @@ fn main() {
 #[test]
 fn make_test_fake_main() {
     // ... but putting it in a comment will still provide a wrapper.
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("");
     let input = "//Ceci n'est pas une `fn main`
 assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
@@ -207,7 +219,7 @@ assert_eq!(2+2, 4);
 #[test]
 fn make_test_dont_insert_main() {
     // Even with that, if you set `dont_insert_main`, it won't create the `fn main` wrapper.
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("");
     let input = "//Ceci n'est pas une `fn main`
 assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
@@ -219,8 +231,8 @@ assert_eq!(2+2, 4);"
 }
 
 #[test]
-fn make_test_issues_21299_33731() {
-    let opts = GlobalTestOptions::default();
+fn make_test_issues_21299() {
+    let opts = default_global_opts("");
 
     let input = "// fn main
 assert_eq!(2+2, 4);";
@@ -234,6 +246,11 @@ assert_eq!(2+2, 4);
 
     let (output, len, _) = make_test(input, None, false, &opts, DEFAULT_EDITION, None);
     assert_eq!((output, len), (expected, 2));
+}
+
+#[test]
+fn make_test_issues_33731() {
+    let opts = default_global_opts("asdf");
 
     let input = "extern crate hella_qwop;
 assert_eq!(asdf::foo, 4);";
@@ -253,7 +270,7 @@ assert_eq!(asdf::foo, 4);
 
 #[test]
 fn make_test_main_in_macro() {
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("my_crate");
     let input = "#[macro_use] extern crate my_crate;
 test_wrapper! {
     fn main() {}
@@ -272,7 +289,7 @@ test_wrapper! {
 #[test]
 fn make_test_returns_result() {
     // creates an inner function and unwraps it
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("");
     let input = "use std::io;
 let mut input = String::new();
 io::stdin().read_line(&mut input)?;
@@ -292,7 +309,7 @@ Ok::<(), io:Error>(())
 #[test]
 fn make_test_named_wrapper() {
     // creates an inner function with a specific name
-    let opts = GlobalTestOptions::default();
+    let opts = default_global_opts("");
     let input = "assert_eq!(2+2, 4);";
     let expected = "#![allow(unused)]
 fn main() { #[allow(non_snake_case)] fn _doctest_main__some_unique_name() {
@@ -307,8 +324,7 @@ assert_eq!(2+2, 4);
 #[test]
 fn make_test_insert_extra_space() {
     // will insert indent spaces in the code block if `insert_indent_space` is true
-    let opts =
-        GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
+    let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
     let input = "use std::*;
 assert_eq!(2+2, 4);
 eprintln!(\"hello anan\");
@@ -327,8 +343,7 @@ fn main() {
 #[test]
 fn make_test_insert_extra_space_fn_main() {
     // if input already has a fn main, it should insert a space before it
-    let opts =
-        GlobalTestOptions { no_crate_inject: false, attrs: vec![], insert_indent_space: true };
+    let opts = GlobalTestOptions { insert_indent_space: true, ..default_global_opts("") };
     let input = "use std::*;
 fn main() {
     assert_eq!(2+2, 4);