]>
Commit | Line | Data |
---|---|---|
cc6df1d7 EH |
1 | //! Tests for the `cargo logout` command. |
2 | ||
3 | use cargo_test_support::install::cargo_home; | |
4 | use cargo_test_support::{cargo_process, registry}; | |
5 | use std::fs; | |
6 | ||
7 | #[cargo_test] | |
8 | fn gated() { | |
9 | registry::init(); | |
10 | cargo_process("logout") | |
11 | .masquerade_as_nightly_cargo() | |
12 | .with_status(101) | |
13 | .with_stderr( | |
14 | "\ | |
15 | [ERROR] the `cargo logout` command is unstable, pass `-Z unstable-options` to enable it | |
16 | See https://github.com/rust-lang/cargo/issues/8933 for more information about \ | |
17 | the `cargo logout` command. | |
18 | ", | |
19 | ) | |
20 | .run(); | |
21 | } | |
22 | ||
23 | /// Checks whether or not the token is set for the given token. | |
24 | fn check_config_token(registry: Option<&str>, should_be_set: bool) { | |
25 | let credentials = cargo_home().join("credentials"); | |
26 | let contents = fs::read_to_string(&credentials).unwrap(); | |
27 | let toml: toml::Value = contents.parse().unwrap(); | |
28 | if let Some(registry) = registry { | |
29 | assert_eq!( | |
30 | toml.get("registries") | |
31 | .and_then(|registries| registries.get(registry)) | |
32 | .and_then(|registry| registry.get("token")) | |
33 | .is_some(), | |
34 | should_be_set | |
35 | ); | |
36 | } else { | |
37 | assert_eq!( | |
38 | toml.get("registry") | |
39 | .and_then(|registry| registry.get("token")) | |
40 | .is_some(), | |
41 | should_be_set | |
42 | ); | |
43 | } | |
44 | } | |
45 | ||
46 | fn simple_logout_test(reg: Option<&str>, flag: &str) { | |
47 | registry::init(); | |
48 | let msg = reg.unwrap_or("crates.io"); | |
49 | check_config_token(reg, true); | |
50 | cargo_process(&format!("logout -Z unstable-options {}", flag)) | |
51 | .masquerade_as_nightly_cargo() | |
52 | .with_stderr(&format!( | |
53 | "\ | |
54 | [UPDATING] [..] | |
55 | [LOGOUT] token for `{}` has been removed from local storage | |
56 | ", | |
57 | msg | |
58 | )) | |
59 | .run(); | |
60 | check_config_token(reg, false); | |
61 | ||
62 | cargo_process(&format!("logout -Z unstable-options {}", flag)) | |
63 | .masquerade_as_nightly_cargo() | |
64 | .with_stderr(&format!( | |
65 | "\ | |
66 | [LOGOUT] not currently logged in to `{}` | |
67 | ", | |
68 | msg | |
69 | )) | |
70 | .run(); | |
71 | check_config_token(reg, false); | |
72 | } | |
73 | ||
74 | #[cargo_test] | |
75 | fn default_registry() { | |
76 | simple_logout_test(None, ""); | |
77 | } | |
78 | ||
79 | #[cargo_test] | |
80 | fn other_registry() { | |
340656e2 | 81 | registry::alt_init(); |
cc6df1d7 EH |
82 | simple_logout_test(Some("alternative"), "--registry alternative"); |
83 | } |