Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jul 29, 2023
1 parent b6a7a0e commit a8dd376
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 270 deletions.
5 changes: 5 additions & 0 deletions crates/ruff/resources/test/fixtures/ruff/ruff_noqa_invalid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os # ruff: noqa: F401


def f():
x = 1
14 changes: 4 additions & 10 deletions crates/ruff/src/noqa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ruff_python_ast::Ranged;
use ruff_text_size::{TextLen, TextRange, TextSize};

use ruff_diagnostics::Diagnostic;
use ruff_python_trivia::indentation_at_offset;
use ruff_source_file::{LineEnding, Locator};

use crate::codes::NoqaCode;
Expand Down Expand Up @@ -249,18 +250,11 @@ impl FileExemption {
warn!("Invalid `# ruff: noqa` directive at {path_display}:{line}: {err}");
}
Ok(Some(exemption)) => {
if !locator
.slice(TextRange::new(
locator.line_start(range.start()),
range.start(),
))
.chars()
.all(char::is_whitespace)
{
if indentation_at_offset(range.start(), locator).is_none() {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
let path_display = relativize_path(path);
warn!("Unexpected end-of-line `# ruff: noqa` directive at {path_display}:{line}");
warn!("Unexpected `# ruff: noqa` directive at {path_display}:{line}. File-level suppression comments must appear on their own line.");
continue;
}

Expand All @@ -277,7 +271,7 @@ impl FileExemption {
#[allow(deprecated)]
let line = locator.compute_line_index(range.start());
let path_display = relativize_path(path);
warn!("Invalid code provided to `# ruff: noqa` at {path_display}:{line}: {code}");
warn!("Invalid rule code provided to `# ruff: noqa` at {path_display}:{line}: {code}");
None
}
}));
Expand Down
18 changes: 14 additions & 4 deletions crates/ruff/src/rules/ruff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,29 @@ mod tests {
}

#[test]
fn ruff_noqa() -> Result<()> {
fn ruff_noqa_all() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/ruff_noqa.py"),
Path::new("ruff/ruff_noqa_all.py"),
&settings::Settings::for_rules(vec![Rule::UnusedImport, Rule::UnusedVariable]),
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn ruff_targeted_noqa() -> Result<()> {
fn ruff_noqa_codes() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/ruff_targeted_noqa.py"),
Path::new("ruff/ruff_noqa_codes.py"),
&settings::Settings::for_rules(vec![Rule::UnusedImport, Rule::UnusedVariable]),
)?;
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn ruff_noqa_invalid() -> Result<()> {
let diagnostics = test_path(
Path::new("ruff/ruff_noqa_invalid.py"),
&settings::Settings::for_rules(vec![Rule::UnusedImport, Rule::UnusedVariable]),
)?;
assert_messages!(diagnostics);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff/src/rules/ruff/mod.rs
---
ruff_targeted_noqa.py:8:5: F841 [*] Local variable `x` is assigned to but never used
ruff_noqa_codes.py:8:5: F841 [*] Local variable `x` is assigned to but never used
|
7 | def f():
8 | x = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/ruff/src/rules/ruff/mod.rs
---
ruff_noqa_invalid.py:1:8: F401 [*] `os` imported but unused
|
1 | import os # ruff: noqa: F401
| ^^ F401
|
= help: Remove unused import: `os`

Fix
1 |-import os # ruff: noqa: F401
2 1 |
3 2 |
4 3 | def f():

ruff_noqa_invalid.py:5:5: F841 [*] Local variable `x` is assigned to but never used
|
4 | def f():
5 | x = 1
| ^ F841
|
= help: Remove assignment to unused variable `x`

Suggested fix
2 2 |
3 3 |
4 4 | def f():
5 |- x = 1
5 |+ pass


2 changes: 1 addition & 1 deletion crates/ruff_python_ast/src/whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn indentation<'a, T>(locator: &'a Locator, located: &T) -> Option<&'a str>
where
T: Ranged,
{
indentation_at_offset(locator, located.start())
indentation_at_offset(located.start(), locator)
}

/// Return the end offset at which the empty lines following a statement.
Expand Down
Loading

0 comments on commit a8dd376

Please sign in to comment.