Skip to content

Commit 2593b86

Browse files
committed
Conditionally mark the test cfg as a well known cfg
1 parent 208f817 commit 2593b86

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,17 +1391,26 @@ fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
13911391
}
13921392
arg_feature.push("))");
13931393

1394-
// In addition to the package features, we also include the `test` cfg (since
1395-
// compiler-team#785, as to be able to someday apply yt conditionaly), as well
1396-
// the `docsrs` cfg from the docs.rs service.
1394+
// In addition to the package features, we also conditionaly include the `test` cfg
1395+
// based on the unit target "test" field (ie `lib.test = false`, `[[bin]] test = false` and
1396+
// others).
13971397
//
1398-
// We include `docsrs` here (in Cargo) instead of rustc, since there is a much closer
1398+
// We also include `docsrs` here (in Cargo) instead of rustc, since there is a much closer
13991399
// relationship between Cargo and docs.rs than rustc and docs.rs. In particular, all
14001400
// users of docs.rs use Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
1401+
let arg_extra = if unit.target.tested()
1402+
// Benchmarks default to `test = false` but most of them still use the test crate
1403+
// and the `#[test]` attribute, so for now always mark `test` as well known for them.
1404+
|| unit.target.is_bench()
1405+
{
1406+
OsString::from("cfg(docsrs,test)")
1407+
} else {
1408+
OsString::from("cfg(docsrs)")
1409+
};
14011410

14021411
vec![
14031412
OsString::from("--check-cfg"),
1404-
OsString::from("cfg(docsrs,test)"),
1413+
arg_extra,
14051414
OsString::from("--check-cfg"),
14061415
arg_feature,
14071416
]

src/doc/src/reference/cargo-targets.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ the target name.
216216
### The `test` field
217217

218218
The `test` field indicates whether or not the target is tested by default by
219-
[`cargo test`]. The default is `true` for lib, bins, and tests.
219+
[`cargo test`], and whenever the target is expected to have tests. Warnings
220+
may be reported when tests are unexpected (i.e., `test = false`).
221+
222+
The default is `true` for lib, bins, and tests.
220223

221224
> **Note**: Examples are built by [`cargo test`] by default to ensure they
222225
> continue to compile, but they are not *tested* by default. Setting `test =

tests/testsuite/check_cfg.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,18 @@ fn test_false_lib() {
336336
.build();
337337

338338
p.cargo("check -v")
339-
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
339+
.with_stderr_does_not_contain(x!("rustc" => "cfg" of "docsrs,test"))
340+
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
340341
.run();
341342

342343
p.cargo("clean").run();
343344
p.cargo("test -v")
344-
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
345+
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
345346
.run();
346347

347348
p.cargo("clean").run();
348349
p.cargo("test --lib -v")
349-
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
350+
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
350351
.run();
351352
}
352353

@@ -372,7 +373,8 @@ fn test_false_bins() {
372373
.build();
373374

374375
p.cargo("check -v")
375-
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test")) // for foo & deamon
376+
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test")) // for foo
377+
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs")) // for deamon
376378
.run();
377379
}
378380

@@ -401,7 +403,8 @@ fn test_false_examples() {
401403
.build();
402404

403405
p.cargo("check --examples -v")
404-
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
406+
.with_stderr_does_not_contain(x!("rustc" => "cfg" of "docsrs,test"))
407+
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
405408
.run();
406409
}
407410

tests/testsuite/test.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4191,7 +4191,19 @@ fn test_hint_workspace_virtual() {
41914191
.file("a/src/lib.rs", "#[test] fn t1() {}")
41924192
.file("b/Cargo.toml", &basic_manifest("b", "0.1.0"))
41934193
.file("b/src/lib.rs", "#[test] fn t1() {assert!(false)}")
4194-
.file("c/Cargo.toml", &basic_manifest("c", "0.1.0"))
4194+
.file(
4195+
"c/Cargo.toml",
4196+
r#"
4197+
[package]
4198+
name = "c"
4199+
version = "0.1.0"
4200+
edition = "2015"
4201+
4202+
[[example]]
4203+
name = "ex1"
4204+
test = true
4205+
"#,
4206+
)
41954207
.file(
41964208
"c/src/lib.rs",
41974209
r#"
@@ -4275,14 +4287,17 @@ fn test_hint_workspace_virtual() {
42754287
[ERROR] test failed, to rerun pass `-p c --bin c`
42764288
[RUNNING] tests/t1.rs (target/debug/deps/t1-[HASH][EXE])
42774289
[ERROR] test failed, to rerun pass `-p c --test t1`
4290+
[RUNNING] unittests examples/ex1.rs (target/debug/examples/ex1-[HASH][EXE])
4291+
[ERROR] test failed, to rerun pass `-p c --example ex1`
42784292
[DOCTEST] a
42794293
[DOCTEST] b
42804294
[DOCTEST] c
42814295
[ERROR] doctest failed, to rerun pass `-p c --doc`
4282-
[ERROR] 4 targets failed:
4296+
[ERROR] 5 targets failed:
42834297
`-p b --lib`
42844298
`-p c --bin c`
42854299
`-p c --test t1`
4300+
`-p c --example ex1`
42864301
`-p c --doc`
42874302
42884303
"#]])

0 commit comments

Comments
 (0)