Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/testing-improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@googleworkspace/cli": patch
---

Add test for missing error path in load_client_config
5 changes: 5 additions & 0 deletions src/auth_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
61 changes: 61 additions & 0 deletions src/oauth_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,65 @@ mod tests {
let result = serde_json::from_str::<ClientSecretFile>(json);
assert!(result.is_err());
}

// Helper to manage the env var safely and clean up automatically
struct EnvGuard {
key: String,
original_value: Option<String>,
}

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"));
}
}
Loading