@@ -201,8 +201,15 @@ mod tests {
201201 use super :: * ;
202202 use serde:: { Deserialize , Serialize } ;
203203 use std:: fs;
204+ use std:: sync:: { Mutex , OnceLock } ;
204205 use tempfile:: TempDir ;
205206
207+ /// Mutex to serialize tests that modify environment variables or current directory.
208+ fn env_lock ( ) -> & ' static Mutex < ( ) > {
209+ static LOCK : OnceLock < Mutex < ( ) > > = OnceLock :: new ( ) ;
210+ LOCK . get_or_init ( || Mutex :: new ( ( ) ) )
211+ }
212+
206213 #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
207214 struct ApplicationConfig {
208215 /// Application name.
@@ -233,6 +240,8 @@ mod tests {
233240 }
234241
235242 fn test_roundtrip_with_extension ( extension : & str ) {
243+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
244+
236245 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
237246 let config_dir = temp_dir. path ( ) . join ( "configuration" ) ;
238247 fs:: create_dir ( & config_dir) . unwrap ( ) ;
@@ -274,6 +283,7 @@ mod tests {
274283
275284 // Set environment and working directory
276285 unsafe {
286+ std:: env:: remove_var ( "APP_CONFIG_DIR" ) ;
277287 std:: env:: set_var ( "APP_ENVIRONMENT" , "prod" ) ;
278288 }
279289 std:: env:: set_current_dir ( temp_dir. path ( ) ) . unwrap ( ) ;
@@ -322,6 +332,8 @@ mod tests {
322332
323333 #[ test]
324334 fn test_app_config_dir_env_var ( ) {
335+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
336+
325337 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
326338 // Note: NOT using "configuration" subdirectory, using a custom path
327339 let custom_config_dir = temp_dir. path ( ) . join ( "my-custom-config" ) ;
@@ -357,6 +369,8 @@ mod tests {
357369
358370 #[ test]
359371 fn test_fallback_to_current_dir_when_app_config_dir_not_set ( ) {
372+ let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
373+
360374 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
361375 let config_dir = temp_dir. path ( ) . join ( "configuration" ) ;
362376 fs:: create_dir ( & config_dir) . unwrap ( ) ;
0 commit comments