11//! utilities for tests
22#![ cfg( test) ]
33
4- use crate :: cache_dir;
5- use std:: io:: Write as _;
4+ use std:: { cell:: RefCell , io:: Write as _} ;
65
76fn copy_dir_all (
87 src : impl AsRef < std:: path:: Path > ,
@@ -26,10 +25,18 @@ pub fn shader_crate_template_path() -> std::path::PathBuf {
2625 project_base. join ( "../shader-crate-template" )
2726}
2827
28+ thread_local ! {
29+ static TEMPDIR : RefCell <Option <tempfile:: TempDir >> = RefCell :: new( Some (
30+ tempfile:: TempDir :: with_prefix( "shader_crate" ) . unwrap( ) ,
31+ ) ) ;
32+ }
33+
2934pub 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+ } )
3340}
3441
3542pub 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::
4552}
4653
4754pub 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+ } ) ;
5358}
0 commit comments