Skip to content

Commit 09a7051

Browse files
authored
Rollup merge of rust-lang#59940 - GuillaumeGomez:rustdoc-test, r=ollie27
Set cfg(test) when rustdoc is running with --test option Following a [discussion on twitter](https://twitter.com/burntsushi5/status/1117091914199785473), I proposed this change. What do you think about it? r? @QuietMisdreavus cc @BurntSushi
2 parents 294e89d + 459d677 commit 09a7051

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/librustdoc/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ impl Options {
351351
.unwrap_or_else(|| PathBuf::from("doc"));
352352
let mut cfgs = matches.opt_strs("cfg");
353353
cfgs.push("rustdoc".to_string());
354+
if should_test {
355+
cfgs.push("test".to_string());
356+
}
354357

355358
let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
356359

src/test/rustdoc-ui/cfg-test.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile-pass
2+
// compile-flags:--test
3+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
4+
5+
/// this doctest will be ignored:
6+
///
7+
/// ```
8+
/// assert!(false);
9+
/// ```
10+
#[cfg(not(test))]
11+
pub struct Foo;
12+
13+
/// this doctest will be tested:
14+
///
15+
/// ```
16+
/// assert!(true);
17+
/// ```
18+
#[cfg(test)]
19+
pub struct Foo;

src/test/rustdoc-ui/cfg-test.stdout

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/cfg-test.rs - Foo (line 15) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
6+

0 commit comments

Comments
 (0)