Skip to content

Commit 2f8bc9a

Browse files
committed
port remaining tests, delete json and json_tests modules
1 parent 339cfd3 commit 2f8bc9a

14 files changed

Lines changed: 74 additions & 264 deletions

crates/ruff/src/printer.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ use itertools::{Itertools, iterate};
99
use ruff_linter::linter::FixTable;
1010
use serde::Serialize;
1111

12-
use ruff_db::diagnostic::{Diagnostic, DiagnosticFormat, DisplayDiagnosticConfig, SecondaryCode};
12+
use ruff_db::diagnostic::{
13+
Diagnostic, DiagnosticFormat, DisplayDiagnosticConfig, DisplayDiagnostics, SecondaryCode,
14+
};
1315
use ruff_linter::fs::relativize_path;
1416
use ruff_linter::logging::LogLevel;
1517
use ruff_linter::message::{
16-
Emitter, EmitterContext, GithubEmitter, GitlabEmitter, GroupedEmitter, JsonEmitter,
17-
JsonLinesEmitter, JunitEmitter, PylintEmitter, RdjsonEmitter, SarifEmitter, TextEmitter,
18+
Emitter, EmitterContext, GithubEmitter, GitlabEmitter, GroupedEmitter, JunitEmitter,
19+
PylintEmitter, RdjsonEmitter, SarifEmitter, TextEmitter,
1820
};
1921
use ruff_linter::notify_user;
2022
use ruff_linter::settings::flags::{self};
@@ -228,13 +230,17 @@ impl Printer {
228230

229231
match self.format {
230232
OutputFormat::Json => {
231-
JsonEmitter.emit(writer, &diagnostics.inner, &context)?;
233+
let config = DisplayDiagnosticConfig::default().format(DiagnosticFormat::Json);
234+
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
235+
write!(writer, "{value}")?;
232236
}
233237
OutputFormat::Rdjson => {
234238
RdjsonEmitter.emit(writer, &diagnostics.inner, &context)?;
235239
}
236240
OutputFormat::JsonLines => {
237-
JsonLinesEmitter.emit(writer, &diagnostics.inner, &context)?;
241+
let config = DisplayDiagnosticConfig::default().format(DiagnosticFormat::JsonLines);
242+
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
243+
write!(writer, "{value}")?;
238244
}
239245
OutputFormat::Junit => {
240246
JunitEmitter.emit(writer, &diagnostics.inner, &context)?;
@@ -283,9 +289,8 @@ impl Printer {
283289
}
284290
OutputFormat::Azure => {
285291
let config = DisplayDiagnosticConfig::default().format(DiagnosticFormat::Azure);
286-
for diagnostic in &diagnostics.inner {
287-
write!(writer, "{}", diagnostic.display(&context, &config))?;
288-
}
292+
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
293+
write!(writer, "{value}")?;
289294
}
290295
OutputFormat::Sarif => {
291296
SarifEmitter.emit(writer, &diagnostics.inner, &context)?;

crates/ruff_db/src/diagnostic/render.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use super::{
2424

2525
mod azure;
2626
mod json;
27+
mod json_lines;
2728

2829
/// A type that implements `std::fmt::Display` for diagnostic rendering.
2930
///

crates/ruff_db/src/diagnostic/render/json.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,24 @@ impl Serialize for ExpandedEdits<'_> {
167167

168168
#[cfg(test)]
169169
mod tests {
170-
use crate::diagnostic::{DiagnosticFormat, render::tests::create_notebook_diagnostics};
170+
use crate::diagnostic::{
171+
DiagnosticFormat,
172+
render::tests::{
173+
create_diagnostics, create_notebook_diagnostics, create_syntax_error_diagnostics,
174+
},
175+
};
176+
177+
#[test]
178+
fn output() {
179+
let (env, diagnostics) = create_diagnostics(DiagnosticFormat::Json);
180+
insta::assert_snapshot!(env.render_diagnostics(&diagnostics));
181+
}
182+
183+
#[test]
184+
fn syntax_errors() {
185+
let (env, diagnostics) = create_syntax_error_diagnostics(DiagnosticFormat::Json);
186+
insta::assert_snapshot!(env.render_diagnostics(&diagnostics));
187+
}
171188

172189
#[test]
173190
fn notebook_output() {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::diagnostic::{
4+
DiagnosticFormat,
5+
render::tests::{
6+
create_diagnostics, create_notebook_diagnostics, create_syntax_error_diagnostics,
7+
},
8+
};
9+
10+
#[test]
11+
fn output() {
12+
let (env, diagnostics) = create_diagnostics(DiagnosticFormat::JsonLines);
13+
insta::assert_snapshot!(env.render_diagnostics(&diagnostics));
14+
}
15+
16+
#[test]
17+
fn syntax_errors() {
18+
let (env, diagnostics) = create_syntax_error_diagnostics(DiagnosticFormat::JsonLines);
19+
insta::assert_snapshot!(env.render_diagnostics(&diagnostics));
20+
}
21+
22+
#[test]
23+
fn notebook_output() {
24+
let (env, diagnostics) = create_notebook_diagnostics(DiagnosticFormat::JsonLines);
25+
insta::assert_snapshot!(env.render_diagnostics(&diagnostics));
26+
}
27+
}

crates/ruff_linter/src/message/snapshots/ruff_linter__message__json__tests__output.snap renamed to crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__json__tests__output.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
2-
source: crates/ruff_linter/src/message/json.rs
3-
expression: content
4-
snapshot_kind: text
2+
source: crates/ruff_db/src/diagnostic/render/json.rs
3+
expression: env.render_diagnostics(&diagnostics)
54
---
65
[
76
{

crates/ruff_linter/src/message/snapshots/ruff_linter__message__json__tests__syntax_errors.snap renamed to crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__json__tests__syntax_errors.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
2-
source: crates/ruff_linter/src/message/json.rs
3-
expression: content
4-
snapshot_kind: text
2+
source: crates/ruff_db/src/diagnostic/render/json.rs
3+
expression: env.render_diagnostics(&diagnostics)
54
---
65
[
76
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/ruff_db/src/diagnostic/render/json_lines.rs
3+
expression: env.render_diagnostics(&diagnostics)
4+
---
5+
{"cell":1,"code":"F401","end_location":{"column":10,"row":2},"filename":"notebook.py","fix":{"applicability":"safe","edits":[{"content":"","end_location":{"column":10,"row":2},"location":{"column":1,"row":2}}],"message":"Remove unused import: `os`"},"location":{"column":8,"row":2},"message":"`os` imported but unused","noqa_row":2,"url":"https://docs.astral.sh/ruff/rules/unused-import"}
6+
{"cell":2,"code":"F401","end_location":{"column":12,"row":2},"filename":"notebook.py","fix":{"applicability":"safe","edits":[{"content":"","end_location":{"column":1,"row":3},"location":{"column":1,"row":2}}],"message":"Remove unused import: `math`"},"location":{"column":8,"row":2},"message":"`math` imported but unused","noqa_row":2,"url":"https://docs.astral.sh/ruff/rules/unused-import"}
7+
{"cell":3,"code":"F841","end_location":{"column":6,"row":4},"filename":"notebook.py","fix":{"applicability":"unsafe","edits":[{"content":"","end_location":{"column":10,"row":4},"location":{"column":1,"row":4}}],"message":"Remove assignment to unused variable `x`"},"location":{"column":5,"row":4},"message":"Local variable `x` is assigned to but never used","noqa_row":4,"url":"https://docs.astral.sh/ruff/rules/unused-variable"}

crates/ruff_linter/src/message/snapshots/ruff_linter__message__json_lines__tests__output.snap renamed to crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__json_lines__tests__output.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
2-
source: crates/ruff_linter/src/message/json_lines.rs
3-
expression: content
4-
snapshot_kind: text
2+
source: crates/ruff_db/src/diagnostic/render/json_lines.rs
3+
expression: env.render_diagnostics(&diagnostics)
54
---
65
{"cell":null,"code":"F401","end_location":{"column":10,"row":1},"filename":"fib.py","fix":{"applicability":"unsafe","edits":[{"content":"","end_location":{"column":1,"row":2},"location":{"column":1,"row":1}}],"message":"Remove unused import: `os`"},"location":{"column":8,"row":1},"message":"`os` imported but unused","noqa_row":1,"url":"https://docs.astral.sh/ruff/rules/unused-import"}
76
{"cell":null,"code":"F841","end_location":{"column":6,"row":6},"filename":"fib.py","fix":{"applicability":"unsafe","edits":[{"content":"","end_location":{"column":10,"row":6},"location":{"column":5,"row":6}}],"message":"Remove assignment to unused variable `x`"},"location":{"column":5,"row":6},"message":"Local variable `x` is assigned to but never used","noqa_row":6,"url":"https://docs.astral.sh/ruff/rules/unused-variable"}

crates/ruff_linter/src/message/snapshots/ruff_linter__message__json_lines__tests__syntax_errors.snap renamed to crates/ruff_db/src/diagnostic/render/snapshots/ruff_db__diagnostic__render__json_lines__tests__syntax_errors.snap

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
2-
source: crates/ruff_linter/src/message/json_lines.rs
3-
expression: content
4-
snapshot_kind: text
2+
source: crates/ruff_db/src/diagnostic/render/json_lines.rs
3+
expression: env.render_diagnostics(&diagnostics)
54
---
65
{"cell":null,"code":null,"end_location":{"column":1,"row":2},"filename":"syntax_errors.py","fix":null,"location":{"column":15,"row":1},"message":"SyntaxError: Expected one or more symbol names after import","noqa_row":null,"url":null}
76
{"cell":null,"code":null,"end_location":{"column":1,"row":4},"filename":"syntax_errors.py","fix":null,"location":{"column":12,"row":3},"message":"SyntaxError: Expected ')', found newline","noqa_row":null,"url":null}

crates/ruff_linter/src/message/json.rs

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)