@@ -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