@@ -88,30 +88,21 @@ pub fn remove_edge_token(gateway_name: &str) -> Result<()> {
8888mod tests {
8989 use super :: * ;
9090
91- /// Helper: hold the shared XDG test lock, set `XDG_CONFIG_HOME` to a
92- /// tempdir, run `f`, then restore the original value.
93- #[ allow( unsafe_code) ]
94- fn with_tmp_xdg < F : FnOnce ( ) > ( tmp : & std:: path:: Path , f : F ) {
95- let _guard = crate :: XDG_TEST_LOCK
96- . lock ( )
97- . unwrap_or_else ( std:: sync:: PoisonError :: into_inner) ;
98- let orig = std:: env:: var ( "XDG_CONFIG_HOME" ) . ok ( ) ;
99- unsafe {
100- std:: env:: set_var ( "XDG_CONFIG_HOME" , tmp) ;
101- }
102- f ( ) ;
103- unsafe {
104- match orig {
105- Some ( v) => std:: env:: set_var ( "XDG_CONFIG_HOME" , v) ,
106- None => std:: env:: remove_var ( "XDG_CONFIG_HOME" ) ,
107- }
108- }
91+ /// Run the assertions in a child with an isolated configuration root.
92+ fn with_tmp_xdg ( tmp : & std:: path:: Path , f : impl FnOnce ( & std:: path:: Path ) ) {
93+ crate :: test_environment:: Environment :: new ( )
94+ . set ( "XDG_CONFIG_HOME" , tmp)
95+ . remove ( crate :: paths:: SYSTEM_GATEWAY_DIR_ENV )
96+ . run ( || {
97+ let root = PathBuf :: from ( std:: env:: var_os ( "XDG_CONFIG_HOME" ) . unwrap ( ) ) ;
98+ f ( & root) ;
99+ } ) ;
109100 }
110101
111102 #[ test]
112103 fn store_and_load_edge_token_roundtrip ( ) {
113104 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
114- with_tmp_xdg ( tmp. path ( ) , || {
105+ with_tmp_xdg ( tmp. path ( ) , |_ | {
115106 store_edge_token ( "test-gateway" , "eyJhbGciOiJSUzI1NiJ9.test.sig" ) . unwrap ( ) ;
116107 assert_eq ! (
117108 load_edge_token( "test-gateway" ) ,
@@ -123,15 +114,15 @@ mod tests {
123114 #[ test]
124115 fn load_edge_token_returns_none_when_not_set ( ) {
125116 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
126- with_tmp_xdg ( tmp. path ( ) , || {
117+ with_tmp_xdg ( tmp. path ( ) , |_ | {
127118 assert_eq ! ( load_edge_token( "no-such-gateway" ) , None ) ;
128119 } ) ;
129120 }
130121
131122 #[ test]
132123 fn store_edge_token_overwrites_previous ( ) {
133124 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
134- with_tmp_xdg ( tmp. path ( ) , || {
125+ with_tmp_xdg ( tmp. path ( ) , |_ | {
135126 store_edge_token ( "c1" , "token-1" ) . unwrap ( ) ;
136127 store_edge_token ( "c1" , "token-2" ) . unwrap ( ) ;
137128 assert_eq ! ( load_edge_token( "c1" ) , Some ( "token-2" . to_string( ) ) ) ;
@@ -141,7 +132,7 @@ mod tests {
141132 #[ test]
142133 fn remove_edge_token_deletes_file ( ) {
143134 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
144- with_tmp_xdg ( tmp. path ( ) , || {
135+ with_tmp_xdg ( tmp. path ( ) , |_ | {
145136 store_edge_token ( "c2" , "token" ) . unwrap ( ) ;
146137 assert ! ( load_edge_token( "c2" ) . is_some( ) ) ;
147138 remove_edge_token ( "c2" ) . unwrap ( ) ;
@@ -152,7 +143,7 @@ mod tests {
152143 #[ test]
153144 fn remove_edge_token_noop_when_missing ( ) {
154145 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
155- with_tmp_xdg ( tmp. path ( ) , || {
146+ with_tmp_xdg ( tmp. path ( ) , |_ | {
156147 // Should not error when file doesn't exist.
157148 remove_edge_token ( "nonexistent" ) . unwrap ( ) ;
158149 } ) ;
@@ -161,7 +152,7 @@ mod tests {
161152 #[ test]
162153 fn edge_token_paths_reject_multi_component_gateway_names ( ) {
163154 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
164- with_tmp_xdg ( tmp. path ( ) , || {
155+ with_tmp_xdg ( tmp. path ( ) , |_ | {
165156 assert ! ( store_edge_token( "../escape" , "token" ) . is_err( ) ) ;
166157 assert_eq ! ( load_edge_token( "../escape" ) , None ) ;
167158 assert ! ( remove_edge_token( "../escape" ) . is_err( ) ) ;
@@ -170,7 +161,7 @@ mod tests {
170161 #[ test]
171162 fn load_edge_token_trims_whitespace ( ) {
172163 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
173- with_tmp_xdg ( tmp. path ( ) , || {
164+ with_tmp_xdg ( tmp. path ( ) , |_ | {
174165 // Write manually with whitespace.
175166 let path = edge_token_path ( "ws-gateway" ) . unwrap ( ) ;
176167 std:: fs:: create_dir_all ( path. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
@@ -182,7 +173,7 @@ mod tests {
182173 #[ test]
183174 fn load_edge_token_returns_none_for_empty_file ( ) {
184175 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
185- with_tmp_xdg ( tmp. path ( ) , || {
176+ with_tmp_xdg ( tmp. path ( ) , |_ | {
186177 let path = edge_token_path ( "empty-gateway" ) . unwrap ( ) ;
187178 std:: fs:: create_dir_all ( path. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
188179 std:: fs:: write ( & path, " \n " ) . unwrap ( ) ;
@@ -193,7 +184,7 @@ mod tests {
193184 #[ test]
194185 fn load_edge_token_falls_back_to_legacy_cf_token ( ) {
195186 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
196- with_tmp_xdg ( tmp. path ( ) , || {
187+ with_tmp_xdg ( tmp. path ( ) , |_ | {
197188 // Write to the legacy cf_token path.
198189 let path = legacy_token_path ( "legacy-gateway" ) . unwrap ( ) ;
199190 std:: fs:: create_dir_all ( path. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
@@ -210,7 +201,7 @@ mod tests {
210201 fn store_edge_token_sets_permissions ( ) {
211202 use std:: os:: unix:: fs:: PermissionsExt ;
212203 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
213- with_tmp_xdg ( tmp. path ( ) , || {
204+ with_tmp_xdg ( tmp. path ( ) , |_ | {
214205 store_edge_token ( "perm-test" , "secret" ) . unwrap ( ) ;
215206 let path = edge_token_path ( "perm-test" ) . unwrap ( ) ;
216207 let mode = std:: fs:: metadata ( & path) . unwrap ( ) . permissions ( ) . mode ( ) & 0o777 ;
0 commit comments