Skip to content

Commit 611a0a1

Browse files
committed
Prefix snapshot outputs with input language
1 parent db4cb26 commit 611a0a1

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

naga/tests/snapshots.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(dead_code, unused_imports)]
44

55
use std::{
6+
fmt::Write,
67
fs,
78
path::{Path, PathBuf},
89
};
@@ -159,7 +160,7 @@ struct Input {
159160
///
160161
/// If the subdirectory is omitted, we assume that the output goes
161162
/// to "wgsl".
162-
subdirectory: Option<PathBuf>,
163+
subdirectory: PathBuf,
163164

164165
/// The input filename name, without a directory.
165166
file_name: PathBuf,
@@ -182,9 +183,9 @@ impl Input {
182183
/// The `input` path is interpreted relative to the `BASE_DIR_IN`
183184
/// subdirectory of the directory given by the `CARGO_MANIFEST_DIR`
184185
/// environment variable.
185-
fn new(subdirectory: Option<&str>, name: &str, extension: &str) -> Input {
186+
fn new(subdirectory: &str, name: &str, extension: &str) -> Input {
186187
Input {
187-
subdirectory: subdirectory.map(PathBuf::from),
188+
subdirectory: PathBuf::from(subdirectory),
188189
// Don't wipe out any extensions on `name`, as
189190
// `with_extension` would do.
190191
file_name: PathBuf::from(format!("{name}.{extension}")),
@@ -194,13 +195,11 @@ impl Input {
194195

195196
/// Return an iterator that produces an `Input` for each entry in `subdirectory`.
196197
fn files_in_dir(
197-
subdirectory: Option<&'static str>,
198+
subdirectory: &'static str,
198199
file_extensions: &'static [&'static str],
199200
) -> impl Iterator<Item = Input> + 'static {
200-
let mut input_directory = Path::new(env!("CARGO_MANIFEST_DIR")).join(BASE_DIR_IN);
201-
if let Some(ref subdirectory) = subdirectory {
202-
input_directory.push(subdirectory);
203-
}
201+
let input_directory = Path::new(CRATE_ROOT).join(BASE_DIR_IN).join(subdirectory);
202+
204203
let entries = match std::fs::read_dir(&input_directory) {
205204
Ok(entries) => entries,
206205
Err(err) => panic!(
@@ -237,14 +236,12 @@ impl Input {
237236
/// Return the path to the input directory.
238237
fn input_directory(&self) -> PathBuf {
239238
let mut dir = Path::new(CRATE_ROOT).join(BASE_DIR_IN);
240-
if let Some(ref subdirectory) = self.subdirectory {
241-
dir.push(subdirectory);
242-
}
239+
dir.push(&self.subdirectory);
243240
dir
244241
}
245242

246243
/// Return the path to the output directory.
247-
fn output_directory(&self, subdirectory: &str) -> PathBuf {
244+
fn output_directory(subdirectory: &str) -> PathBuf {
248245
let mut dir = Path::new(CRATE_ROOT).join(BASE_DIR_OUT);
249246
dir.push(subdirectory);
250247
dir
@@ -258,14 +255,22 @@ impl Input {
258255
}
259256

260257
fn output_path(&self, subdirectory: &str, extension: &str) -> PathBuf {
261-
let mut output = self.output_directory(subdirectory);
258+
let mut output = Self::output_directory(subdirectory);
262259
if self.keep_input_extension {
263-
let mut file_name = self.file_name.as_os_str().to_owned();
264-
file_name.push(".");
265-
file_name.push(extension);
260+
let mut file_name = String::new();
261+
write!(&mut file_name, "{}", self.subdirectory.display()).unwrap();
262+
file_name.push_str("-");
263+
write!(&mut file_name, "{}", self.file_name.display()).unwrap();
264+
file_name.push('.');
265+
file_name.push_str(extension);
266266
output.push(&file_name);
267267
} else {
268-
output.push(&self.file_name);
268+
let mut file_name = String::new();
269+
write!(&mut file_name, "{}", self.subdirectory.display()).unwrap();
270+
file_name.push_str("-");
271+
write!(&mut file_name, "{}", self.file_name.display()).unwrap();
272+
273+
output.push(&file_name);
269274
output.set_extension(extension);
270275
}
271276
output
@@ -771,7 +776,7 @@ fn write_output_wgsl(
771776
fn convert_snapshots_wgsl() {
772777
let _ = env_logger::try_init();
773778

774-
for input in Input::files_in_dir(Some("wgsl"), &["wgsl"]) {
779+
for input in Input::files_in_dir("wgsl", &["wgsl"]) {
775780
let source = input.read_source();
776781
// crlf will make the large split output different on different platform
777782
let source = source.replace('\r', "");
@@ -792,7 +797,7 @@ fn convert_snapshots_spv() {
792797

793798
let _ = env_logger::try_init();
794799

795-
for input in Input::files_in_dir(Some("spv"), &["spvasm"]) {
800+
for input in Input::files_in_dir("spv", &["spvasm"]) {
796801
println!("Assembling '{}'", input.file_name.display());
797802

798803
let command = Command::new("spirv-as")
@@ -840,7 +845,7 @@ fn convert_snapshots_spv() {
840845
fn convert_snapshots_glsl() {
841846
let _ = env_logger::try_init();
842847

843-
for input in Input::files_in_dir(Some("glsl"), &["vert", "frag", "comp"]) {
848+
for input in Input::files_in_dir("glsl", &["vert", "frag", "comp"]) {
844849
let input = Input {
845850
keep_input_extension: true,
846851
..input

0 commit comments

Comments
 (0)