From cdb9fda3b8f91451113ad1028edeb4619fad3b55 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 5 Jul 2023 17:49:07 -0400 Subject: [PATCH] Add debug-based snapshot tests for noqa directive parsing (#5535) ## Summary Better tests, helpful for future refactors. --- crates/ruff/src/noqa.rs | 129 ++++++++++++++++-- .../ruff__noqa__tests__noqa_all.snap | 11 ++ ...oqa__tests__noqa_all_case_insensitive.snap | 11 ++ ...ff__noqa__tests__noqa_all_multi_space.snap | 5 + .../ruff__noqa__tests__noqa_all_no_space.snap | 5 + .../ruff__noqa__tests__noqa_code.snap | 14 ++ ...qa__tests__noqa_code_case_insensitive.snap | 14 ++ ...f__noqa__tests__noqa_code_multi_space.snap | 5 + ...ruff__noqa__tests__noqa_code_no_space.snap | 5 + .../ruff__noqa__tests__noqa_codes.snap | 15 ++ ...a__tests__noqa_codes_case_insensitive.snap | 15 ++ ...__noqa__tests__noqa_codes_multi_space.snap | 5 + ...uff__noqa__tests__noqa_codes_no_space.snap | 5 + ...ruff__noqa__tests__noqa_leading_space.snap | 14 ++ ...uff__noqa__tests__noqa_trailing_space.snap | 14 ++ 15 files changed, 252 insertions(+), 15 deletions(-) create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_case_insensitive.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_multi_space.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_no_space.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_case_insensitive.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_multi_space.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_no_space.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_case_insensitive.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_multi_space.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_no_space.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_leading_space.snap create mode 100644 crates/ruff/src/snapshots/ruff__noqa__tests__noqa_trailing_space.snap diff --git a/crates/ruff/src/noqa.rs b/crates/ruff/src/noqa.rs index 3d1422f675..64f953811c 100644 --- a/crates/ruff/src/noqa.rs +++ b/crates/ruff/src/noqa.rs @@ -20,7 +20,7 @@ use crate::rule_redirects::get_redirect_target; static NOQA_LINE_REGEX: Lazy = Lazy::new(|| { Regex::new( - r"(?P\s*)(?P(?i:# noqa)(?::\s?(?P(?:[A-Z]+[0-9]+)(?:[,\s]+[A-Z]+[0-9]+)*))?)(?P\s*)", + r"(?P\s*)(?P(?i:# noqa)(?::\s?(?P[A-Z]+[0-9]+(?:[,\s]+[A-Z]+[0-9]+)*))?)(?P\s*)", ) .unwrap() }); @@ -566,28 +566,127 @@ impl FromIterator for NoqaMapping { #[cfg(test)] mod tests { + use insta::assert_debug_snapshot; use ruff_text_size::{TextRange, TextSize}; use ruff_diagnostics::Diagnostic; use ruff_python_ast::source_code::Locator; 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::pyflakes; + use crate::rules::pyflakes::rules::UnusedVariable; #[test] - fn regex() { - assert!(NOQA_LINE_REGEX.is_match("# noqa")); - assert!(NOQA_LINE_REGEX.is_match("# NoQA")); + fn noqa_all() { + 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)); + } - assert!(NOQA_LINE_REGEX.is_match("# noqa: F401")); - assert!(NOQA_LINE_REGEX.is_match("# NoQA: F401")); - assert!(NOQA_LINE_REGEX.is_match("# noqa: F401, E501")); + #[test] + fn noqa_code() { + 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")); - assert!(NOQA_LINE_REGEX.is_match("# NoQA:F401")); - assert!(NOQA_LINE_REGEX.is_match("# noqa:F401, E501")); + #[test] + fn noqa_codes() { + 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] @@ -605,7 +704,7 @@ mod tests { assert_eq!(output, format!("{contents}")); let diagnostics = [Diagnostic::new( - pyflakes::rules::UnusedVariable { + UnusedVariable { name: "x".to_string(), }, TextRange::new(TextSize::from(0), TextSize::from(0)), @@ -629,7 +728,7 @@ mod tests { TextRange::new(TextSize::from(0), TextSize::from(0)), ), Diagnostic::new( - pyflakes::rules::UnusedVariable { + UnusedVariable { name: "x".to_string(), }, TextRange::new(TextSize::from(0), TextSize::from(0)), @@ -653,7 +752,7 @@ mod tests { TextRange::new(TextSize::from(0), TextSize::from(0)), ), Diagnostic::new( - pyflakes::rules::UnusedVariable { + UnusedVariable { name: "x".to_string(), }, TextRange::new(TextSize::from(0), TextSize::from(0)), diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all.snap new file mode 100644 index 0000000000..d87458d852 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all.snap @@ -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, + }, +) diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_case_insensitive.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_case_insensitive.snap new file mode 100644 index 0000000000..d87458d852 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_case_insensitive.snap @@ -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, + }, +) diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_multi_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_multi_space.snap new file mode 100644 index 0000000000..57be5a0532 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_multi_space.snap @@ -0,0 +1,5 @@ +--- +source: crates/ruff/src/noqa.rs +expression: "Directive::extract(range, &locator)" +--- +None diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_no_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_no_space.snap new file mode 100644 index 0000000000..57be5a0532 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_all_no_space.snap @@ -0,0 +1,5 @@ +--- +source: crates/ruff/src/noqa.rs +expression: "Directive::extract(range, &locator)" +--- +None diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code.snap new file mode 100644 index 0000000000..4620f00ce0 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code.snap @@ -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", + ], + }, +) diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_case_insensitive.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_case_insensitive.snap new file mode 100644 index 0000000000..4620f00ce0 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_case_insensitive.snap @@ -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", + ], + }, +) diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_multi_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_multi_space.snap new file mode 100644 index 0000000000..57be5a0532 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_multi_space.snap @@ -0,0 +1,5 @@ +--- +source: crates/ruff/src/noqa.rs +expression: "Directive::extract(range, &locator)" +--- +None diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_no_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_no_space.snap new file mode 100644 index 0000000000..57be5a0532 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_code_no_space.snap @@ -0,0 +1,5 @@ +--- +source: crates/ruff/src/noqa.rs +expression: "Directive::extract(range, &locator)" +--- +None diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes.snap new file mode 100644 index 0000000000..4b2854c1f6 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes.snap @@ -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", + ], + }, +) diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_case_insensitive.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_case_insensitive.snap new file mode 100644 index 0000000000..4b2854c1f6 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_case_insensitive.snap @@ -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", + ], + }, +) diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_multi_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_multi_space.snap new file mode 100644 index 0000000000..57be5a0532 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_multi_space.snap @@ -0,0 +1,5 @@ +--- +source: crates/ruff/src/noqa.rs +expression: "Directive::extract(range, &locator)" +--- +None diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_no_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_no_space.snap new file mode 100644 index 0000000000..57be5a0532 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_codes_no_space.snap @@ -0,0 +1,5 @@ +--- +source: crates/ruff/src/noqa.rs +expression: "Directive::extract(range, &locator)" +--- +None diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_leading_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_leading_space.snap new file mode 100644 index 0000000000..cef106a235 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_leading_space.snap @@ -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", + ], + }, +) diff --git a/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_trailing_space.snap b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_trailing_space.snap new file mode 100644 index 0000000000..54e666a3c4 --- /dev/null +++ b/crates/ruff/src/snapshots/ruff__noqa__tests__noqa_trailing_space.snap @@ -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", + ], + }, +)