/// Sets environment variables to the given value for the duration of the closure.
/// Restores the previous values when the closure completes or panics, before unwinding the
/// panic.
Sets environment variables to the given value for the duration of the closure.
Restores the previous values when the closure completes or panics, before unwinding the
panic.
Sets environment variables to the given value for the duration of the closure . Restores the previous values when the closure completes or panics before unwinding the panic .
pub fn with_env_vars<F>(label: &str, kvs: Vec<(&str, Option<&str>)>, closure: F)
    where
        F: Fn() + UnwindSafe + RefUnwindSafe,
    {
        let guard = SERIAL_TEST.lock().unwrap();
        let mut old_kvs: Vec<(&str, Result<String, VarError>)> = Vec::new();
        for (k, v) in kvs {
            let old_v = env::var(k);
            old_kvs.push((k, old_v));
            match v {
                None => env::remove_var(k),
                Some(v) => env::set_var(k, v),
            }
        }
        eprintln!("W_ENV[{}]: OLD_KVS: {:?}", label, old_kvs);
        let old_kvs_2 = old_kvs.clone();
        match panic::catch_unwind(|| {
            closure();
        }) {
            Ok(_) => {
                eprintln!("W_END[{}]: OK - resetting env to: {:?}", label, old_kvs);
                for (k, v) in old_kvs {
                    reset_env(k, v);
                }
            },
            Err(err) => {
                eprintln!("W_END[{}]: Err - resetting env to: {:?}", label, old_kvs);
                for (k, v) in old_kvs {
                    reset_env(k, v);
                }
                drop(guard);
                panic::resume_unwind(err);
            },
        };
        for (k, v) in old_kvs_2 {
            eprintln!(
                "W_END[{}] RESET ACTUAL: {:?}:{:?} expected:{:?}",
                label,
                k,
                env::var(k),
                v
            );
        }
    }
pub fn with_env_vars < F > ( label : & str , kvs : Vec < ( & str , Option < & str > ) > , closure : F ) where F : Fn ( ) + UnwindSafe + RefUnwindSafe , { let guard = SERIAL_TEST . lock ( ) . unwrap ( ) ; let mut old_kvs : Vec < ( & str , Result < String , VarError > ) > = Vec :: new ( ) ; for ( k , v ) in kvs { let old_v = env :: var ( k ) ; old_kvs . push ( ( k , old_v ) ) ; match v { None => env :: remove_var ( k ) , Some ( v ) => env :: set_var ( k , v ) , } } eprintln ! ( " " label old_kvs ) ; let old_kvs_2 = old_kvs . clone ( ) ; match panic :: catch_unwind ( | | { closure ( ) ; } ) { Ok ( _ ) => { eprintln ! ( " " label old_kvs ) ; for ( k , v ) in old_kvs { reset_env ( k , v ) ; } } , Err ( err ) => { eprintln ! ( " " label old_kvs ) ; for ( k , v ) in old_kvs { reset_env ( k , v ) ; } drop ( guard ) ; panic :: resume_unwind ( err ) ; } , } ; for ( k , v ) in old_kvs_2 { eprintln ! ( " " label k env var ( k ) v ) ; } }
Sets environment variables to the given value for the duration of the closure.
Sets environment variables to the given value for the duration of the closure .
label
&str
kvs
Vec<(&str, Option<&str>)>
closure
F