@@ -8,10 +8,16 @@ use std::io::Write as _;
88use std:: path:: PathBuf ;
99use tempfile:: TempDir ;
1010
11+ /// `TestEnv` sets up a temp dir in `./target/cargo-gpu-test/` that is used as the cache dir. Not initializing a
12+ /// `TestEnv` and asking for the cache dir will panic, to ensure you set it up. Calling [`Self::setup_shader_crate`]
13+ /// or [`Self::setup_shader_crate_with_cargo_toml`] will copy the `shader_crate_template` into the temp dir and return
14+ /// you the path to it, so each test now has it's unique copy and won't race to change the template in the repo.
15+ /// Dropping `TestEnv` will clean up the dir, except when panic unwinding, so you can debug failures.
1116#[ must_use]
1217pub struct TestEnv ( TempDir ) ;
1318
1419impl TestEnv {
20+ /// Create a new [`TestEnv`]
1521 pub fn new ( ) -> Self {
1622 let target_dir = cargo_metadata:: MetadataCommand :: new ( )
1723 . exec ( )
@@ -32,12 +38,16 @@ impl TestEnv {
3238 TestEnv ( test_dir)
3339 }
3440
41+ /// Copies the `shader_crate_template` to the temp dir and returns the path to the directory.
3542 pub fn setup_shader_crate ( & self ) -> anyhow:: Result < PathBuf > {
3643 let shader_crate_path = crate :: cache_dir ( ) . unwrap ( ) . join ( "shader_crate" ) ;
3744 copy_dir_all ( shader_crate_template_path ( ) , & shader_crate_path) ?;
3845 Ok ( shader_crate_path)
3946 }
4047
48+ /// Like [`Self::setup_shader_crate`], copies the `shader_crate_template` to the temp dir and returns the path to
49+ /// the directory. Additionally, takes a closure to allow you to overwrite the contents of the `Cargo.toml`.
50+ /// This function will write the bare minimum for a valid crate, that is, give it a package name.
4151 pub fn setup_shader_crate_with_cargo_toml (
4252 & self ,
4353 f : impl FnOnce ( & mut File ) -> std:: io:: Result < ( ) > ,
@@ -69,7 +79,7 @@ thread_local! {
6979 static TESTDIR : RefCell <Option <PathBuf >> = RefCell :: new( None ) ;
7080}
7181
72- /// [`crate::cache_dir`] for testing
82+ /// [`crate::cache_dir`] for testing, see [`TestEnv`]
7383pub fn test_cache_dir ( ) -> anyhow:: Result < PathBuf > {
7484 Ok ( TESTDIR . with_borrow ( |a| a. clone ( ) ) . context (
7585 "TestEnv is not initialized! Add `let _env = TestEnv::new();` to the beginning of your test" ,
@@ -93,6 +103,7 @@ fn copy_dir_all(
93103 Ok ( ( ) )
94104}
95105
106+ /// Path to the `shader-crate-template` for copying or querying data
96107pub fn shader_crate_template_path ( ) -> PathBuf {
97108 let project_base = std:: path:: PathBuf :: from ( env ! ( "CARGO_MANIFEST_DIR" ) ) ;
98109 project_base. join ( "../shader-crate-template" )
0 commit comments