@@ -72,24 +72,61 @@ fn handle_config_edit() -> Result<()> {
7272#[ cfg( test) ]
7373mod tests {
7474 use super :: * ;
75+ use std:: sync:: { Mutex , OnceLock } ;
76+
77+ fn env_lock ( ) -> & ' static Mutex < ( ) > {
78+ static LOCK : OnceLock < Mutex < ( ) > > = OnceLock :: new ( ) ;
79+ LOCK . get_or_init ( || Mutex :: new ( ( ) ) )
80+ }
81+
82+ struct EnvRestore {
83+ editor : Option < String > ,
84+ visual : Option < String > ,
85+ }
86+
87+ impl EnvRestore {
88+ fn capture ( ) -> Self {
89+ Self {
90+ editor : std:: env:: var ( "EDITOR" ) . ok ( ) ,
91+ visual : std:: env:: var ( "VISUAL" ) . ok ( ) ,
92+ }
93+ }
94+ }
95+
96+ impl Drop for EnvRestore {
97+ fn drop ( & mut self ) {
98+ match & self . editor {
99+ Some ( v) => std:: env:: set_var ( "EDITOR" , v) ,
100+ None => std:: env:: remove_var ( "EDITOR" ) ,
101+ }
102+ match & self . visual {
103+ Some ( v) => std:: env:: set_var ( "VISUAL" , v) ,
104+ None => std:: env:: remove_var ( "VISUAL" ) ,
105+ }
106+ }
107+ }
75108
76109 #[ test]
77110 fn test_resolve_editor_from_env ( ) {
111+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
112+ let _restore = EnvRestore :: capture ( ) ;
78113 std:: env:: set_var ( "EDITOR" , "nano" ) ;
79114 assert_eq ! ( resolve_editor( ) , "nano" ) ;
80- std:: env:: remove_var ( "EDITOR" ) ;
81115 }
82116
83117 #[ test]
84118 fn test_resolve_editor_visual_fallback ( ) {
119+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
120+ let _restore = EnvRestore :: capture ( ) ;
85121 std:: env:: remove_var ( "EDITOR" ) ;
86122 std:: env:: set_var ( "VISUAL" , "code" ) ;
87123 assert_eq ! ( resolve_editor( ) , "code" ) ;
88- std:: env:: remove_var ( "VISUAL" ) ;
89124 }
90125
91126 #[ test]
92127 fn test_resolve_editor_default ( ) {
128+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
129+ let _restore = EnvRestore :: capture ( ) ;
93130 std:: env:: remove_var ( "EDITOR" ) ;
94131 std:: env:: remove_var ( "VISUAL" ) ;
95132 let editor = resolve_editor ( ) ;
@@ -102,7 +139,6 @@ mod tests {
102139
103140 #[ test]
104141 fn test_config_path_json_output ( ) {
105- // Verify the JSON mode produces valid JSON with expected fields
106142 let path = config:: config_path ( ) . unwrap ( ) ;
107143 let json = serde_json:: json!( {
108144 "path" : path. to_string_lossy( ) ,
0 commit comments