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 2 commits
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
34 changes: 33 additions & 1 deletion crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ pub fn alt_api_url() -> Url {
Url::from_file_path(alt_api_path()).ok().unwrap()
}

fn generate_path(name: &str) -> PathBuf {
paths::root().join(name)
}
fn generate_url(name: &str) -> Url {
Url::from_file_path(generate_path(name)).ok().unwrap()
}
fn generate_dl_url(name: &str) -> String {
let base = Url::from_file_path(generate_path(name)).ok().unwrap();
format!("{}/{{crate}}/{{version}}/{{crate}}-{{version}}.crate", base)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can all the other functions (the alt_* functions) be changed to call these new functions instead of duplicating the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I fixed it like that. I also corrected registry_path function etc., but is this OK?


/// A builder for creating a new package in a registry.
///
/// This uses "source replacement" using an automatically generated
Expand Down Expand Up @@ -166,9 +177,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 = generate_url("alternative2-registry")
)
.as_bytes()
));
Expand Down Expand Up @@ -214,6 +229,23 @@ pub fn init() {
fs::create_dir_all(alt_api_path().join("api/v1/crates")).unwrap();
}

pub fn init_alt2_registry() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be written so that the other two registries reuse the same code instead of duplicating it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I replaced this with a new function init_registry and the existing codes for creating registry call the new function. I am a little worried that there are many arguments for this new function, but I couldn't come up with another good way to write it.

// Initialize an alternative2 registry.
repo(&generate_path("alternative2-registry"))
.file(
"config.json",
&format!(
r#"
{{"dl":"{}","api":"{}"}}
"#,
generate_dl_url("alt2_dl"),
generate_url("alt2_api")
),
)
.build();
fs::create_dir_all(generate_path("alt2_api").join("api/v1/crates")).unwrap();
}

impl Package {
/// Creates a new package builder.
/// Call `publish()` to finalize and build the 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
16 changes: 16 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 @@ -169,6 +170,7 @@ fn new_credentials_is_used_instead_old() {
#[cargo_test]
fn registry_credentials() {
registry::init();
registry::init_alt2_registry();
setup_new_credentials();

let reg = "alternative";
Expand All @@ -185,4 +187,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)));
}