diff --git a/.changeset/testing-improvement.md b/.changeset/testing-improvement.md new file mode 100644 index 00000000..a76bae75 --- /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 e50f90a0..d7278e3b 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 b6a510be..210725f0 100644 --- a/src/oauth_config.rs +++ b/src/oauth_config.rs @@ -199,4 +199,65 @@ 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, + 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) { + if let Some(val) = &self.original_value { + std::env::set_var(&self.key, val); + } else { + 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(); + 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(); + + // 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(); + let err = result.unwrap_err(); + assert!(err + .to_string() + .contains("Invalid client_secret.json format")); + } }