@@ -27,14 +27,15 @@ fn setup_new_credentials_at(config: PathBuf) {
27
27
) ) ;
28
28
}
29
29
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 > ) {
31
32
let credentials = credentials_toml ( ) ;
32
33
assert ! ( credentials. is_file( ) ) ;
33
34
34
35
let contents = fs:: read_to_string ( & credentials) . unwrap ( ) ;
35
36
let toml: toml:: Table = contents. parse ( ) . unwrap ( ) ;
36
37
37
- let token = match registry {
38
+ let actual_token = match registry {
38
39
// A registry has been provided, so check that the token exists in a
39
40
// table for the registry.
40
41
Some ( registry) => toml
@@ -54,10 +55,15 @@ fn check_token(expected_token: &str, registry: Option<&str>) -> bool {
54
55
} ) ,
55
56
} ;
56
57
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
+ }
61
67
}
62
68
}
63
69
@@ -75,10 +81,10 @@ fn registry_credentials() {
75
81
cargo_process ( "login --registry" ) . arg ( reg) . arg ( TOKEN ) . run ( ) ;
76
82
77
83
// Ensure that we have not updated the default token
78
- assert ! ( check_token( ORIGINAL_TOKEN , None ) ) ;
84
+ check_token ( Some ( ORIGINAL_TOKEN ) , None ) ;
79
85
80
86
// 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) ) ;
82
88
83
89
let reg2 = "alternative2" ;
84
90
cargo_process ( "login --registry" )
@@ -88,9 +94,9 @@ fn registry_credentials() {
88
94
89
95
// Ensure not overwriting 1st alternate registry token with
90
96
// 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) ) ;
94
100
}
95
101
96
102
#[ cargo_test]
0 commit comments