Fix overwriting alternate registry token

When executing `cargo login`, 2nd alternate registry token overwrites
1st alternate registry token.

Fixes #7701.
This commit is contained in:
Takayuki Nakata 2019-12-13 21:54:07 +09:00
parent 19a0de242f
commit 6cbde6e2bf
3 changed files with 65 additions and 3 deletions

View file

@ -44,6 +44,12 @@ pub fn alt_registry_path() -> PathBuf {
pub fn alt_registry_url() -> Url {
Url::from_file_path(alt_registry_path()).ok().unwrap()
}
pub fn alt2_registry_path() -> PathBuf {
paths::root().join("alternative2-registry")
}
pub fn alt2_registry_url() -> Url {
Url::from_file_path(alt_registry_path()).ok().unwrap()
}
/// Gets the alternative-registry version of `dl_path`.
pub fn alt_dl_path() -> PathBuf {
paths::root().join("alt_dl")
@ -52,6 +58,13 @@ pub fn alt_dl_url() -> String {
let base = Url::from_file_path(alt_dl_path()).ok().unwrap();
format!("{}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate", base)
}
pub fn alt2_dl_path() -> PathBuf {
paths::root().join("alt2_dl")
}
pub fn alt2_dl_url() -> String {
let base = Url::from_file_path(alt2_dl_path()).ok().unwrap();
format!("{}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate", base)
}
/// Gets the alternative-registry version of `api_path`.
pub fn alt_api_path() -> PathBuf {
paths::root().join("alt_api")
@ -59,6 +72,12 @@ pub fn alt_api_path() -> PathBuf {
pub fn alt_api_url() -> Url {
Url::from_file_path(alt_api_path()).ok().unwrap()
}
pub fn alt2_api_path() -> PathBuf {
paths::root().join("alt2_api")
}
pub fn alt2_api_url() -> Url {
Url::from_file_path(alt2_api_path()).ok().unwrap()
}
/// A builder for creating a new package in a registry.
///
@ -166,9 +185,13 @@ pub fn init() {
[registries.alternative]
index = '{alt}'
[registries.alternative2]
index = '{alt2}'
"#,
reg = registry_url(),
alt = alt_registry_url()
alt = alt_registry_url(),
alt2 = alt2_registry_url()
)
.as_bytes()
));
@ -180,6 +203,9 @@ pub fn init() {
[registries.alternative]
token = "api-token"
[registries.alternative2]
token = "api-token"
"#
));
@ -212,6 +238,21 @@ pub fn init() {
)
.build();
fs::create_dir_all(alt_api_path().join("api/v1/crates")).unwrap();
// Initialize an alternative2 registry.
repo(&alt2_registry_path())
.file(
"config.json",
&format!(
r#"
{{"dl":"{}","api":"{}"}}
"#,
alt2_dl_url(),
alt2_api_url()
),
)
.build();
fs::create_dir_all(alt2_api_path().join("api/v1/crates")).unwrap();
}
impl Package {

View file

@ -1314,14 +1314,14 @@ pub fn save_credentials(cfg: &Config, token: String, registry: Option<String>) -
.open_rw(filename, cfg, "credentials' config file")?
};
let (key, value) = {
let (key, mut value) = {
let key = "token".to_string();
let value = ConfigValue::String(token, file.path().to_path_buf());
let mut map = HashMap::new();
map.insert(key, value);
let table = CV::Table(map, file.path().to_path_buf());
if let Some(registry) = registry {
if let Some(registry) = registry.clone() {
let mut map = HashMap::new();
map.insert(registry, table);
(
@ -1352,6 +1352,12 @@ pub fn save_credentials(cfg: &Config, token: String, registry: Option<String>) -
.insert("registry".into(), map.into());
}
if let Some(_) = registry {
if let Some(table) = toml.as_table_mut().unwrap().remove("registries") {
let v = CV::from_toml(file.path(), table)?;
value.merge(v)?;
}
}
toml.as_table_mut().unwrap().insert(key, value.into_toml());
let contents = toml.to_string();

View file

@ -12,6 +12,7 @@ use cargo_test_support::{cargo_process, t};
use toml;
const TOKEN: &str = "test-token";
const TOKEN2: &str = "test-token2";
const ORIGINAL_TOKEN: &str = "api-token";
fn setup_new_credentials() {
@ -185,4 +186,18 @@ fn registry_credentials() {
// Also ensure that we get the new token for the registry
assert!(check_token(TOKEN, Some(reg)));
let reg2 = "alternative2";
cargo_process("login --registry")
.arg(reg2)
.arg(TOKEN2)
.arg("-Zunstable-options")
.masquerade_as_nightly_cargo()
.run();
// Ensure not overwriting 1st alternate registry token with
// 2nd alternate registry token (see rust-lang/cargo#7701).
assert!(check_token(ORIGINAL_TOKEN, None));
assert!(check_token(TOKEN, Some(reg)));
assert!(check_token(TOKEN2, Some(reg2)));
}