Skip to content

Commit

Permalink
fix(graphical): Fix wrong severity of related errors (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Feb 9, 2023
1 parent b658fc0 commit 3497508
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/handlers/graphical.rs
Expand Up @@ -281,7 +281,7 @@ impl GraphicalReportHandler {
if let Some(related) = diagnostic.related() {
writeln!(f)?;
for rel in related {
match diagnostic.severity() {
match rel.severity() {
Some(Severity::Error) | None => write!(f, "Error: ")?,
Some(Severity::Warning) => write!(f, "Warning: ")?,
Some(Severity::Advice) => write!(f, "Advice: ")?,
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/narratable.rs
Expand Up @@ -132,7 +132,7 @@ impl NarratableReportHandler {
if let Some(related) = diagnostic.related() {
writeln!(f)?;
for rel in related {
match diagnostic.severity() {
match rel.severity() {
Some(Severity::Error) | None => write!(f, "Error: ")?,
Some(Severity::Warning) => write!(f, "Warning: ")?,
Some(Severity::Advice) => write!(f, "Advice: ")?,
Expand Down Expand Up @@ -218,10 +218,10 @@ impl NarratableReportHandler {
Ok(())
}

fn render_context<'a>(
fn render_context(
&self,
f: &mut impl fmt::Write,
source: &'a dyn SourceCode,
source: &dyn SourceCode,
context: &LabeledSpan,
labels: &[LabeledSpan],
) -> fmt::Result {
Expand Down
2 changes: 1 addition & 1 deletion src/protocol.rs
Expand Up @@ -488,7 +488,7 @@ impl SourceOffset {
Ok((
loc.file().into(),
fs::read_to_string(loc.file())
.map(|txt| Self::from_location(&txt, loc.line() as usize, loc.column() as usize))?,
.map(|txt| Self::from_location(txt, loc.line() as usize, loc.column() as usize))?,
))
}
}
Expand Down
126 changes: 126 additions & 0 deletions tests/graphical.rs
Expand Up @@ -1057,6 +1057,132 @@ Error: oops::my::bad
Ok(())
}

#[test]
fn related_severity() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
#[error("oops!")]
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
struct MyBad {
#[source_code]
src: NamedSource,
#[label("this bit here")]
highlight: SourceSpan,
#[related]
related: Vec<MyRelated>,
}

#[derive(Debug, Diagnostic, Error)]
enum MyRelated {
#[error("oops!")]
#[diagnostic(
severity(Error),
code(oops::my::related::error),
help("try doing it better next time?")
)]
Error {
#[source_code]
src: NamedSource,
#[label("this bit here")]
highlight: SourceSpan,
},

#[error("oops!")]
#[diagnostic(
severity(Warning),
code(oops::my::related::warning),
help("try doing it better next time?")
)]
Warning {
#[source_code]
src: NamedSource,
#[label("this bit here")]
highlight: SourceSpan,
},

#[error("oops!")]
#[diagnostic(
severity(Advice),
code(oops::my::related::advice),
help("try doing it better next time?")
)]
Advice {
#[source_code]
src: NamedSource,
#[label("this bit here")]
highlight: SourceSpan,
},
}

let src = "source\n text\n here".to_string();
let err = MyBad {
src: NamedSource::new("bad_file.rs", src.clone()),
highlight: (9, 4).into(),
related: vec![
MyRelated::Error {
src: NamedSource::new("bad_file.rs", src.clone()),
highlight: (0, 6).into(),
},
MyRelated::Warning {
src: NamedSource::new("bad_file.rs", src.clone()),
highlight: (0, 6).into(),
},
MyRelated::Advice {
src: NamedSource::new("bad_file.rs", src),
highlight: (0, 6).into(),
},
],
};
let out = fmt_report(err.into());
println!("Error: {}", out);
let expected = r#"oops::my::bad
× oops!
╭─[bad_file.rs:1:1]
1 │ source
2 │ text
· ──┬─
· ╰── this bit here
3 │ here
╰────
help: try doing it better next time?
Error: oops::my::related::error
× oops!
╭─[bad_file.rs:1:1]
1 │ source
· ───┬──
· ╰── this bit here
2 │ text
╰────
help: try doing it better next time?
Warning: oops::my::related::warning
⚠ oops!
╭─[bad_file.rs:1:1]
1 │ source
· ───┬──
· ╰── this bit here
2 │ text
╰────
help: try doing it better next time?
Advice: oops::my::related::advice
☞ oops!
╭─[bad_file.rs:1:1]
1 │ source
· ───┬──
· ╰── this bit here
2 │ text
╰────
help: try doing it better next time?
"#
.trim_start()
.to_string();
assert_eq!(expected, out);
Ok(())
}

#[test]
fn zero_length_eol_span() {
#[derive(Error, Debug, Diagnostic)]
Expand Down

0 comments on commit 3497508

Please sign in to comment.