Skip to content

Commit ba2dfb8

Browse files
InSyncWithFooMichaReiser
authored andcommitted
Add missing unit tests for # noqa: A-like cases (#16677)
## Summary Follow-up to #16659. This change adds tests for these three cases, which are (also) not covered by existing tests: * `# noqa: A` (lone incomplete code) * `# noqa: A123, B` (complete codes, last one incomplete) * `# noqa: A123B` (squashed codes, last one incomplete)
1 parent df9cf0e commit ba2dfb8

10 files changed

+180
-0
lines changed

crates/ruff_linter/src/noqa.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,30 @@ mod tests {
12741274
assert_lexed_ranges_match_slices(directive, source);
12751275
}
12761276

1277+
#[test]
1278+
fn malformed_code_1() {
1279+
let source = "# noqa: F";
1280+
let directive = lex_inline_noqa(TextRange::up_to(source.text_len()), source);
1281+
assert_debug_snapshot!(directive);
1282+
assert_lexed_ranges_match_slices(directive, source);
1283+
}
1284+
1285+
#[test]
1286+
fn malformed_code_2() {
1287+
let source = "# noqa: RUF001, F";
1288+
let directive = lex_inline_noqa(TextRange::up_to(source.text_len()), source);
1289+
assert_debug_snapshot!(directive);
1290+
assert_lexed_ranges_match_slices(directive, source);
1291+
}
1292+
1293+
#[test]
1294+
fn malformed_code_3() {
1295+
let source = "# noqa: RUF001F";
1296+
let directive = lex_inline_noqa(TextRange::up_to(source.text_len()), source);
1297+
assert_debug_snapshot!(directive);
1298+
assert_lexed_ranges_match_slices(directive, source);
1299+
}
1300+
12771301
#[test]
12781302
fn noqa_code() {
12791303
let source = "# noqa: F401";
@@ -1530,6 +1554,30 @@ mod tests {
15301554
assert_lexed_ranges_match_slices(exemption, source);
15311555
}
15321556

1557+
#[test]
1558+
fn flake8_malformed_code_1() {
1559+
let source = "# flake8: noqa: F";
1560+
let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source);
1561+
assert_debug_snapshot!(directive);
1562+
assert_lexed_ranges_match_slices(directive, source);
1563+
}
1564+
1565+
#[test]
1566+
fn flake8_malformed_code_2() {
1567+
let source = "# flake8: noqa: RUF001, F";
1568+
let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source);
1569+
assert_debug_snapshot!(directive);
1570+
assert_lexed_ranges_match_slices(directive, source);
1571+
}
1572+
1573+
#[test]
1574+
fn flake8_malformed_code_3() {
1575+
let source = "# flake8: noqa: RUF001F";
1576+
let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source);
1577+
assert_debug_snapshot!(directive);
1578+
assert_lexed_ranges_match_slices(directive, source);
1579+
}
1580+
15331581
#[test]
15341582
fn ruff_exemption_all() {
15351583
let source = "# ruff: noqa";
@@ -1562,6 +1610,30 @@ mod tests {
15621610
assert_lexed_ranges_match_slices(exemption, source);
15631611
}
15641612

1613+
#[test]
1614+
fn ruff_malformed_code_1() {
1615+
let source = "# ruff: noqa: F";
1616+
let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source);
1617+
assert_debug_snapshot!(directive);
1618+
assert_lexed_ranges_match_slices(directive, source);
1619+
}
1620+
1621+
#[test]
1622+
fn ruff_malformed_code_2() {
1623+
let source = "# ruff: noqa: RUF001, F";
1624+
let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source);
1625+
assert_debug_snapshot!(directive);
1626+
assert_lexed_ranges_match_slices(directive, source);
1627+
}
1628+
1629+
#[test]
1630+
fn ruff_malformed_code_3() {
1631+
let source = "# ruff: noqa: RUF001F";
1632+
let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source);
1633+
assert_debug_snapshot!(directive);
1634+
assert_lexed_ranges_match_slices(directive, source);
1635+
}
1636+
15651637
#[test]
15661638
fn flake8_exemption_all_no_space() {
15671639
let source = "#flake8:noqa";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Err(
6+
MissingCodes,
7+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Ok(
6+
Some(
7+
NoqaLexerOutput {
8+
warnings: [],
9+
directive: Codes(
10+
Codes {
11+
range: 0..22,
12+
codes: [
13+
Code {
14+
code: "RUF001",
15+
range: 16..22,
16+
},
17+
],
18+
},
19+
),
20+
},
21+
),
22+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Err(
6+
InvalidCodeSuffix,
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Err(
6+
MissingCodes,
7+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Ok(
6+
Some(
7+
NoqaLexerOutput {
8+
warnings: [],
9+
directive: Codes(
10+
Codes {
11+
range: 0..14,
12+
codes: [
13+
Code {
14+
code: "RUF001",
15+
range: 8..14,
16+
},
17+
],
18+
},
19+
),
20+
},
21+
),
22+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Err(
6+
InvalidCodeSuffix,
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Err(
6+
MissingCodes,
7+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Ok(
6+
Some(
7+
NoqaLexerOutput {
8+
warnings: [],
9+
directive: Codes(
10+
Codes {
11+
range: 0..20,
12+
codes: [
13+
Code {
14+
code: "RUF001",
15+
range: 14..20,
16+
},
17+
],
18+
},
19+
),
20+
},
21+
),
22+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
source: crates/ruff_linter/src/noqa.rs
3+
expression: directive
4+
---
5+
Err(
6+
InvalidCodeSuffix,
7+
)

0 commit comments

Comments
 (0)