Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions crates/ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ruff_python_semantic = { path = "../ruff_python_semantic" }
ruff_python_stdlib = { path = "../ruff_python_stdlib" }
ruff_rustpython = { path = "../ruff_rustpython" }

annotate-snippets = { version = "0.9.1", features = ["color"] }
anyhow = { workspace = true }
bitflags = { workspace = true }
chrono = { workspace = true }
Expand All @@ -48,6 +49,7 @@ path-absolutize = { workspace = true, features = [
] }
pathdiff = { version = "0.2.1" }
pep440_rs = { version = "0.3.1", features = ["serde"] }
quick-junit = { version = "0.3.2" }
regex = { workspace = true }
result-like = { version = "0.4.6" }
rustc-hash = { workspace = true }
Expand All @@ -71,6 +73,8 @@ unicode-width = { version = "0.1.10" }
insta = { workspace = true, features = ["yaml", "redactions"] }
pretty_assertions = "1.3.0"
test-case = { workspace = true }
# Disable colored output in tests
colored = { workspace = true, features = ["no-color"] }

[features]
default = []
Expand Down
9 changes: 7 additions & 2 deletions crates/ruff/src/autofix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ pub mod actions;

/// Auto-fix errors in a file, and write the fixed source code to disk.
pub fn fix_file(diagnostics: &[Diagnostic], locator: &Locator) -> Option<(String, FixTable)> {
if diagnostics.iter().all(|check| check.fix.is_empty()) {
let mut with_fixes = diagnostics
.iter()
.filter(|diag| !diag.fix.is_empty())
.peekable();

if with_fixes.peek().is_none() {
None
} else {
Some(apply_fixes(diagnostics.iter(), locator))
Some(apply_fixes(with_fixes, locator))
}
}

Expand Down
89 changes: 0 additions & 89 deletions crates/ruff/src/message.rs

This file was deleted.

53 changes: 53 additions & 0 deletions crates/ruff/src/message/azure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::message::{Emitter, EmitterContext, Message};
use crate::registry::AsRule;
use std::io::Write;

/// Generate error logging commands for Azure Pipelines format.
/// See [documentation](https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#logissue-log-an-error-or-warning)
#[derive(Default)]
pub struct AzureEmitter;

impl Emitter for AzureEmitter {
fn emit(
&mut self,
writer: &mut dyn Write,
messages: &[Message],
context: &EmitterContext,
) -> anyhow::Result<()> {
for message in messages {
let (line, col) = if context.is_jupyter_notebook(&message.filename) {
// We can't give a reasonable location for the structured formats,
// so we show one that's clearly a fallback
(1, 0)
} else {
(message.location.row(), message.location.column())
};

writeln!(
writer,
"##vso[task.logissue type=error\
;sourcepath={filename};linenumber={line};columnnumber={col};code={code};]{body}",
filename = message.filename,
code = message.kind.rule().noqa_code(),
body = message.kind.body,
)?;
}

Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::message::tests::{capture_emitter_output, create_messages};
use crate::message::AzureEmitter;
use insta::assert_snapshot;

#[test]
fn output() {
let mut emitter = AzureEmitter::default();
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
}
}
65 changes: 65 additions & 0 deletions crates/ruff/src/message/github.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::fs::relativize_path;
use crate::message::{Emitter, EmitterContext, Message};
use crate::registry::AsRule;
use std::io::Write;

/// Generate error workflow command in GitHub Actions format.
/// See: [GitHub documentation](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-error-message)
#[derive(Default)]
pub struct GithubEmitter;

impl Emitter for GithubEmitter {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My read on the Rust naming rules is that this is correct (over GitHubEmitter).

Copy link
Member Author

@MichaReiser MichaReiser Apr 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. I chose this naming to keep it consistent with SerializationFormat::Github

fn emit(
&mut self,
writer: &mut dyn Write,
messages: &[Message],
context: &EmitterContext,
) -> anyhow::Result<()> {
for message in messages {
let (row, column) = if context.is_jupyter_notebook(&message.filename) {
// We can't give a reasonable location for the structured formats,
// so we show one that's clearly a fallback
(1, 0)
} else {
(message.location.row(), message.location.column())
};

write!(
writer,
"::error title=Ruff \
({code}),file={file},line={row},col={column},endLine={end_row},endColumn={end_column}::",
code = message.kind.rule().noqa_code(),
file = message.filename,
row = message.location.row(),
column = message.location.column(),
end_row = message.end_location.row(),
end_column = message.end_location.column(),
)?;

writeln!(
writer,
"{path}:{row}:{column}: {code} {body}",
path = relativize_path(&message.filename),
code = message.kind.rule().noqa_code(),
body = message.kind.body,
)?;
}

Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::message::tests::{capture_emitter_output, create_messages};
use crate::message::GithubEmitter;
use insta::assert_snapshot;

#[test]
fn output() {
let mut emitter = GithubEmitter::default();
let content = capture_emitter_output(&mut emitter, &create_messages());

assert_snapshot!(content);
}
}
Loading