Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 010731d

Browse files
Add new run-make tests for doctests
1 parent 2fd8d1c commit 010731d

File tree

8 files changed

+171
-0
lines changed

8 files changed

+171
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Check that valid binaries are persisted by running them, regardless of whether the
2+
// --run or --no-run option is used.
3+
4+
use run_make_support::fs_wrapper::{create_dir, remove_dir_all};
5+
use run_make_support::{run, rustc, rustdoc};
6+
use std::path::Path;
7+
8+
fn setup_test_env<F: FnOnce(&Path, &Path)>(callback: F) {
9+
let out_dir = Path::new("doctests");
10+
create_dir(&out_dir);
11+
rustc().input("t.rs").crate_type("rlib").run();
12+
callback(&out_dir, Path::new("libt.rlib"));
13+
remove_dir_all(out_dir);
14+
}
15+
16+
fn check_generated_binaries() {
17+
run("doctests/merged_doctest_2024/rust_out");
18+
}
19+
20+
fn main() {
21+
setup_test_env(|out_dir, extern_path| {
22+
rustdoc()
23+
.input("t.rs")
24+
.arg("-Zunstable-options")
25+
.arg("--test")
26+
.arg("--persist-doctests")
27+
.arg(out_dir)
28+
.extern_("t", extern_path)
29+
.edition("2024")
30+
.run();
31+
check_generated_binaries();
32+
});
33+
setup_test_env(|out_dir, extern_path| {
34+
rustdoc()
35+
.input("t.rs")
36+
.arg("-Zunstable-options")
37+
.arg("--test")
38+
.arg("--persist-doctests")
39+
.arg(out_dir)
40+
.extern_("t", extern_path)
41+
.arg("--no-run")
42+
.edition("2024")
43+
.run();
44+
check_generated_binaries();
45+
});
46+
// Behavior with --test-run-directory with relative paths.
47+
setup_test_env(|_, _| {
48+
let run_dir_path = Path::new("rundir");
49+
create_dir(&run_dir_path);
50+
51+
rustdoc()
52+
.input("t.rs")
53+
.arg("-Zunstable-options")
54+
.arg("--test")
55+
.arg("--persist-doctests")
56+
.arg("doctests")
57+
.arg("--test-run-directory")
58+
.arg(run_dir_path)
59+
.extern_("t", "libt.rlib")
60+
.edition("2024")
61+
.run();
62+
63+
remove_dir_all(run_dir_path);
64+
});
65+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// Fungle the foople.
2+
/// ```
3+
/// t::foople();
4+
/// ```
5+
pub fn foople() {}
6+
7+
/// Flomble the florp
8+
/// ```
9+
/// t::florp();
10+
/// ```
11+
pub fn florp() {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
running 2 tests
3+
test doctest.rs - (line 4) ... ok
4+
test doctest.rs - init (line 8) ... ok
5+
6+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
7+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
running 2 tests
3+
test doctest.rs - (line 4) ... ok
4+
test doctest.rs - init (line 8) ... ok
5+
6+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
7+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![crate_name = "foo"]
2+
#![crate_type = "lib"]
3+
4+
//! ```standalone
5+
//! foo::init();
6+
//! ```
7+
8+
/// ```standalone
9+
/// foo::init();
10+
/// ```
11+
pub fn init() {
12+
static mut IS_INIT: bool = false;
13+
14+
unsafe {
15+
assert!(!IS_INIT);
16+
IS_INIT = true;
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
running 2 tests
3+
test doctest-standalone.rs - (line 4) ... ok
4+
test doctest-standalone.rs - init (line 8) ... ok
5+
6+
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
7+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![crate_name = "foo"]
2+
#![crate_type = "lib"]
3+
4+
//! ```
5+
//! foo::init();
6+
//! ```
7+
8+
/// ```
9+
/// foo::init();
10+
/// ```
11+
pub fn init() {
12+
static mut IS_INIT: bool = false;
13+
14+
unsafe {
15+
assert!(!IS_INIT);
16+
IS_INIT = true;
17+
}
18+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use run_make_support::{cwd, diff, rustc, rustdoc};
2+
use std::path::Path;
3+
4+
fn test_and_compare(input_file: &str, stdout_file: &str, edition: &str, dep: &Path) {
5+
let mut cmd = rustdoc();
6+
7+
let output = cmd
8+
.input(input_file)
9+
.arg("--test")
10+
.arg("-Zunstable-options")
11+
.edition(edition)
12+
.arg("--test-args=--test-threads=1")
13+
.extern_("foo", dep.display().to_string())
14+
.env("RUST_BACKTRACE", "short")
15+
.run();
16+
17+
diff()
18+
.expected_file(stdout_file)
19+
.actual_text("output", output.stdout_utf8())
20+
.normalize(r#"finished in \d+\.\d+s"#, "finished in $$TIME")
21+
.run();
22+
}
23+
24+
fn main() {
25+
let out_file = cwd().join("libfoo.rlib");
26+
27+
rustc().input("doctest.rs").crate_type("rlib").output(&out_file).run();
28+
29+
// First we ensure that running with the 2024 edition will not fail at runtime.
30+
test_and_compare("doctest.rs", "doctest-2024.stdout", "2024", &out_file);
31+
32+
// Then we ensure that running with an edition < 2024 will not fail at runtime.
33+
test_and_compare("doctest.rs", "doctest-2021.stdout", "2021", &out_file);
34+
35+
// Now we check with the standalone attribute which should succeed in all cases.
36+
test_and_compare("doctest-standalone.rs", "doctest-standalone.stdout", "2024", &out_file);
37+
test_and_compare("doctest-standalone.rs", "doctest-standalone.stdout", "2021", &out_file);
38+
}

0 commit comments

Comments
 (0)