1
1
//! utilities for tests
2
+
2
3
#![ cfg( test) ]
3
4
4
- use crate :: cache_dir;
5
- use std:: io:: Write as _;
5
+ use std:: { cell:: RefCell , io:: Write as _} ;
6
6
7
7
fn copy_dir_all (
8
8
src : impl AsRef < std:: path:: Path > ,
@@ -20,16 +20,23 @@ fn copy_dir_all(
20
20
}
21
21
Ok ( ( ) )
22
22
}
23
-
24
23
pub fn shader_crate_template_path ( ) -> std:: path:: PathBuf {
25
24
let project_base = std:: path:: PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
26
25
project_base. join ( "../shader-crate-template" )
27
26
}
28
27
28
+ thread_local ! {
29
+ static TEMPDIR : RefCell <Option <tempfile:: TempDir >> = RefCell :: new( Some (
30
+ tempfile:: TempDir :: with_prefix( "shader_crate" ) . unwrap( ) ,
31
+ ) ) ;
32
+ }
33
+
29
34
pub fn shader_crate_test_path ( ) -> std:: path:: PathBuf {
30
- let shader_crate_path = crate :: cache_dir ( ) . unwrap ( ) . join ( "shader_crate" ) ;
31
- copy_dir_all ( shader_crate_template_path ( ) , shader_crate_path. clone ( ) ) . unwrap ( ) ;
32
- shader_crate_path
35
+ TEMPDIR . with_borrow ( |tempdir| {
36
+ let shader_crate_path = tempdir. as_ref ( ) . unwrap ( ) . path ( ) ;
37
+ copy_dir_all ( shader_crate_template_path ( ) , shader_crate_path) . unwrap ( ) ;
38
+ shader_crate_path. to_path_buf ( )
39
+ } )
33
40
}
34
41
35
42
pub fn overwrite_shader_cargo_toml ( shader_crate_path : & std:: path:: Path ) -> std:: fs:: File {
@@ -45,9 +52,7 @@ pub fn overwrite_shader_cargo_toml(shader_crate_path: &std::path::Path) -> std::
45
52
}
46
53
47
54
pub fn tests_teardown ( ) {
48
- let cache_dir = cache_dir ( ) . unwrap ( ) ;
49
- if !cache_dir. exists ( ) {
50
- return ;
51
- }
52
- std:: fs:: remove_dir_all ( cache_dir) . unwrap ( ) ;
55
+ TEMPDIR . with_borrow_mut ( |tempdir| {
56
+ tempdir. take ( ) . unwrap ( ) . close ( ) . unwrap ( ) ;
57
+ } ) ;
53
58
}
0 commit comments