Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(linter): Junit reporter #8756

Merged
merged 9 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ phf = "0.11.2"
pico-args = "0.5.0"
prettyplease = "0.2.25"
project-root = "0.2.2"
quick-junit = "0.5.1"
Sysix marked this conversation as resolved.
Show resolved Hide resolved
rayon = "1.10.0"
regex = "1.11.1"
ropey = "1.6.1"
Expand Down
1 change: 1 addition & 0 deletions apps/oxlint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bpaf = { workspace = true, features = ["autocomplete", "bright-color", "derive"]
cow-utils = { workspace = true }
ignore = { workspace = true, features = ["simd-accel"] }
miette = { workspace = true }
quick-junit = { workspace = true }
rayon = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true }
Expand Down
109 changes: 109 additions & 0 deletions apps/oxlint/src/output_formatter/junit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use oxc_diagnostics::{
reporter::{DiagnosticReporter, DiagnosticResult, Info},
Error, Severity,
};
use rustc_hash::FxHashMap;

use super::InternalFormatter;
use quick_junit::{NonSuccessKind, Report, TestCase, TestCaseStatus, TestSuite};

#[derive(Default)]
pub struct JUnitOutputFormatter;

impl InternalFormatter for JUnitOutputFormatter {
fn get_diagnostic_reporter(&self) -> Box<dyn DiagnosticReporter> {
Box::new(JUnitReporter::default())
}
}

#[derive(Default)]
struct JUnitReporter {
diagnostics: Vec<Error>,
}

impl DiagnosticReporter for JUnitReporter {
fn finish(&mut self, _: &DiagnosticResult) -> Option<String> {
Some(format_junit(&self.diagnostics))
}

fn render_error(&mut self, error: Error) -> Option<String> {
self.diagnostics.push(error);
None
}
}

fn format_junit(diagnostics: &[Error]) -> String {
let mut grouped: FxHashMap<String, Vec<&Error>> = FxHashMap::default();

for diagnostic in diagnostics {
let info = Info::new(diagnostic);
grouped.entry(info.filename).or_default().push(diagnostic);
}

let mut report = Report::new("Oxlint");
for diagnostics in grouped.values() {
let diagnostic = diagnostics[0];
let filename = Info::new(diagnostic).filename;

let mut test_suite = TestSuite::new(filename);

for diagnostic in diagnostics {
let rule = diagnostic.code().map_or_else(String::new, |code| code.to_string());
let Info { message, start, .. } = Info::new(diagnostic);

let mut status = match diagnostic.severity() {
Some(Severity::Error) => TestCaseStatus::non_success(NonSuccessKind::Error),
_ => TestCaseStatus::non_success(NonSuccessKind::Failure),
};
status.set_message(message.clone());
status.set_description(format!(
"line {}, column {}, {}",
start.line,
start.column,
message.clone()
));
let test_case = TestCase::new(rule, status);
test_suite.add_test_case(test_case);
}
report.add_test_suite(test_suite);
}
report.to_string().unwrap()
}

#[cfg(test)]
mod test {
use super::*;
use oxc_diagnostics::{reporter::DiagnosticResult, NamedSource, OxcDiagnostic};
use oxc_span::Span;

#[test]
fn test_junit_reporter() {
const EXPECTED_REPORT: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="Oxlint" tests="2" failures="1" errors="1">
<testsuite name="file.js" tests="2" disabled="0" errors="1" failures="1">
<testcase name="">
<error message="error message">line 1, column 1, error message</error>
</testcase>
<testcase name="">
<failure message="warning message">line 1, column 1, warning message</failure>
</testcase>
</testsuite>
</testsuites>
"#;
let mut reporter = JUnitReporter::default();

let error = OxcDiagnostic::error("error message")
.with_label(Span::new(0, 8))
.with_source_code(NamedSource::new("file.js", "let a = ;"));

let warning = OxcDiagnostic::warn("warning message")
.with_label(Span::new(0, 9))
.with_source_code(NamedSource::new("file.js", "debugger;"));

reporter.render_error(error);
reporter.render_error(warning);

let output = reporter.finish(&DiagnosticResult::default()).unwrap();
assert_eq!(output.to_string(), EXPECTED_REPORT);
}
}
5 changes: 5 additions & 0 deletions apps/oxlint/src/output_formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod checkstyle;
mod default;
mod github;
mod json;
mod junit;
mod stylish;
mod unix;

Expand All @@ -10,6 +11,7 @@ use std::time::Duration;

use checkstyle::CheckStyleOutputFormatter;
use github::GithubOutputFormatter;
use junit::JUnitOutputFormatter;
use stylish::StylishOutputFormatter;
use unix::UnixOutputFormatter;

Expand All @@ -27,6 +29,7 @@ pub enum OutputFormat {
Unix,
Checkstyle,
Stylish,
JUnit,
}

impl FromStr for OutputFormat {
Expand All @@ -40,6 +43,7 @@ impl FromStr for OutputFormat {
"checkstyle" => Ok(Self::Checkstyle),
"github" => Ok(Self::Github),
"stylish" => Ok(Self::Stylish),
"junit" => Ok(Self::JUnit),
Sysix marked this conversation as resolved.
Show resolved Hide resolved
_ => Err(format!("'{s}' is not a known format")),
}
}
Expand Down Expand Up @@ -93,6 +97,7 @@ impl OutputFormatter {
OutputFormat::Unix => Box::<UnixOutputFormatter>::default(),
OutputFormat::Default => Box::new(DefaultOutputFormatter),
OutputFormat::Stylish => Box::<StylishOutputFormatter>::default(),
OutputFormat::JUnit => Box::<JUnitOutputFormatter>::default(),
}
}

Expand Down