mirror of https://github.com/astral-sh/ruff
Add debug-based snapshot tests for noqa directive parsing (#5535)
## Summary Better tests, helpful for future refactors.
This commit is contained in:
parent
a0c0b74b6d
commit
cdb9fda3b8
|
|
@ -20,7 +20,7 @@ use crate::rule_redirects::get_redirect_target;
|
||||||
|
|
||||||
static NOQA_LINE_REGEX: Lazy<Regex> = Lazy::new(|| {
|
static NOQA_LINE_REGEX: Lazy<Regex> = Lazy::new(|| {
|
||||||
Regex::new(
|
Regex::new(
|
||||||
r"(?P<leading_spaces>\s*)(?P<noqa>(?i:# noqa)(?::\s?(?P<codes>(?:[A-Z]+[0-9]+)(?:[,\s]+[A-Z]+[0-9]+)*))?)(?P<trailing_spaces>\s*)",
|
r"(?P<leading_spaces>\s*)(?P<noqa>(?i:# noqa)(?::\s?(?P<codes>[A-Z]+[0-9]+(?:[,\s]+[A-Z]+[0-9]+)*))?)(?P<trailing_spaces>\s*)",
|
||||||
)
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
});
|
});
|
||||||
|
|
@ -566,28 +566,127 @@ impl FromIterator<TextRange> for NoqaMapping {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use insta::assert_debug_snapshot;
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
|
|
||||||
use ruff_diagnostics::Diagnostic;
|
use ruff_diagnostics::Diagnostic;
|
||||||
use ruff_python_ast::source_code::Locator;
|
use ruff_python_ast::source_code::Locator;
|
||||||
use ruff_python_whitespace::LineEnding;
|
use ruff_python_whitespace::LineEnding;
|
||||||
|
|
||||||
use crate::noqa::{add_noqa_inner, NoqaMapping, NOQA_LINE_REGEX};
|
use crate::noqa::{add_noqa_inner, Directive, NoqaMapping};
|
||||||
use crate::rules::pycodestyle::rules::AmbiguousVariableName;
|
use crate::rules::pycodestyle::rules::AmbiguousVariableName;
|
||||||
use crate::rules::pyflakes;
|
use crate::rules::pyflakes::rules::UnusedVariable;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn regex() {
|
fn noqa_all() {
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# noqa"));
|
let source = "# noqa";
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# NoQA"));
|
let range = TextRange::new(TextSize::from(0), TextSize::from(6));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# noqa: F401"));
|
#[test]
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# NoQA: F401"));
|
fn noqa_code() {
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# noqa: F401, E501"));
|
let source = "# noqa: F401";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(12));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# noqa:F401"));
|
#[test]
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# NoQA:F401"));
|
fn noqa_codes() {
|
||||||
assert!(NOQA_LINE_REGEX.is_match("# noqa:F401, E501"));
|
let source = "# noqa: F401, F841";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(18));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_all_case_insensitive() {
|
||||||
|
let source = "# NOQA";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(6));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_code_case_insensitive() {
|
||||||
|
let source = "# NOQA: F401";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(12));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_codes_case_insensitive() {
|
||||||
|
let source = "# NOQA: F401, F841";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(18));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_leading_space() {
|
||||||
|
let source = "# # noqa: F401";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(16));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_trailing_space() {
|
||||||
|
let source = "# noqa: F401 #";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(16));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_all_no_space() {
|
||||||
|
let source = "#noqa";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(5));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_code_no_space() {
|
||||||
|
let source = "#noqa:F401";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(10));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_codes_no_space() {
|
||||||
|
let source = "#noqa:F401,F841";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(15));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_all_multi_space() {
|
||||||
|
let source = "# noqa";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(7));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_code_multi_space() {
|
||||||
|
let source = "# noqa: F401";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(13));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn noqa_codes_multi_space() {
|
||||||
|
let source = "# noqa: F401, F841";
|
||||||
|
let range = TextRange::new(TextSize::from(0), TextSize::from(20));
|
||||||
|
let locator = Locator::new(source);
|
||||||
|
assert_debug_snapshot!(Directive::extract(range, &locator));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
@ -605,7 +704,7 @@ mod tests {
|
||||||
assert_eq!(output, format!("{contents}"));
|
assert_eq!(output, format!("{contents}"));
|
||||||
|
|
||||||
let diagnostics = [Diagnostic::new(
|
let diagnostics = [Diagnostic::new(
|
||||||
pyflakes::rules::UnusedVariable {
|
UnusedVariable {
|
||||||
name: "x".to_string(),
|
name: "x".to_string(),
|
||||||
},
|
},
|
||||||
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
||||||
|
|
@ -629,7 +728,7 @@ mod tests {
|
||||||
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
||||||
),
|
),
|
||||||
Diagnostic::new(
|
Diagnostic::new(
|
||||||
pyflakes::rules::UnusedVariable {
|
UnusedVariable {
|
||||||
name: "x".to_string(),
|
name: "x".to_string(),
|
||||||
},
|
},
|
||||||
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
||||||
|
|
@ -653,7 +752,7 @@ mod tests {
|
||||||
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
||||||
),
|
),
|
||||||
Diagnostic::new(
|
Diagnostic::new(
|
||||||
pyflakes::rules::UnusedVariable {
|
UnusedVariable {
|
||||||
name: "x".to_string(),
|
name: "x".to_string(),
|
||||||
},
|
},
|
||||||
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
TextRange::new(TextSize::from(0), TextSize::from(0)),
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
All(
|
||||||
|
All {
|
||||||
|
leading_space_len: 0,
|
||||||
|
noqa_range: 0..6,
|
||||||
|
trailing_space_len: 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
All(
|
||||||
|
All {
|
||||||
|
leading_space_len: 0,
|
||||||
|
noqa_range: 0..6,
|
||||||
|
trailing_space_len: 0,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
None
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
None
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
Codes(
|
||||||
|
Codes {
|
||||||
|
leading_space_len: 0,
|
||||||
|
noqa_range: 0..12,
|
||||||
|
trailing_space_len: 0,
|
||||||
|
codes: [
|
||||||
|
"F401",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
Codes(
|
||||||
|
Codes {
|
||||||
|
leading_space_len: 0,
|
||||||
|
noqa_range: 0..12,
|
||||||
|
trailing_space_len: 0,
|
||||||
|
codes: [
|
||||||
|
"F401",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
None
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
None
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
Codes(
|
||||||
|
Codes {
|
||||||
|
leading_space_len: 0,
|
||||||
|
noqa_range: 0..18,
|
||||||
|
trailing_space_len: 0,
|
||||||
|
codes: [
|
||||||
|
"F401",
|
||||||
|
"F841",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
Codes(
|
||||||
|
Codes {
|
||||||
|
leading_space_len: 0,
|
||||||
|
noqa_range: 0..18,
|
||||||
|
trailing_space_len: 0,
|
||||||
|
codes: [
|
||||||
|
"F401",
|
||||||
|
"F841",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
None
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
None
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
Codes(
|
||||||
|
Codes {
|
||||||
|
leading_space_len: 3,
|
||||||
|
noqa_range: 4..16,
|
||||||
|
trailing_space_len: 0,
|
||||||
|
codes: [
|
||||||
|
"F401",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff/src/noqa.rs
|
||||||
|
expression: "Directive::extract(range, &locator)"
|
||||||
|
---
|
||||||
|
Codes(
|
||||||
|
Codes {
|
||||||
|
leading_space_len: 0,
|
||||||
|
noqa_range: 0..12,
|
||||||
|
trailing_space_len: 3,
|
||||||
|
codes: [
|
||||||
|
"F401",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue