Skip to content

Commit 91d39bc

Browse files
committed
Share the check_token function between login and logout tests.
1 parent 1b2de21 commit 91d39bc

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

tests/testsuite/login.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ fn setup_new_credentials_at(config: PathBuf) {
2727
));
2828
}
2929

30-
fn check_token(expected_token: &str, registry: Option<&str>) -> bool {
30+
/// Asserts whether or not the token is set to the given value for the given registry.
31+
pub fn check_token(expected_token: Option<&str>, registry: Option<&str>) {
3132
let credentials = credentials_toml();
3233
assert!(credentials.is_file());
3334

3435
let contents = fs::read_to_string(&credentials).unwrap();
3536
let toml: toml::Table = contents.parse().unwrap();
3637

37-
let token = match registry {
38+
let actual_token = match registry {
3839
// A registry has been provided, so check that the token exists in a
3940
// table for the registry.
4041
Some(registry) => toml
@@ -54,10 +55,15 @@ fn check_token(expected_token: &str, registry: Option<&str>) -> bool {
5455
}),
5556
};
5657

57-
if let Some(token_val) = token {
58-
token_val == expected_token
59-
} else {
60-
false
58+
match (actual_token, expected_token) {
59+
(None, None) => {}
60+
(Some(actual), Some(expected)) => assert_eq!(actual, expected),
61+
(None, Some(expected)) => {
62+
panic!("expected `{registry:?}` to be `{expected}`, but was not set")
63+
}
64+
(Some(actual), None) => {
65+
panic!("expected `{registry:?}` to be unset, but was set to `{actual}`")
66+
}
6167
}
6268
}
6369

@@ -75,10 +81,10 @@ fn registry_credentials() {
7581
cargo_process("login --registry").arg(reg).arg(TOKEN).run();
7682

7783
// Ensure that we have not updated the default token
78-
assert!(check_token(ORIGINAL_TOKEN, None));
84+
check_token(Some(ORIGINAL_TOKEN), None);
7985

8086
// Also ensure that we get the new token for the registry
81-
assert!(check_token(TOKEN, Some(reg)));
87+
check_token(Some(TOKEN), Some(reg));
8288

8389
let reg2 = "alternative2";
8490
cargo_process("login --registry")
@@ -88,9 +94,9 @@ fn registry_credentials() {
8894

8995
// Ensure not overwriting 1st alternate registry token with
9096
// 2nd alternate registry token (see rust-lang/cargo#7701).
91-
assert!(check_token(ORIGINAL_TOKEN, None));
92-
assert!(check_token(TOKEN, Some(reg)));
93-
assert!(check_token(TOKEN2, Some(reg2)));
97+
check_token(Some(ORIGINAL_TOKEN), None);
98+
check_token(Some(TOKEN), Some(reg));
99+
check_token(Some(TOKEN2), Some(reg2));
94100
}
95101

96102
#[cargo_test]

tests/testsuite/logout.rs

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! Tests for the `cargo logout` command.
22
3-
use cargo_test_support::install::cargo_home;
3+
use super::login::check_token;
44
use cargo_test_support::registry::TestRegistry;
55
use cargo_test_support::{cargo_process, registry};
6-
use std::fs;
76

87
#[cargo_test]
98
fn gated() {
@@ -21,32 +20,9 @@ the `cargo logout` command.
2120
.run();
2221
}
2322

24-
/// Checks whether or not the token is set for the given token.
25-
fn check_config_token(registry: Option<&str>, should_be_set: bool) {
26-
let credentials = cargo_home().join("credentials.toml");
27-
let contents = fs::read_to_string(&credentials).unwrap();
28-
let toml: toml::Table = contents.parse().unwrap();
29-
if let Some(registry) = registry {
30-
assert_eq!(
31-
toml.get("registries")
32-
.and_then(|registries| registries.get(registry))
33-
.and_then(|registry| registry.get("token"))
34-
.is_some(),
35-
should_be_set
36-
);
37-
} else {
38-
assert_eq!(
39-
toml.get("registry")
40-
.and_then(|registry| registry.get("token"))
41-
.is_some(),
42-
should_be_set
43-
);
44-
}
45-
}
46-
4723
fn simple_logout_test(registry: &TestRegistry, reg: Option<&str>, flag: &str, note: &str) {
4824
let msg = reg.unwrap_or("crates-io");
49-
check_config_token(reg, true);
25+
check_token(Some(registry.token()), reg);
5026
let mut cargo = cargo_process(&format!("logout -Z unstable-options {}", flag));
5127
if reg.is_none() {
5228
cargo.replace_crates_io(registry.index_url());
@@ -61,7 +37,7 @@ If you need to revoke the token, visit {note} and follow the instructions there.
6137
"
6238
))
6339
.run();
64-
check_config_token(reg, false);
40+
check_token(None, reg);
6541

6642
let mut cargo = cargo_process(&format!("logout -Z unstable-options {}", flag));
6743
if reg.is_none() {
@@ -71,7 +47,7 @@ If you need to revoke the token, visit {note} and follow the instructions there.
7147
.masquerade_as_nightly_cargo(&["cargo-logout"])
7248
.with_stderr(&format!("[LOGOUT] not currently logged in to `{msg}`"))
7349
.run();
74-
check_config_token(reg, false);
50+
check_token(None, reg);
7551
}
7652

7753
#[cargo_test]
@@ -89,4 +65,6 @@ fn other_registry() {
8965
"--registry alternative",
9066
"the `alternative` website",
9167
);
68+
// It should not touch crates.io.
69+
check_token(Some("sekrit"), None);
9270
}

0 commit comments

Comments
 (0)