Skip to content

Commit 7664357

Browse files
authored
test: add test for missing error paths in load_client_config (#19)
1 parent e71ce29 commit 7664357

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

.changeset/testing-improvement.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@googleworkspace/cli": patch
3+
---
4+
5+
Add test for missing error path in load_client_config

src/auth_commands.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ const READONLY_SCOPES: &[&str] = &[
4444
];
4545

4646
pub fn config_dir() -> PathBuf {
47+
#[cfg(test)]
48+
if let Ok(dir) = std::env::var("GOOGLE_WORKSPACE_CLI_CONFIG_DIR") {
49+
return PathBuf::from(dir);
50+
}
51+
4752
dirs::config_dir()
4853
.unwrap_or_else(|| PathBuf::from("."))
4954
.join("gws")

src/oauth_config.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,65 @@ mod tests {
199199
let result = serde_json::from_str::<ClientSecretFile>(json);
200200
assert!(result.is_err());
201201
}
202+
203+
// Helper to manage the env var safely and clean up automatically
204+
struct EnvGuard {
205+
key: String,
206+
original_value: Option<String>,
207+
}
208+
209+
impl EnvGuard {
210+
fn new(key: &str, value: &str) -> Self {
211+
let original_value = std::env::var(key).ok();
212+
std::env::set_var(key, value);
213+
Self {
214+
key: key.to_string(),
215+
original_value,
216+
}
217+
}
218+
}
219+
220+
impl Drop for EnvGuard {
221+
fn drop(&mut self) {
222+
if let Some(val) = &self.original_value {
223+
std::env::set_var(&self.key, val);
224+
} else {
225+
std::env::remove_var(&self.key);
226+
}
227+
}
228+
}
229+
230+
#[test]
231+
#[serial_test::serial]
232+
fn test_load_client_config() {
233+
let dir = tempfile::tempdir().unwrap();
234+
let _env_guard = EnvGuard::new(
235+
"GOOGLE_WORKSPACE_CLI_CONFIG_DIR",
236+
dir.path().to_str().unwrap(),
237+
);
238+
239+
// Initially no config file exists
240+
let result = load_client_config();
241+
let err = result.unwrap_err();
242+
assert!(err.to_string().contains("Cannot read"));
243+
244+
// Create a valid config file
245+
save_client_config("test-id", "test-secret", "test-project").unwrap();
246+
247+
// Now loading should succeed
248+
let config = load_client_config().unwrap();
249+
assert_eq!(config.client_id, "test-id");
250+
assert_eq!(config.client_secret, "test-secret");
251+
assert_eq!(config.project_id, "test-project");
252+
253+
// Create an invalid config file
254+
let path = client_config_path();
255+
std::fs::write(&path, "invalid json").unwrap();
256+
257+
let result = load_client_config();
258+
let err = result.unwrap_err();
259+
assert!(err
260+
.to_string()
261+
.contains("Invalid client_secret.json format"));
262+
}
202263
}

0 commit comments

Comments
 (0)