@@ -136,17 +136,10 @@ impl SimpleDiskCache {
136136        let  name = name. to_string ( ) ; 
137137        let  dir = PathBuf :: from ( dir) ; 
138138        let  path = dir. join ( & name) ; 
139-         // Cache folder must either not exist or exist with metadata. 
140139        if  !dir. as_path ( ) . exists ( )  { 
141140            panic ! ( "Cache parent dir {:?} must exist" ,  dir) 
142141        }  else  if  path. exists ( )  { 
143-             let  file = path. join ( SimpleDiskCache :: STATE_FILE ) ; 
144-             if  !file. exists ( )  { 
145-                 panic ! ( "Cache folder {:?} already exists without metadata" ,  path. to_str( ) ) 
146-             } 
147-             // Wipe existing cache. 
148-             std:: fs:: remove_dir_all ( & path) . unwrap ( ) ; 
149-             std:: fs:: create_dir ( & path) . unwrap ( ) ; 
142+             panic ! ( "Cache folder {:?} already exists" ,  path. to_str( ) ) 
150143        }  else  { 
151144            std:: fs:: create_dir ( & path) . unwrap ( ) ; 
152145        } 
@@ -295,16 +288,20 @@ impl SimpleDiskCache {
295288
296289#[ cfg( test) ]  
297290mod  tests { 
291+     extern  crate  tempdir; 
292+ 
298293    use  super :: * ; 
299294    use  std:: time:: Duration ; 
295+     use  tempdir:: TempDir ; 
300296    use  tokio:: time:: sleep; 
301297
302298    #[ tokio:: test]  
303299    async  fn  test_simple_disk_cache ( )  { 
304300        // Arrange 
301+         let  tmp_dir = TempDir :: new ( "simple_disk_cache" ) . unwrap ( ) ; 
305302        let  cache = SimpleDiskCache :: new ( 
306303            "test-cache-1" , 
307-             "./" , 
304+             tmp_dir . path ( ) . to_str ( ) . unwrap ( ) , 
308305            10 ,   // ttl 
309306            60 ,   // purge period 
310307            None  // max size 
@@ -366,9 +363,10 @@ mod tests {
366363
367364        let  ttl = 1 ; 
368365        let  time_between_inserts = 1 ; 
366+         let  tmp_dir = TempDir :: new ( "simple_disk_cache" ) . unwrap ( ) ; 
369367        let  cache = SimpleDiskCache :: new ( 
370368            "test-cache-2" , 
371-             "./" , 
369+             tmp_dir . path ( ) . to_str ( ) . unwrap ( ) , 
372370            ttl,     // ttl for cache entries 
373371            1000 ,  // purge expired interval set large to not trigger expiry on "set" 
374372            None   // max cache size unset 
@@ -406,9 +404,10 @@ mod tests {
406404
407405        let  ttl = 1 ; 
408406        let  time_between_inserts = ttl; 
407+         let  tmp_dir = TempDir :: new ( "simple_disk_cache" ) . unwrap ( ) ; 
409408        let  cache = SimpleDiskCache :: new ( 
410409            "test-cache-3" , 
411-             "./" , 
410+             tmp_dir . path ( ) . to_str ( ) . unwrap ( ) , 
412411            ttl,     // ttl for cache entries 
413412            1000 ,  // purge expired interval set large to not trigger expiry on "set" 
414413            None   // max cache size unset 
@@ -458,9 +457,10 @@ mod tests {
458457        let  time_between_inserts = ttl; 
459458        let  size = 1000 ; 
460459        let  chunk = vec ! [ 0 ;  size] ; 
460+         let  tmp_dir = TempDir :: new ( "simple_disk_cache" ) . unwrap ( ) ; 
461461        let  cache = SimpleDiskCache :: new ( 
462462            "test-cache-4" , 
463-             "./" , 
463+             tmp_dir . path ( ) . to_str ( ) . unwrap ( ) , 
464464            ttl,            // ttl for cache entries 
465465            1000 ,         // purge expired interval set large to not trigger expiry on "set" 
466466            Some ( size* 2 )  // max cache size accomodates two entries 
@@ -501,9 +501,10 @@ mod tests {
501501        let  time_between_inserts = 1 ; 
502502        let  size = 1000 ; 
503503        let  chunk = vec ! [ 0 ;  size] ; 
504+         let  tmp_dir = TempDir :: new ( "simple_disk_cache" ) . unwrap ( ) ; 
504505        let  cache = SimpleDiskCache :: new ( 
505506            "test-cache-5" , 
506-             "./" , 
507+             tmp_dir . path ( ) . to_str ( ) . unwrap ( ) , 
507508            ttl,            // ttl for cache entries 
508509            1000 ,         // purge expired interval set large to not trigger expiry on "set" 
509510            Some ( size* 2 )  // max cache size accomodates two entries 
@@ -540,9 +541,10 @@ mod tests {
540541        // set(1st) -> prune() -> [no threshold hit] -> set(2nd) -> [periodic expiry hit] -> prune() -> prune_expired() -> [1st removed] 
541542        let  ttl = 1 ; 
542543        let  time_between_inserts = ttl; 
544+         let  tmp_dir = TempDir :: new ( "simple_disk_cache" ) . unwrap ( ) ; 
543545        let  cache = SimpleDiskCache :: new ( 
544546            "test-cache-6" , 
545-             "./" , 
547+             tmp_dir . path ( ) . to_str ( ) . unwrap ( ) , 
546548            ttl,  // ttl for cache entries 
547549            ttl,  // purge expired interval 
548550            None 
0 commit comments