Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3577c15

Browse files
committed
test(linter): add diagnostic format test snapshots
1 parent 34d3d72 commit 3577c15

File tree

12 files changed

+167
-15
lines changed

12 files changed

+167
-15
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/oxlint/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ ignore = { workspace = true, features = ["simd-accel"] }
4040
insta = { workspace = true }
4141
miette = { workspace = true }
4242
rayon = { workspace = true }
43+
regex = { workspace = true }
4344
rustc-hash = { workspace = true }
4445
serde = { workspace = true }
4546
serde_json = { workspace = true }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"rules": {
3+
"no-debugger": "error",
4+
"no-unused-vars": "warn"
5+
}
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function foo(a, b) {
2+
return a;
3+
}
4+
5+
debugger;

apps/oxlint/src/output_formatter/default.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use std::time::Duration;
22

3+
use crate::output_formatter::InternalFormatter;
34
use oxc_diagnostics::{
45
reporter::{DiagnosticReporter, DiagnosticResult},
56
Error, GraphicalReportHandler,
67
};
78
use oxc_linter::table::RuleTable;
89

9-
use crate::output_formatter::InternalFormatter;
10-
1110
#[derive(Debug)]
1211
pub struct DefaultOutputFormatter;
1312

@@ -59,12 +58,25 @@ struct GraphicalReporter {
5958
handler: GraphicalReportHandler,
6059
}
6160

61+
#[cfg(not(test))]
6262
impl Default for GraphicalReporter {
6363
fn default() -> Self {
6464
Self { handler: GraphicalReportHandler::new() }
6565
}
6666
}
6767

