Skip to content

Commit

Permalink
Auto merge of rust-lang#8012 - ehuss:fix-config-profile-test, r=alexc…
Browse files Browse the repository at this point in the history
…richton

Fix config profiles using "dev" in `cargo test`.

Fix a bug where the "dev" profile was not loaded from config when running `cargo test` when "dev" is not listed in `Cargo.toml`.

There was a mistake in rust-lang#7750 where it did not consider implicit profiles. Config profiles need to be loaded explicitly in order to properly handle environment variables. However, it was only looking at the profile requested on the command-line and those listed in `Cargo.toml`. `cargo test` also implicitly uses the "dev" profile for dependencies, so make sure those are loaded from config as well.
  • Loading branch information
bors authored and ehuss committed Mar 17, 2020
1 parent 7b1e9f5 commit a8fe057
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,27 +999,33 @@ fn merge_config_profiles(
Some(profiles) => profiles.get_all().clone(),
None => BTreeMap::new(),
};
// List of profile names to check if defined in config only.
let mut check_to_add = vec![requested_profile];
// Set of profile names to check if defined in config only.
let mut check_to_add = HashSet::new();
check_to_add.insert(requested_profile);
// Merge config onto manifest profiles.
for (name, profile) in &mut profiles {
if let Some(config_profile) = get_config_profile(name, config, features)? {
profile.merge(&config_profile);
}
if let Some(inherits) = &profile.inherits {
check_to_add.push(*inherits);
check_to_add.insert(*inherits);
}
}
// Add the built-in profiles. This is important for things like `cargo
// test` which implicitly use the "dev" profile for dependencies.
for name in &["dev", "release", "test", "bench"] {
check_to_add.insert(InternedString::new(name));
}
// Add config-only profiles.
// Need to iterate repeatedly to get all the inherits values.
let mut current = Vec::new();
let mut current = HashSet::new();
while !check_to_add.is_empty() {
std::mem::swap(&mut current, &mut check_to_add);
for name in current.drain(..) {
for name in current.drain() {
if !profiles.contains_key(&name) {
if let Some(config_profile) = get_config_profile(&name, config, features)? {
if let Some(inherits) = &config_profile.inherits {
check_to_add.push(*inherits);
check_to_add.insert(*inherits);
}
profiles.insert(name, config_profile);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/testsuite/profile_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Tests for profiles defined in config files.

use cargo_test_support::paths::CargoPathExt;
use cargo_test_support::registry::Package;
use cargo_test_support::{basic_lib_manifest, paths, project};

#[cargo_test]
Expand Down Expand Up @@ -454,3 +455,38 @@ fn named_env_profile() {
.with_stderr_contains("[..]-C codegen-units=1 [..]")
.run();
}

#[cargo_test]
fn test_with_dev_profile() {
// `cargo test` uses "dev" profile for dependencies.
Package::new("somedep", "1.0.0").publish();
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
somedep = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();
p.cargo("test --lib --no-run -v")
.env("CARGO_PROFILE_DEV_DEBUG", "0")
.with_stderr(
"\
[UPDATING] [..]
[DOWNLOADING] [..]
[DOWNLOADED] [..]
[COMPILING] somedep v1.0.0
[RUNNING] `rustc --crate-name somedep [..]-C debuginfo=0[..]
[COMPILING] foo v0.1.0 [..]
[RUNNING] `rustc --crate-name foo [..]-C debuginfo=2[..]
[FINISHED] [..]
",
)
.run();
}

0 comments on commit a8fe057

Please sign in to comment.