Skip to content

Commit af0edfe

Browse files
committed
Auto merge of #4202 - dethoter:issue-4199, r=alexcrichton
Fix an incorrect merge of credentials. Add a new test for that. Fix for #4199.
2 parents 4733e07 + 03a7f05 commit af0edfe

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/cargo/util/config.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,18 @@ impl Config {
486486
_ => unreachable!(),
487487
};
488488

489-
let mut registry = cfg.entry("registry".into())
490-
.or_insert(CV::Table(HashMap::new(),
491-
PathBuf::from(".")));
492-
registry.merge(value).chain_err(|| {
493-
format!("failed to merge configuration at `{}`",
494-
credentials.display())
495-
})?;
489+
let registry = cfg.entry("registry".into())
490+
.or_insert(CV::Table(HashMap::new(), PathBuf::from(".")));
491+
492+
match (registry, value) {
493+
(&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => {
494+
let new = mem::replace(new, HashMap::new());
495+
for (key, value) in new.into_iter() {
496+
old.insert(key, value);
497+
}
498+
}
499+
_ => unreachable!(),
500+
}
496501

497502
Ok(())
498503
}

tests/login.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use cargotest::cargo_process;
1111
use cargotest::support::execs;
1212
use cargotest::support::registry::registry;
1313
use cargotest::install::cargo_home;
14+
use cargo::util::config::Config;
15+
use cargo::core::Shell;
1416
use hamcrest::{assert_that, existing_file, is_not};
1517

1618
const TOKEN: &str = "test-token";
@@ -110,3 +112,17 @@ fn login_without_credentials() {
110112
File::open(&credentials).unwrap().read_to_string(&mut contents).unwrap();
111113
assert!(check_host_token(contents.parse().unwrap()));
112114
}
115+
116+
#[test]
117+
fn new_credentials_is_used_instead_old() {
118+
setup_old_credentials();
119+
setup_new_credentials();
120+
121+
assert_that(cargo_process().arg("login")
122+
.arg("--host").arg(registry().to_string()).arg(TOKEN),
123+
execs().with_status(0));
124+
125+
let config = Config::new(Shell::new(), cargo_home(), cargo_home());
126+
let token = config.get_string("registry.token").unwrap().map(|p| p.val);
127+
assert!(token.unwrap() == TOKEN);
128+
}

0 commit comments

Comments
 (0)