diff --git a/Cargo.lock b/Cargo.lock index ed1b68e..6c730eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "acl-sys" @@ -134,6 +134,7 @@ dependencies = [ "anstyle", "clap", "clap_complete", + "env-lock", "log", "nix", "posix-acl", @@ -144,6 +145,12 @@ dependencies = [ "xcb", ] +[[package]] +name = "env-lock" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2afbd3cff67810192b5bdb76b77feff72775314d41195c1d779bc92e0e83285d" + [[package]] name = "is_terminal_polyfill" version = "1.70.2" diff --git a/Cargo.toml b/Cargo.toml index 8636f82..35c1099 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,5 +30,6 @@ default = [] [dev-dependencies] clap_complete = "~4.5.54" +env-lock = "1.0.1" snapbox = "0.6.21" testing_logger = "0.1.1" diff --git a/src/tests.rs b/src/tests.rs index 4e6854a..5d8ddb3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,3 @@ -use std::env; use std::fmt::Write; use std::path::PathBuf; use std::sync::OnceLock; @@ -80,28 +79,30 @@ fn test_context() -> EgoContext { } } -/// SAFETY: unsafe changes to environment variables #[test] fn wayland_socket() { let ctx = test_context(); - unsafe { env::remove_var("WAYLAND_DISPLAY") }; + + let env = env_lock::lock_env([("WAYLAND_DISPLAY", None::<&str>)]); assert_eq!(get_wayland_socket(&ctx).unwrap(), None); + drop(env); - unsafe { env::set_var("WAYLAND_DISPLAY", "wayland-7") }; + let env = env_lock::lock_env([("WAYLAND_DISPLAY", Some("wayland-7"))]); assert_eq!( get_wayland_socket(&ctx).unwrap().unwrap(), PathBuf::from("/run/user/1000/wayland-7") ); + drop(env); - unsafe { env::set_var("WAYLAND_DISPLAY", "/tmp/wayland-7") }; + let env = env_lock::lock_env([("WAYLAND_DISPLAY", Some("/tmp/wayland-7"))]); assert_eq!(get_wayland_socket(&ctx).unwrap().unwrap(), PathBuf::from("/tmp/wayland-7")); + drop(env); } -/// SAFETY: unsafe changes to environment variables #[test] #[cfg_attr(not(target_os = "linux"), ignore = "Linux-specifix")] -fn test_x11_error() { - unsafe { env::remove_var("DISPLAY") }; +fn test_a_x11_error() { + env_lock::lock_env([("DISPLAY", None::<&str>)]); let err = x11_xcb_add_acl("test", "test").unwrap_err(); assert_eq!(