|
| 1 | +use std::{collections::BTreeMap, ops::Deref, path::Path}; |
| 2 | + |
| 3 | +use quick_junit::{NonSuccessKind, Report, TestCase, TestCaseStatus, TestSuite, XmlString}; |
| 4 | + |
| 5 | +use ruff_source_file::LineColumn; |
| 6 | + |
| 7 | +use crate::diagnostic::{Diagnostic, SecondaryCode, render::FileResolver}; |
| 8 | + |
| 9 | +/// A renderer for diagnostics in the [JUnit] format. |
| 10 | +/// |
| 11 | +/// See [`junit.xsd`] for the specification in the JUnit repository and an annotated [version] |
| 12 | +/// linked from the [`quick_junit`] docs. |
| 13 | +/// |
| 14 | +/// [JUnit]: https://junit.org/ |
| 15 | +/// [`junit.xsd`]: https://github.com/junit-team/junit-framework/blob/2870b7d8fd5bf7c1efe489d3991d3ed3900e82bb/platform-tests/src/test/resources/jenkins-junit.xsd |
| 16 | +/// [version]: https://llg.cubic.org/docs/junit/ |
| 17 | +/// [`quick_junit`]: https://docs.rs/quick-junit/latest/quick_junit/ |
| 18 | +pub struct JunitRenderer<'a> { |
| 19 | + resolver: &'a dyn FileResolver, |
| 20 | +} |
| 21 | + |
| 22 | +impl<'a> JunitRenderer<'a> { |
| 23 | + pub fn new(resolver: &'a dyn FileResolver) -> Self { |
| 24 | + Self { resolver } |
| 25 | + } |
| 26 | + |
| 27 | + pub(super) fn render( |
| 28 | + &self, |
| 29 | + f: &mut std::fmt::Formatter, |
| 30 | + diagnostics: &[Diagnostic], |
| 31 | + ) -> std::fmt::Result { |
| 32 | + let mut report = Report::new("ruff"); |
| 33 | + |
| 34 | + if diagnostics.is_empty() { |
| 35 | + let mut test_suite = TestSuite::new("ruff"); |
| 36 | + test_suite |
| 37 | + .extra |
| 38 | + .insert(XmlString::new("package"), XmlString::new("org.ruff")); |
| 39 | + let mut case = TestCase::new("No errors found", TestCaseStatus::success()); |
| 40 | + case.set_classname("ruff"); |
| 41 | + test_suite.add_test_case(case); |
| 42 | + report.add_test_suite(test_suite); |
| 43 | + } else { |
| 44 | + for (filename, diagnostics) in group_diagnostics_by_filename(diagnostics, self.resolver) |
| 45 | + { |
| 46 | + let mut test_suite = TestSuite::new(filename); |
| 47 | + test_suite |
| 48 | + .extra |
| 49 | + .insert(XmlString::new("package"), XmlString::new("org.ruff")); |
| 50 | + |
| 51 | + let classname = Path::new(filename).with_extension(""); |
| 52 | + |
| 53 | + for diagnostic in diagnostics { |
| 54 | + let DiagnosticWithLocation { |
| 55 | + diagnostic, |
| 56 | + start_location: location, |
| 57 | + } = diagnostic; |
| 58 | + let mut status = TestCaseStatus::non_success(NonSuccessKind::Failure); |
| 59 | + status.set_message(diagnostic.body()); |
| 60 | + |
| 61 | + if let Some(location) = location { |
| 62 | + status.set_description(format!( |
| 63 | + "line {row}, col {col}, {body}", |
| 64 | + row = location.line, |
| 65 | + col = location.column, |
| 66 | + body = diagnostic.body() |
| 67 | + )); |
| 68 | + } else { |
| 69 | + status.set_description(diagnostic.body()); |
| 70 | + } |
| 71 | + |
| 72 | + let code = diagnostic |
| 73 | + .secondary_code() |
| 74 | + .map_or_else(|| diagnostic.name(), SecondaryCode::as_str); |
| 75 | + let mut case = TestCase::new(format!("org.ruff.{code}"), status); |
| 76 | + case.set_classname(classname.to_str().unwrap()); |
| 77 | + |
| 78 | + if let Some(location) = location { |
| 79 | + case.extra.insert( |
| 80 | + XmlString::new("line"), |
| 81 | + XmlString::new(location.line.to_string()), |
| 82 | + ); |
| 83 | + case.extra.insert( |
| 84 | + XmlString::new("column"), |
| 85 | + XmlString::new(location.column.to_string()), |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + test_suite.add_test_case(case); |
| 90 | + } |
| 91 | + report.add_test_suite(test_suite); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + let adapter = FmtAdapter { fmt: f }; |
| 96 | + report.serialize(adapter).map_err(|_| std::fmt::Error) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// TODO(brent) this and `group_diagnostics_by_filename` are also used by the `grouped` output |
| 101 | +// format. I think they'd make more sense in that file, but I started here first. I'll move them to |
| 102 | +// that module when adding the `grouped` output format. |
| 103 | +struct DiagnosticWithLocation<'a> { |
| 104 | + diagnostic: &'a Diagnostic, |
| 105 | + start_location: Option<LineColumn>, |
| 106 | +} |
| 107 | + |
| 108 | +impl Deref for DiagnosticWithLocation<'_> { |
| 109 | + type Target = Diagnostic; |
| 110 | + |
| 111 | + fn deref(&self) -> &Self::Target { |
| 112 | + self.diagnostic |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +fn group_diagnostics_by_filename<'a>( |
| 117 | + diagnostics: &'a [Diagnostic], |
| 118 | + resolver: &'a dyn FileResolver, |
| 119 | +) -> BTreeMap<&'a str, Vec<DiagnosticWithLocation<'a>>> { |
| 120 | + let mut grouped_diagnostics = BTreeMap::default(); |
| 121 | + for diagnostic in diagnostics { |
| 122 | + let (filename, start_location) = diagnostic |
| 123 | + .primary_span_ref() |
| 124 | + .map(|span| { |
| 125 | + let file = span.file(); |
| 126 | + let start_location = |
| 127 | + span.range() |
| 128 | + .filter(|_| !resolver.is_notebook(file)) |
| 129 | + .map(|range| { |
| 130 | + file.diagnostic_source(resolver) |
| 131 | + .as_source_code() |
| 132 | + .line_column(range.start()) |
| 133 | + }); |
| 134 | + |
| 135 | + (span.file().path(resolver), start_location) |
| 136 | + }) |
| 137 | + .unwrap_or_default(); |
| 138 | + |
| 139 | + grouped_diagnostics |
| 140 | + .entry(filename) |
| 141 | + .or_insert_with(Vec::new) |
| 142 | + .push(DiagnosticWithLocation { |
| 143 | + diagnostic, |
| 144 | + start_location, |
| 145 | + }); |
| 146 | + } |
| 147 | + grouped_diagnostics |
| 148 | +} |
| 149 | + |
| 150 | +struct FmtAdapter<'a> { |
| 151 | + fmt: &'a mut dyn std::fmt::Write, |
| 152 | +} |
| 153 | + |
| 154 | +impl std::io::Write for FmtAdapter<'_> { |
| 155 | + fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { |
| 156 | + self.fmt |
| 157 | + .write_str(std::str::from_utf8(buf).map_err(|_| { |
| 158 | + std::io::Error::new( |
| 159 | + std::io::ErrorKind::InvalidData, |
| 160 | + "Invalid UTF-8 in JUnit report", |
| 161 | + ) |
| 162 | + })?) |
| 163 | + .map_err(std::io::Error::other)?; |
| 164 | + |
| 165 | + Ok(buf.len()) |
| 166 | + } |
| 167 | + |
| 168 | + fn flush(&mut self) -> std::io::Result<()> { |
| 169 | + Ok(()) |
| 170 | + } |
| 171 | + |
| 172 | + fn write_fmt(&mut self, args: std::fmt::Arguments<'_>) -> std::io::Result<()> { |
| 173 | + self.fmt.write_fmt(args).map_err(std::io::Error::other) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +#[cfg(test)] |
| 178 | +mod tests { |
| 179 | + use crate::diagnostic::{ |
| 180 | + DiagnosticFormat, |
| 181 | + render::tests::{create_diagnostics, create_syntax_error_diagnostics}, |
| 182 | + }; |
| 183 | + |
| 184 | + #[test] |
| 185 | + fn output() { |
| 186 | + let (env, diagnostics) = create_diagnostics(DiagnosticFormat::Junit); |
| 187 | + insta::assert_snapshot!(env.render_diagnostics(&diagnostics)); |
| 188 | + } |
| 189 | + |
| 190 | + #[test] |
| 191 | + fn syntax_errors() { |
| 192 | + let (env, diagnostics) = create_syntax_error_diagnostics(DiagnosticFormat::Junit); |
| 193 | + insta::assert_snapshot!(env.render_diagnostics(&diagnostics)); |
| 194 | + } |
| 195 | +} |
0 commit comments