test: add test for missing error paths in load_client_config (#19)
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@googleworkspace/cli": patch
|
||||
---
|
||||
|
||||
Add test for missing error path in load_client_config
|
||||
@@ -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")
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user