From 520d0a326912d1a4016bfe5af5cc29b669c2e7b7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 3 Mar 2026 22:56:33 +0000 Subject: [PATCH 1/5] Add test for load_client_config error paths Co-authored-by: jpoehnelt <3392975+jpoehnelt@users.noreply.github.com> --- .changeset/testing-improvement.md | 5 +++ src/auth_commands.rs | 5 +++ src/oauth_config.rs | 53 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 .changeset/testing-improvement.md diff --git a/.changeset/testing-improvement.md b/.changeset/testing-improvement.md new file mode 100644 index 0000000..a76bae7 --- /dev/null +++ b/.changeset/testing-improvement.md @@ -0,0 +1,5 @@ +--- +"@googleworkspace/cli": patch +--- + +Add test for missing error path in load_client_config diff --git a/src/auth_commands.rs b/src/auth_commands.rs index e50f90a..d7278e3 100644 --- a/src/auth_commands.rs +++ b/src/auth_commands.rs @@ -44,6 +44,11 @@ const READONLY_SCOPES: &[&str] = &[ ]; pub fn config_dir() -> PathBuf { + #[cfg(test)] + if let Ok(dir) = std::env::var("GOOGLE_WORKSPACE_CLI_CONFIG_DIR") { + return PathBuf::from(dir); + } + dirs::config_dir() .unwrap_or_else(|| PathBuf::from(".")) .join("gws") diff --git a/src/oauth_config.rs b/src/oauth_config.rs index b6a510b..929ed87 100644 --- a/src/oauth_config.rs +++ b/src/oauth_config.rs @@ -199,4 +199,57 @@ mod tests { let result = serde_json::from_str::(json); assert!(result.is_err()); } + + // Helper to manage the env var safely and clean up automatically + struct EnvGuard { + key: String, + } + + impl EnvGuard { + fn new(key: &str, value: &str) -> Self { + std::env::set_var(key, value); + Self { key: key.to_string() } + } + } + + impl Drop for EnvGuard { + fn drop(&mut self) { + std::env::remove_var(&self.key); + } + } + + #[test] + #[serial_test::serial] + fn test_load_client_config() { + let dir = tempfile::tempdir().unwrap(); + let _env_guard = EnvGuard::new( + "GOOGLE_WORKSPACE_CLI_CONFIG_DIR", + dir.path().to_str().unwrap() + ); + + // Initially no config file exists + let result = load_client_config(); + assert!(result.is_err()); + assert!(result.unwrap_err().to_string().contains("Cannot read")); + + // Create a valid config file + save_client_config("test-id", "test-secret", "test-project").unwrap(); + + // Now loading should succeed + let config = load_client_config().unwrap(); + assert_eq!(config.client_id, "test-id"); + assert_eq!(config.client_secret, "test-secret"); + assert_eq!(config.project_id, "test-project"); + + // Create an invalid config file + let path = client_config_path(); + std::fs::write(&path, "invalid json").unwrap(); + + let result = load_client_config(); + assert!(result.is_err()); + assert!(result + .unwrap_err() + .to_string() + .contains("Invalid client_secret.json format")); + } } From 705747c3b6b0535ad753c72f80b2117a6069cb31 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 3 Mar 2026 23:01:00 +0000 Subject: [PATCH 2/5] Add test for load_client_config error paths Co-authored-by: jpoehnelt <3392975+jpoehnelt@users.noreply.github.com> --- src/oauth_config.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/oauth_config.rs b/src/oauth_config.rs index 929ed87..0a594a9 100644 --- a/src/oauth_config.rs +++ b/src/oauth_config.rs @@ -208,7 +208,9 @@ mod tests { impl EnvGuard { fn new(key: &str, value: &str) -> Self { std::env::set_var(key, value); - Self { key: key.to_string() } + Self { + key: key.to_string(), + } } } @@ -224,7 +226,7 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let _env_guard = EnvGuard::new( "GOOGLE_WORKSPACE_CLI_CONFIG_DIR", - dir.path().to_str().unwrap() + dir.path().to_str().unwrap(), ); // Initially no config file exists From b2467a1bae38028a80d33836f91459bad5347bea Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 3 Mar 2026 23:06:29 +0000 Subject: [PATCH 3/5] Address PR feedback on EnvGuard Co-authored-by: jpoehnelt <3392975+jpoehnelt@users.noreply.github.com> --- src/oauth_config.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/oauth_config.rs b/src/oauth_config.rs index 0a594a9..af29f91 100644 --- a/src/oauth_config.rs +++ b/src/oauth_config.rs @@ -203,20 +203,27 @@ mod tests { // Helper to manage the env var safely and clean up automatically struct EnvGuard { key: String, + original_value: Option, } impl EnvGuard { fn new(key: &str, value: &str) -> Self { + let original_value = std::env::var(key).ok(); std::env::set_var(key, value); Self { key: key.to_string(), + original_value, } } } impl Drop for EnvGuard { fn drop(&mut self) { - std::env::remove_var(&self.key); + if let Some(val) = &self.original_value { + std::env::set_var(&self.key, val); + } else { + std::env::remove_var(&self.key); + } } } From 994133a67db8681e43be14a577d3a92ef8acb7e1 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 00:08:47 +0000 Subject: [PATCH 4/5] Simplify load_client_config tests Co-authored-by: jpoehnelt <3392975+jpoehnelt@users.noreply.github.com> --- src/oauth_config.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/oauth_config.rs b/src/oauth_config.rs index af29f91..cb08697 100644 --- a/src/oauth_config.rs +++ b/src/oauth_config.rs @@ -238,8 +238,8 @@ mod tests { // Initially no config file exists let result = load_client_config(); - assert!(result.is_err()); - assert!(result.unwrap_err().to_string().contains("Cannot read")); + let err = result.unwrap_err(); + assert!(err.to_string().contains("Cannot read")); // Create a valid config file save_client_config("test-id", "test-secret", "test-project").unwrap(); @@ -255,10 +255,10 @@ mod tests { std::fs::write(&path, "invalid json").unwrap(); let result = load_client_config(); - assert!(result.is_err()); - assert!(result - .unwrap_err() - .to_string() - .contains("Invalid client_secret.json format")); + let err = result.unwrap_err(); + assert!( + err.to_string() + .contains("Invalid client_secret.json format") + ); } } From ceb8a477a8afe5abf0cdbe2617de9cd1dc2223ba Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 4 Mar 2026 00:11:51 +0000 Subject: [PATCH 5/5] Run cargo fmt Co-authored-by: jpoehnelt <3392975+jpoehnelt@users.noreply.github.com> --- src/oauth_config.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/oauth_config.rs b/src/oauth_config.rs index cb08697..210725f 100644 --- a/src/oauth_config.rs +++ b/src/oauth_config.rs @@ -256,9 +256,8 @@ mod tests { let result = load_client_config(); let err = result.unwrap_err(); - assert!( - err.to_string() - .contains("Invalid client_secret.json format") - ); + assert!(err + .to_string() + .contains("Invalid client_secret.json format")); } }