Skip to content

Commit e12fff0

Browse files
committed
move testing utilities to mod test
1 parent dfa71c0 commit e12fff0

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

crates/cargo-gpu/src/main.rs

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ mod lockfile;
6767
mod metadata;
6868
mod show;
6969
mod spirv_source;
70+
mod test;
7071

7172
/// Central function to write to the user.
7273
#[macro_export]
@@ -223,57 +224,3 @@ fn to_dirname(text: &str) -> String {
223224
.collect::<Vec<_>>()
224225
.concat()
225226
}
226-
227-
#[cfg(test)]
228-
mod test {
229-
use crate::cache_dir;
230-
use std::io::Write as _;
231-
232-
fn copy_dir_all(
233-
src: impl AsRef<std::path::Path>,
234-
dst: impl AsRef<std::path::Path>,
235-
) -> anyhow::Result<()> {
236-
std::fs::create_dir_all(&dst)?;
237-
for maybe_entry in std::fs::read_dir(src)? {
238-
let entry = maybe_entry?;
239-
let ty = entry.file_type()?;
240-
if ty.is_dir() {
241-
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
242-
} else {
243-
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
244-
}
245-
}
246-
Ok(())
247-
}
248-
249-
pub fn shader_crate_template_path() -> std::path::PathBuf {
250-
let project_base = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
251-
project_base.join("../shader-crate-template")
252-
}
253-
254-
pub fn shader_crate_test_path() -> std::path::PathBuf {
255-
let shader_crate_path = crate::cache_dir().unwrap().join("shader_crate");
256-
copy_dir_all(shader_crate_template_path(), shader_crate_path.clone()).unwrap();
257-
shader_crate_path
258-
}
259-
260-
pub fn overwrite_shader_cargo_toml(shader_crate_path: &std::path::Path) -> std::fs::File {
261-
let cargo_toml = shader_crate_path.join("Cargo.toml");
262-
let mut file = std::fs::OpenOptions::new()
263-
.write(true)
264-
.truncate(true)
265-
.open(cargo_toml)
266-
.unwrap();
267-
writeln!(file, "[package]").unwrap();
268-
writeln!(file, "name = \"test\"").unwrap();
269-
file
270-
}
271-
272-
pub fn tests_teardown() {
273-
let cache_dir = cache_dir().unwrap();
274-
if !cache_dir.exists() {
275-
return;
276-
}
277-
std::fs::remove_dir_all(cache_dir).unwrap();
278-
}
279-
}

crates/cargo-gpu/src/test.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! utilities for tests
2+
#![cfg(test)]
3+
4+
use crate::cache_dir;
5+
use std::io::Write as _;
6+
7+
fn copy_dir_all(
8+
src: impl AsRef<std::path::Path>,
9+
dst: impl AsRef<std::path::Path>,
10+
) -> anyhow::Result<()> {
11+
std::fs::create_dir_all(&dst)?;
12+
for maybe_entry in std::fs::read_dir(src)? {
13+
let entry = maybe_entry?;
14+
let ty = entry.file_type()?;
15+
if ty.is_dir() {
16+
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
17+
} else {
18+
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
19+
}
20+
}
21+
Ok(())
22+
}
23+
24+
pub fn shader_crate_template_path() -> std::path::PathBuf {
25+
let project_base = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
26+
project_base.join("../shader-crate-template")
27+
}
28+
29+
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
33+
}
34+
35+
pub fn overwrite_shader_cargo_toml(shader_crate_path: &std::path::Path) -> std::fs::File {
36+
let cargo_toml = shader_crate_path.join("Cargo.toml");
37+
let mut file = std::fs::OpenOptions::new()
38+
.write(true)
39+
.truncate(true)
40+
.open(cargo_toml)
41+
.unwrap();
42+
writeln!(file, "[package]").unwrap();
43+
writeln!(file, "name = \"test\"").unwrap();
44+
file
45+
}
46+
47+
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();
53+
}

0 commit comments

Comments
 (0)