@@ -239,32 +239,40 @@ mod tests {
239
239
self . input = Some ( input. to_string ( ) ) ;
240
240
self
241
241
}
242
- fn run < T > ( & self , test : impl FnOnce ( Aocd ) -> Result < T > ) -> Result < T > {
243
- let dir = tempdir ( ) ?;
244
- let cache_path = dir. path ( ) . join ( "aocd-cache" ) ;
245
- std:: env:: set_var ( "AOC_SESSION" , "testsession" ) ;
246
- std:: env:: set_var ( "AOC_CACHE_DIR" , & cache_path) ;
247
- dbg ! ( & cache_path) ;
248
- let client = Aocd :: new ( self . year , self . day ) ;
242
+ fn run < F , T > ( & self , test : F ) -> Result < T >
243
+ where
244
+ T : std:: panic:: RefUnwindSafe ,
245
+ F : FnOnce ( & Aocd ) -> Result < T >
246
+ + std:: panic:: UnwindSafe
247
+ + std:: panic:: RefUnwindSafe
248
+ + Copy ,
249
+ {
250
+ let cache_path = std:: env:: temp_dir ( ) . join ( "aocd-tests" ) ;
251
+ let _ = std:: fs:: remove_dir_all ( & cache_path) ;
249
252
250
- let result = if let Some ( input) = & self . input {
251
- let url = format ! ( "{}/{}/day/{}/input" , client. url, client. year, client. day) ;
252
- let m = mock ( "GET" , url. as_str ( ) )
253
- . with_status ( 200 )
254
- . with_body ( input)
255
- . expect ( 0 )
256
- . create ( ) ;
257
- let result = test ( client) ;
258
- m. assert ( ) ;
259
- result
260
- } else {
261
- test ( client)
262
- } ;
263
-
264
- // Explicitly drop the cache so that we notice if it doesn't work.
265
- drop ( cache_path) ;
266
- dir. close ( ) ?;
267
- result
253
+ temp_env:: with_vars (
254
+ vec ! [
255
+ ( "AOC_SESSION" , Some ( "test-session" ) ) ,
256
+ ( "AOC_CACHE_DIR" , Some ( cache_path. to_str( ) . unwrap( ) ) ) ,
257
+ ] ,
258
+ move || {
259
+ let client = Aocd :: new ( self . year , self . day ) ;
260
+ if let Some ( input) = & self . input {
261
+ let url = format ! ( "/{}/day/{}/input" , client. year, client. day) ;
262
+ let m = mock ( "GET" , url. as_str ( ) )
263
+ . with_status ( 200 )
264
+ . with_header ( "content-type" , "text/plain" )
265
+ . with_body ( input)
266
+ . expect ( 1 )
267
+ . create ( ) ;
268
+ let result = test ( & client) ;
269
+ m. assert ( ) ;
270
+ result
271
+ } else {
272
+ test ( & client)
273
+ }
274
+ } ,
275
+ )
268
276
}
269
277
}
270
278
@@ -285,19 +293,22 @@ mod tests {
285
293
. day ( 1 )
286
294
. input ( "test input" )
287
295
. run ( |client| {
296
+ assert_eq ! ( client. get_input( ) , "test input" ) ;
297
+ // A second call will trigger a cache hit. If it doesn't the test will fail because
298
+ // the mock endpoint only expects a single call.
288
299
assert_eq ! ( client. get_input( ) , "test input" ) ;
289
300
Ok ( ( ) )
290
301
} )
291
302
}
292
303
293
304
#[ test]
294
305
fn test_find_aoc_token_env ( ) {
295
- std :: env :: set_var ( "AOC_SESSION" , "testsession" ) ;
296
- std :: env :: set_var ( "AOC_TOKEN" , "testtoken " ) ;
297
- assert_eq ! ( find_aoc_token ( ) , "testsession" ) ;
298
- std :: env :: remove_var ( "AOC_SESSION" ) ;
299
- assert_eq ! ( find_aoc_token( ) , "testtoken" ) ;
300
- std :: env :: remove_var ( "AOC_TOKEN" ) ;
306
+ temp_env :: with_var ( "AOC_SESSION" , Some ( "testsession" ) , || {
307
+ assert_eq ! ( find_aoc_token ( ) , "testsession " ) ;
308
+ } ) ;
309
+ temp_env :: with_var ( "AOC_TOKEN" , Some ( "testtoken" ) , || {
310
+ assert_eq ! ( find_aoc_token( ) , "testtoken" ) ;
311
+ } ) ;
301
312
}
302
313
303
314
#[ test]
@@ -307,10 +318,9 @@ mod tests {
307
318
let mut file = File :: create ( & file_path) ?;
308
319
writeln ! ( file, "testtokenintempfile" ) ?;
309
320
310
- std:: env:: remove_var ( "AOC_SESSION" ) ;
311
- std:: env:: remove_var ( "AOC_TOKEN" ) ;
312
- std:: env:: set_var ( "AOC_TOKEN_PATH" , & file_path) ;
313
- assert_eq ! ( find_aoc_token( ) , "testtokenintempfile" ) ;
314
- Ok ( ( ) )
321
+ temp_env:: with_var ( "AOC_TOKEN_PATH" , Some ( & file_path) , || {
322
+ assert_eq ! ( find_aoc_token( ) , "testtokenintempfile" ) ;
323
+ Ok ( ( ) )
324
+ } )
315
325
}
316
326
}
0 commit comments