Skip to content

Commit

Permalink
Conditionally mark the test cfg as a well known cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Jan 4, 2025
1 parent 350fd28 commit 9e47488
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,17 +1391,22 @@ fn check_cfg_args(unit: &Unit) -> Vec<OsString> {
}
arg_feature.push("))");

// In addition to the package features, we also include the `test` cfg (since
// compiler-team#785, as to be able to someday apply yt conditionaly), as well
// the `docsrs` cfg from the docs.rs service.
// In addition to the package features, we also conditionaly include the `test` cfg
// based on the unit target "test" field (ie `lib.test = false`, `[[bin]] test = false` and
// others).
//
// We include `docsrs` here (in Cargo) instead of rustc, since there is a much closer
// We also include `docsrs` here (in Cargo) instead of rustc, since there is a much closer
// relationship between Cargo and docs.rs than rustc and docs.rs. In particular, all
// users of docs.rs use Cargo, but not all users of rustc (like Rust-for-Linux) use docs.rs.
let arg_extra = if unit.target.tested() {
OsString::from("cfg(docsrs,test)")
} else {
OsString::from("cfg(docsrs)")
};

vec![
OsString::from("--check-cfg"),
OsString::from("cfg(docsrs,test)"),
arg_extra,
OsString::from("--check-cfg"),
arg_feature,
]
Expand Down
13 changes: 8 additions & 5 deletions tests/testsuite/check_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,18 @@ fn test_false_lib() {
.build();

p.cargo("check -v")
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
.with_stderr_does_not_contain(x!("rustc" => "cfg" of "docsrs,test"))
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
.run();

p.cargo("clean").run();
p.cargo("test -v")
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
.run();

p.cargo("clean").run();
p.cargo("test --lib -v")
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
.run();
}

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

p.cargo("check -v")
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test")) // for foo & deamon
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test")) // for foo
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs")) // for deamon
.run();
}

Expand Down Expand Up @@ -401,7 +403,8 @@ fn test_false_examples() {
.build();

p.cargo("check --examples -v")
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs,test"))
.with_stderr_does_not_contain(x!("rustc" => "cfg" of "docsrs,test"))
.with_stderr_contains(x!("rustc" => "cfg" of "docsrs"))
.run();
}

Expand Down

0 comments on commit 9e47488

Please sign in to comment.