Skip to content

Commit

Permalink
Auto merge of #4202 - dethoter:issue-4199, r=alexcrichton
Browse files Browse the repository at this point in the history
Fix an incorrect merge of credentials. Add a new test for that.

Fix for #4199.
  • Loading branch information
bors committed Jun 21, 2017
2 parents 4733e07 + 03a7f05 commit af0edfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,18 @@ impl Config {
_ => unreachable!(),
};

let mut registry = cfg.entry("registry".into())
.or_insert(CV::Table(HashMap::new(),
PathBuf::from(".")));
registry.merge(value).chain_err(|| {
format!("failed to merge configuration at `{}`",
credentials.display())
})?;
let registry = cfg.entry("registry".into())
.or_insert(CV::Table(HashMap::new(), PathBuf::from(".")));

match (registry, value) {
(&mut CV::Table(ref mut old, _), CV::Table(ref mut new, _)) => {
let new = mem::replace(new, HashMap::new());
for (key, value) in new.into_iter() {
old.insert(key, value);
}
}
_ => unreachable!(),
}

Ok(())
}
Expand Down
16 changes: 16 additions & 0 deletions tests/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use cargotest::cargo_process;
use cargotest::support::execs;
use cargotest::support::registry::registry;
use cargotest::install::cargo_home;
use cargo::util::config::Config;
use cargo::core::Shell;
use hamcrest::{assert_that, existing_file, is_not};

const TOKEN: &str = "test-token";
Expand Down Expand Up @@ -110,3 +112,17 @@ fn login_without_credentials() {
File::open(&credentials).unwrap().read_to_string(&mut contents).unwrap();
assert!(check_host_token(contents.parse().unwrap()));
}

#[test]
fn new_credentials_is_used_instead_old() {
setup_old_credentials();
setup_new_credentials();

assert_that(cargo_process().arg("login")
.arg("--host").arg(registry().to_string()).arg(TOKEN),
execs().with_status(0));

let config = Config::new(Shell::new(), cargo_home(), cargo_home());
let token = config.get_string("registry.token").unwrap().map(|p| p.val);
assert!(token.unwrap() == TOKEN);
}

0 comments on commit af0edfe

Please sign in to comment.