68+
#[cfg(test)]
69+
use oxc_diagnostics::GraphicalTheme;
70+
71+
/// we need to override the GraphicalReport for the tests
72+
/// because our CI can not handle colors output and [`GraphicalReportHandler`] will auto detect the environment
73+
#[cfg(test)]
74+
impl Default for GraphicalReporter {
75+
fn default() -> Self {
76+
Self { handler: GraphicalReportHandler::new_themed(GraphicalTheme::none()) }
77+
}
78+
}
79+
6880
impl DiagnosticReporter for GraphicalReporter {
6981
fn finish(&mut self, result: &DiagnosticResult) -> Option<String> {
7082
let mut output = String::new();
@@ -103,13 +115,6 @@ impl DiagnosticReporter for GraphicalReporter {
103115
Some(output)
104116
}
105117
}
106-
impl GraphicalReporter {
107-
#[cfg(test)]
108-
pub fn with_handler(mut self, handler: GraphicalReportHandler) -> Self {
109-
self.handler = handler;
110-
self
111-
}
112-
}
113118

114119
#[cfg(test)]
115120
mod test {
@@ -119,7 +124,7 @@ mod test {
119124
default::{DefaultOutputFormatter, GraphicalReporter},
120125
InternalFormatter, LintCommandInfo,
121126
};
122-
use miette::{GraphicalReportHandler, GraphicalTheme, NamedSource};
127+
use miette::NamedSource;
123128
use oxc_diagnostics::{
124129
reporter::{DiagnosticReporter, DiagnosticResult},
125130
OxcDiagnostic,
@@ -196,9 +201,7 @@ mod test {
196201

197202
#[test]
198203
fn reporter_error() {
199-
let mut reporter = GraphicalReporter::default().with_handler(
200-
GraphicalReportHandler::new_themed(GraphicalTheme::none()).with_links(false),
201-
);
204+
let mut reporter = GraphicalReporter::default();
202205

203206
let error = OxcDiagnostic::warn("error message")
204207
.with_label(Span::new(0, 8))

apps/oxlint/src/output_formatter/mod.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,58 @@ impl OutputFormatter {
111111
self.internal.get_diagnostic_reporter()
112112
}
113113
}
114+
115+
#[cfg(test)]
116+
mod test {
117+
use std::path::PathBuf;
118+
119+
use crate::tester::Tester;
120+
121+
#[test]
122+
fn test_output_formatter_diagnostic_default() {
123+
let cwd: PathBuf = "fixtures/output_formatter_diagnostic".into();
124+
let args = &["--format=default", "test.js"];
125+
126+
Tester::new().with_cwd(cwd.clone()).test_and_snapshot(args);
127+
}
128+
129+
/// disabled for windows
130+
/// json will output the offset which will be different for windows
131+
/// when there are multiple lines (`\r\n` vs `\n`)
132+
#[cfg(all(test, not(target_os = "windows")))]
133+
#[test]
134+
fn test_output_formatter_diagnostic_json() {
135+
let cwd: PathBuf = "fixtures/output_formatter_diagnostic".into();
136+
let args = &["--format=json", "test.js"];
137+
138+
Tester::new().with_cwd(cwd.clone()).test_and_snapshot(args);
139+
}
140+
141+
#[test]
142+
fn test_output_formatter_diagnostic_checkstyle() {
143+
let cwd: PathBuf = "fixtures/output_formatter_diagnostic".into();
144+
let args = &["--format=checkstyle", "test.js"];
145+
146+
Tester::new().with_cwd(cwd.clone()).test_and_snapshot(args);
147+
}
148+
149+
#[test]
150+
fn test_output_formatter_diagnostic_github() {
151+
let cwd: PathBuf = "fixtures/output_formatter_diagnostic".into();
152+
let args = &["--format=github", "test.js"];
153+
154+
Tester::new().with_cwd(cwd.clone()).test_and_snapshot(args);
155+
}
156+
157+
/// disabled for windows
158+
/// json will output the offset which will be different for windows
159+
/// when there are multiple lines (`\r\n` vs `\n`)
160+
#[cfg(all(test, not(target_os = "windows")))]
161+
#[test]
162+
fn test_output_formatter_diagnostic_stylish() {
163+
let cwd: PathBuf = "fixtures/output_formatter_diagnostic".into();
164+
let args = &["--format=stylish", "test.js"];
165+
166+
Tester::new().with_cwd(cwd.clone()).test_and_snapshot(args);
167+
}
168+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
--format=checkstyle test.js
6+
----------
7+
<?xml version="1.0" encoding="utf-8"?><checkstyle version="4.3"><file name="test.js"><error line="5" column="1" severity="error" message="`debugger` statement is not allowed" source="" /><error line="1" column="10" severity="warning" message="Function &apos;foo&apos; is declared but never used." source="" /><error line="1" column="17" severity="warning" message="Parameter &apos;b&apos; is declared but never used. Unused parameters should start with a &apos;_&apos;." source="" /></file></checkstyle>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
--format=default test.js
6+
----------
7+
8+
x ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html\eslint(no-debugger)]8;;\: `debugger` statement is not allowed
9+
,-[test.js:5:1]
10+
4 |
11+
5 | debugger;
12+
: ^^^^^^^^^
13+
`----
14+
help: Delete this code.
15+
16+
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html\eslint(no-unused-vars)]8;;\: Function 'foo' is declared but never used.
17+
,-[test.js:1:10]
18+
1 | function foo(a, b) {
19+
: ^|^
20+
: `-- 'foo' is declared here
21+
2 | return a;
22+
`----
23+
help: Consider removing this declaration.
24+
25+
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html\eslint(no-unused-vars)]8;;\: Parameter 'b' is declared but never used. Unused parameters should start with a '_'.
26+
,-[test.js:1:17]
27+
1 | function foo(a, b) {
28+
: |
29+
: `-- 'b' is declared here
30+
2 | return a;
31+
`----
32+
help: Consider removing this parameter.
33+
34+
Found 2 warnings and 1 error.
35+
Finished in <variable> on 1 file with 97 rules using <variable>.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
--format=github test.js
6+
----------
7+
::error file=test.js,line=5,endLine=5,col=1,endColumn=10,title=oxlint::`debugger` statement is not allowed
8+
::warning file=test.js,line=1,endLine=1,col=10,endColumn=13,title=oxlint::Function 'foo' is declared but never used.
9+
::warning file=test.js,line=1,endLine=1,col=17,endColumn=18,title=oxlint::Parameter 'b' is declared but never used. Unused parameters should start with a '_'.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
--format=json test.js
6+
----------
7+
[
8+
{"message": "`debugger` statement is not allowed","code": "eslint(no-debugger)","severity": "error","causes": [],"url": "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html","help": "Delete this code.","filename": "test.js","labels": [{"span": {"offset": 38,"length": 9}}],"related": []},
9+
{"message": "Function 'foo' is declared but never used.","code": "eslint(no-unused-vars)","severity": "warning","causes": [],"url": "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html","help": "Consider removing this declaration.","filename": "test.js","labels": [{"label": "'foo' is declared here","span": {"offset": 9,"length": 3}}],"related": []},
10+
{"message": "Parameter 'b' is declared but never used. Unused parameters should start with a '_'.","code": "eslint(no-unused-vars)","severity": "warning","causes": [],"url": "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-unused-vars.html","help": "Consider removing this parameter.","filename": "test.js","labels": [{"label": "'b' is declared here","span": {"offset": 16,"length": 1}}],"related": []}
11+
]

0 commit comments

Comments
 (0)