Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overwriting alternate registry token #7708

Merged
merged 6 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -52,13 +58,26 @@ 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")
}
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.
///
Expand Down Expand Up @@ -166,9 +185,13 @@ pub fn init() {

[registries.alternative]
index = '{alt}'

[registries.alternative2]
index = '{alt2}'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best to not set this here, and only set it in the registry_credentials test. Can probably just append to the config file.

"#,
reg = registry_url(),
alt = alt_registry_url()
alt = alt_registry_url(),
alt2 = alt2_registry_url()
)
.as_bytes()
));
Expand All @@ -180,6 +203,9 @@ pub fn init() {

[registries.alternative]
token = "api-token"

[registries.alternative2]
token = "api-token"
"#
));

Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/cargo/util/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
(
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions tests/testsuite/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines can be removed, I think we just forgot to remove them when stabilized. And remove the same 2 lines in the call above.

.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)));
}