From 98f8d863d54d3c17579033df33bbcf91510c587d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Thu, 24 Nov 2022 17:53:57 +0100 Subject: [PATCH] Add proper failure when login fails --- lib/cli/src/commands/login.rs | 9 +++++++-- lib/registry/src/login.rs | 23 ++++++++++------------- tests/integration/cli/tests/login.rs | 23 ++++++++++++++++++----- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/lib/cli/src/commands/login.rs b/lib/cli/src/commands/login.rs index 539f132b147..0846b3b8639 100644 --- a/lib/cli/src/commands/login.rs +++ b/lib/cli/src/commands/login.rs @@ -51,8 +51,11 @@ impl Login { /// execute [List] pub fn execute(&self) -> Result<(), anyhow::Error> { let token = self.get_token_or_ask_user()?; - wasmer_registry::login::login_and_save_token(&self.registry, &token) - .map_err(|e| anyhow::anyhow!("{e}")) + match wasmer_registry::login::login_and_save_token(&self.registry, &token)? { + Some(s) => println!("Login for WAPM user {:?} saved", s), + None => println!("Login for WAPM user saved"), + } + Ok(()) } } @@ -61,6 +64,7 @@ fn test_login_2() { let login = Login { registry: "wapm.dev".to_string(), token: None, + debug: false, }; assert_eq!( @@ -71,6 +75,7 @@ fn test_login_2() { let login = Login { registry: "wapm.dev".to_string(), token: Some("abc".to_string()), + debug: false, }; assert_eq!(login.get_token_or_ask_user().unwrap(), "abc"); diff --git a/lib/registry/src/login.rs b/lib/registry/src/login.rs index 53bd8628665..f5df8d82c69 100644 --- a/lib/registry/src/login.rs +++ b/lib/registry/src/login.rs @@ -8,12 +8,14 @@ pub fn login_and_save_token( #[cfg(test)] test_name: &str, registry: &str, token: &str, -) -> Result<(), anyhow::Error> { +) -> Result, anyhow::Error> { let registry = format_graphql(registry); #[cfg(test)] - let mut config = PartialWapmConfig::from_file(test_name).map_err(|e| anyhow::anyhow!("{e}"))?; + let mut config = PartialWapmConfig::from_file(test_name) + .map_err(|e| anyhow::anyhow!("config from file: {e}"))?; #[cfg(not(test))] - let mut config = PartialWapmConfig::from_file().map_err(|e| anyhow::anyhow!("{e}"))?; + let mut config = + PartialWapmConfig::from_file().map_err(|e| anyhow::anyhow!("config from file: {e}"))?; config.registry.set_current_registry(®istry); config.registry.set_login_token_for_registry( &config.registry.get_current_registry(), @@ -21,16 +23,11 @@ pub fn login_and_save_token( UpdateRegistry::Update, ); #[cfg(test)] - let path = - PartialWapmConfig::get_file_location(test_name).map_err(|e| anyhow::anyhow!("{e}"))?; + let path = PartialWapmConfig::get_file_location(test_name) + .map_err(|e| anyhow::anyhow!("get file location: {e}"))?; #[cfg(not(test))] - let path = PartialWapmConfig::get_file_location().map_err(|e| anyhow::anyhow!("{e}"))?; + let path = PartialWapmConfig::get_file_location() + .map_err(|e| anyhow::anyhow!("get file location: {e}"))?; config.save(&path)?; - let username = crate::utils::get_username_registry_token(®istry, token); - if let Some(s) = username.ok().and_then(|o| o) { - println!("Login for WAPM user {:?} saved", s); - } else { - println!("Login for WAPM user saved"); - } - Ok(()) + crate::utils::get_username_registry_token(®istry, token) } diff --git a/tests/integration/cli/tests/login.rs b/tests/integration/cli/tests/login.rs index 554ac8f5c59..fdb1bea19dc 100644 --- a/tests/integration/cli/tests/login.rs +++ b/tests/integration/cli/tests/login.rs @@ -18,18 +18,31 @@ fn login_works() -> anyhow::Result<()> { .arg(wapm_dev_token) .output()?; + let stdout = std::str::from_utf8(&output.stdout) + .expect("stdout is not utf8! need to handle arbitrary bytes"); + + let stderr = std::str::from_utf8(&output.stderr) + .expect("stderr is not utf8! need to handle arbitrary bytes"); + if !output.status.success() { bail!( "wasmer login failed with: stdout: {}\n\nstderr: {}", - std::str::from_utf8(&output.stdout) - .expect("stdout is not utf8! need to handle arbitrary bytes"), - std::str::from_utf8(&output.stderr) - .expect("stderr is not utf8! need to handle arbitrary bytes") + stdout, + stderr ); } let stdout_output = std::str::from_utf8(&output.stdout).unwrap(); - assert_eq!(stdout_output, "Login for WAPM user \"ciuser\" saved\n"); + let expected = "Login for WAPM user \"ciuser\" saved\n"; + if stdout_output != expected { + println!("expected:"); + println!("{expected}"); + println!("got:"); + println!("{stdout}"); + println!("-----"); + println!("{stderr}"); + panic!("stdout incorrect"); + } Ok(()) }