Skip to content

Commit

Permalink
Auto merge of #11145 - hi-rustin:rustin-patch-login, r=weihanglo
Browse files Browse the repository at this point in the history
Check empty input for login

### What does this PR try to resolve?

close #11144
Check empty input for login.

### How should we test and review this PR?

- unit test
- cargo login and enter
  • Loading branch information
bors committed Oct 7, 2022
2 parents d1a2592 + ff575b2 commit 882c5dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,10 @@ pub fn registry_login(
}
};

if token.is_empty() {
bail!("please provide a non-empty token");
}

if let RegistryConfig::Token(old_token) = &reg_cfg {
if old_token == &token {
config.shell().status("Login", "already logged in")?;
Expand Down
28 changes: 28 additions & 0 deletions tests/testsuite/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,31 @@ fn registry_credentials() {
assert!(check_token(TOKEN, Some(reg)));
assert!(check_token(TOKEN2, Some(reg2)));
}

#[cargo_test]
fn empty_login_token() {
let _registry = RegistryBuilder::new().build();
setup_new_credentials();

cargo_process("login")
.with_stdout("please paste the API Token found on [..]/me below")
.with_stdin("\t\n")
.with_stderr(
"\
[UPDATING] `dummy-registry` index
[ERROR] please provide a non-empty token
",
)
.with_status(101)
.run();

cargo_process("login")
.arg("")
.with_stderr(
"\
[ERROR] please provide a non-empty token
",
)
.with_status(101)
.run();
}

0 comments on commit 882c5dd

Please sign in to comment.