Skip to content

Commit

Permalink
[naga]: Make snapshot tests include paths in errors.
Browse files Browse the repository at this point in the history
Following Rust convention, let `naga::front::wgsl::ParseError`'s
methods `emit_to_stderr_with_path` and `emit_to_string_with_path`
accept any `AsRef<Path>` argument as the path.

Pass input paths in snapshot tests, so that failures processing
shaders name the input file being processed.
  • Loading branch information
jimblandy authored and teoxoy committed Nov 21, 2023
1 parent 104119a commit a820a3f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
3 changes: 1 addition & 2 deletions naga-cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ fn run() -> Result<(), Box<dyn std::error::Error>> {
match result {
Ok(v) => (v, Some(input)),
Err(ref e) => {
let path = input_path.to_string_lossy();
e.emit_to_stderr_with_path(&input, &path);
e.emit_to_stderr_with_path(&input, input_path);
return Err(CliError("Could not parse WGSL").into());
}
}
Expand Down
12 changes: 10 additions & 2 deletions naga/src/front/wgsl/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ impl ParseError {
}

/// Emits a summary of the error to standard error stream.
pub fn emit_to_stderr_with_path(&self, source: &str, path: &str) {
pub fn emit_to_stderr_with_path<P>(&self, source: &str, path: P)
where
P: AsRef<std::path::Path>,
{
let path = path.as_ref().display().to_string();
let files = SimpleFile::new(path, source);
let config = codespan_reporting::term::Config::default();
let writer = StandardStream::stderr(ColorChoice::Auto);
Expand All @@ -69,7 +73,11 @@ impl ParseError {
}

/// Emits a summary of the error to a string.
pub fn emit_to_string_with_path(&self, source: &str, path: &str) -> String {
pub fn emit_to_string_with_path<P>(&self, source: &str, path: P) -> String
where
P: AsRef<std::path::Path>,
{
let path = path.as_ref().display().to_string();
let files = SimpleFile::new(path, source);
let config = codespan_reporting::term::Config::default();
let mut writer = NoColor::new(Vec::new());
Expand Down
10 changes: 8 additions & 2 deletions naga/tests/snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,10 @@ fn convert_wgsl() {
let source = input.read_source();
match naga::front::wgsl::parse_str(&source) {
Ok(mut module) => check_targets(&input, &mut module, targets, None),
Err(e) => panic!("{}", e.emit_to_string(&source)),
Err(e) => panic!(
"{}",
e.emit_to_string_with_path(&source, input.input_path())
),
}
}

Expand All @@ -798,7 +801,10 @@ fn convert_wgsl() {
let source = input.read_source();
match naga::front::wgsl::parse_str(&source) {
Ok(mut module) => check_targets(&input, &mut module, targets, Some(&source)),
Err(e) => panic!("{}", e.emit_to_string(&source)),
Err(e) => panic!(
"{}",
e.emit_to_string_with_path(&source, input.input_path())
),
}
}
}
Expand Down

0 comments on commit a820a3f

Please sign in to comment.