Skip to content

Commit 46c0937

Browse files
committed
Use fake rules for testing deprecation and removal infrastructure (#9752)
Updates #9689 and #9691 to use rule testing infrastructure from #9747
1 parent e5008ca commit 46c0937

File tree

5 files changed

+235
-83
lines changed

5 files changed

+235
-83
lines changed

crates/ruff/tests/integration_test.rs

Lines changed: 59 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,9 @@ fn nursery_prefix() {
824824
-:1:1: RUF901 [*] Hey this is a stable test rule with a safe fix.
825825
-:1:1: RUF902 Hey this is a stable test rule with an unsafe fix.
826826
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
827-
Found 4 errors.
827+
-:1:1: RUF920 Hey this is a deprecated test rule.
828+
-:1:1: RUF921 Hey this is another deprecated test rule.
829+
Found 6 errors.
828830
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
829831
830832
----- stderr -----
@@ -846,7 +848,9 @@ fn nursery_all() {
846848
-:1:1: RUF901 [*] Hey this is a stable test rule with a safe fix.
847849
-:1:1: RUF902 Hey this is a stable test rule with an unsafe fix.
848850
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
849-
Found 5 errors.
851+
-:1:1: RUF920 Hey this is a deprecated test rule.
852+
-:1:1: RUF921 Hey this is another deprecated test rule.
853+
Found 7 errors.
850854
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
851855
852856
----- stderr -----
@@ -1094,28 +1098,45 @@ fn preview_enabled_group_ignore() {
10941098
#[test]
10951099
fn removed_direct() {
10961100
// Selection of a removed rule should fail
1097-
let mut cmd = RuffCheck::default().args(["--select", "PLR1706"]).build();
1101+
let mut cmd = RuffCheck::default().args(["--select", "RUF931"]).build();
10981102
assert_cmd_snapshot!(cmd, @r###"
10991103
success: false
11001104
exit_code: 2
11011105
----- stdout -----
11021106
11031107
----- stderr -----
11041108
ruff failed
1105-
Cause: Rule `PLR1706` was removed and cannot be selected.
1109+
Cause: Rule `RUF931` was removed and cannot be selected.
1110+
"###);
1111+
}
1112+
1113+
#[test]
1114+
fn removed_direct_multiple() {
1115+
// Selection of multiple removed rule should fail with a message
1116+
// including all the rules
1117+
let mut cmd = RuffCheck::default()
1118+
.args(["--select", "RUF930", "--select", "RUF931"])
1119+
.build();
1120+
assert_cmd_snapshot!(cmd, @r###"
1121+
success: false
1122+
exit_code: 2
1123+
----- stdout -----
1124+
1125+
----- stderr -----
1126+
ruff failed
1127+
Cause: The following rules have been removed and cannot be selected:
1128+
- RUF930
1129+
- RUF931
1130+
11061131
"###);
11071132
}
11081133

11091134
#[test]
11101135
fn removed_indirect() {
11111136
// Selection _including_ a removed rule without matching should not fail
11121137
// nor should the rule be used
1113-
let mut cmd = RuffCheck::default().args(["--select", "PLR"]).build();
1114-
assert_cmd_snapshot!(cmd.pass_stdin(r###"
1115-
# This would have been a PLR1706 violation
1116-
x, y = 1, 2
1117-
maximum = x >= y and x or y
1118-
"""###), @r###"
1138+
let mut cmd = RuffCheck::default().args(["--select", "RUF93"]).build();
1139+
assert_cmd_snapshot!(cmd, @r###"
11191140
success: true
11201141
exit_code: 0
11211142
----- stdout -----
@@ -1128,74 +1149,49 @@ maximum = x >= y and x or y
11281149
fn deprecated_direct() {
11291150
// Selection of a deprecated rule without preview enabled should still work
11301151
// but a warning should be displayed
1131-
let mut cmd = RuffCheck::default().args(["--select", "TRY200"]).build();
1132-
assert_cmd_snapshot!(cmd
1133-
.pass_stdin(r###"
1134-
def reciprocal(n):
1135-
try:
1136-
return 1 / n
1137-
except ZeroDivisionError:
1138-
raise ValueError()
1139-
"###), @r###"
1152+
let mut cmd = RuffCheck::default().args(["--select", "RUF920"]).build();
1153+
assert_cmd_snapshot!(cmd, @r###"
11401154
success: false
11411155
exit_code: 1
11421156
----- stdout -----
1143-
-:6:9: TRY200 Use `raise from` to specify exception cause
1157+
-:1:1: RUF920 Hey this is a deprecated test rule.
11441158
Found 1 error.
11451159
11461160
----- stderr -----
1147-
warning: Rule `TRY200` is deprecated and will be removed in a future release.
1161+
warning: Rule `RUF920` is deprecated and will be removed in a future release.
11481162
"###);
11491163
}
11501164

11511165
#[test]
11521166
fn deprecated_multiple_direct() {
11531167
let mut cmd = RuffCheck::default()
1154-
.args(["--select", "ANN101", "--select", "ANN102"])
1168+
.args(["--select", "RUF920", "--select", "RUF921"])
11551169
.build();
1156-
assert_cmd_snapshot!(cmd
1157-
.pass_stdin(r###"
1158-
class Foo:
1159-
def a(self):
1160-
pass
1161-
1162-
@classmethod
1163-
def b(cls):
1164-
pass
1165-
"###), @r###"
1170+
assert_cmd_snapshot!(cmd, @r###"
11661171
success: false
11671172
exit_code: 1
11681173
----- stdout -----
1169-
-:3:11: ANN101 Missing type annotation for `self` in method
1170-
-:7:11: ANN102 Missing type annotation for `cls` in classmethod
1174+
-:1:1: RUF920 Hey this is a deprecated test rule.
1175+
-:1:1: RUF921 Hey this is another deprecated test rule.
11711176
Found 2 errors.
11721177
11731178
----- stderr -----
1174-
warning: Rule `ANN102` is deprecated and will be removed in a future release.
1175-
warning: Rule `ANN101` is deprecated and will be removed in a future release.
1179+
warning: Rule `RUF921` is deprecated and will be removed in a future release.
1180+
warning: Rule `RUF920` is deprecated and will be removed in a future release.
11761181
"###);
11771182
}
11781183

11791184
#[test]
11801185
fn deprecated_indirect() {
1181-
// `ANN` includes deprecated rules `ANN101` and `ANN102` but should not warn
1186+
// `RUF92` includes deprecated rules but should not warn
11821187
// since it is not a "direct" selection
1183-
let mut cmd = RuffCheck::default().args(["--select", "ANN1"]).build();
1184-
assert_cmd_snapshot!(cmd
1185-
.pass_stdin(r###"
1186-
class Foo:
1187-
def a(self):
1188-
pass
1189-
1190-
@classmethod
1191-
def b(cls):
1192-
pass
1193-
"###), @r###"
1188+
let mut cmd = RuffCheck::default().args(["--select", "RUF92"]).build();
1189+
assert_cmd_snapshot!(cmd, @r###"
11941190
success: false
11951191
exit_code: 1
11961192
----- stdout -----
1197-
-:3:11: ANN101 Missing type annotation for `self` in method
1198-
-:7:11: ANN102 Missing type annotation for `cls` in classmethod
1193+
-:1:1: RUF920 Hey this is a deprecated test rule.
1194+
-:1:1: RUF921 Hey this is another deprecated test rule.
11991195
Found 2 errors.
12001196
12011197
----- stderr -----
@@ -1206,40 +1202,26 @@ class Foo:
12061202
fn deprecated_direct_preview_enabled() {
12071203
// Direct selection of a deprecated rule in preview should fail
12081204
let mut cmd = RuffCheck::default()
1209-
.args(["--select", "TRY200", "--preview"])
1205+
.args(["--select", "RUF920", "--preview"])
12101206
.build();
1211-
assert_cmd_snapshot!(cmd
1212-
.pass_stdin(r###"
1213-
def reciprocal(n):
1214-
try:
1215-
return 1 / n
1216-
except ZeroDivisionError:
1217-
raise ValueError()
1218-
"###), @r###"
1207+
assert_cmd_snapshot!(cmd, @r###"
12191208
success: false
12201209
exit_code: 2
12211210
----- stdout -----
12221211
12231212
----- stderr -----
12241213
ruff failed
1225-
Cause: Selection of deprecated rule `TRY200` is not allowed when preview is enabled.
1214+
Cause: Selection of deprecated rule `RUF920` is not allowed when preview is enabled.
12261215
"###);
12271216
}
12281217

12291218
#[test]
12301219
fn deprecated_indirect_preview_enabled() {
1231-
// `TRY200` is deprecated and should be off by default in preview.
1220+
// `RUF920` is deprecated and should be off by default in preview.
12321221
let mut cmd = RuffCheck::default()
1233-
.args(["--select", "TRY", "--preview"])
1222+
.args(["--select", "RUF92", "--preview"])
12341223
.build();
1235-
assert_cmd_snapshot!(cmd
1236-
.pass_stdin(r###"
1237-
def reciprocal(n):
1238-
try:
1239-
return 1 / n
1240-
except ZeroDivisionError:
1241-
raise ValueError()
1242-
"###), @r###"
1224+
assert_cmd_snapshot!(cmd, @r###"
12431225
success: true
12441226
exit_code: 0
12451227
----- stdout -----
@@ -1253,25 +1235,18 @@ fn deprecated_multiple_direct_preview_enabled() {
12531235
// Direct selection of the deprecated rules in preview should fail with
12541236
// a message listing all of the rule codes
12551237
let mut cmd = RuffCheck::default()
1256-
.args(["--select", "ANN101", "--select", "ANN102", "--preview"])
1238+
.args(["--select", "RUF920", "--select", "RUF921", "--preview"])
12571239
.build();
1258-
assert_cmd_snapshot!(cmd
1259-
.pass_stdin(r###"
1260-
def reciprocal(n):
1261-
try:
1262-
return 1 / n
1263-
except ZeroDivisionError:
1264-
raise ValueError()
1265-
"###), @r###"
1240+
assert_cmd_snapshot!(cmd, @r###"
12661241
success: false
12671242
exit_code: 2
12681243
----- stdout -----
12691244
12701245
----- stderr -----
12711246
ruff failed
12721247
Cause: Selection of deprecated rules is not allowed when preview is enabled. Remove selection of:
1273-
- ANN102
1274-
- ANN101
1248+
- RUF921
1249+
- RUF920
12751250
12761251
"###);
12771252
}
@@ -1793,7 +1768,9 @@ extend-safe-fixes = ["RUF9"]
17931768
-:1:1: RUF901 Hey this is a stable test rule with a safe fix.
17941769
-:1:1: RUF902 [*] Hey this is a stable test rule with an unsafe fix.
17951770
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
1796-
Found 4 errors.
1771+
-:1:1: RUF920 Hey this is a deprecated test rule.
1772+
-:1:1: RUF921 Hey this is another deprecated test rule.
1773+
Found 6 errors.
17971774
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
17981775
17991776
----- stderr -----

crates/ruff_linter/src/codes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,14 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
951951
#[cfg(feature = "test-rules")]
952952
#[allow(deprecated)]
953953
(Ruff, "912") => (RuleGroup::Nursery, rules::ruff::rules::NurseryTestRule),
954+
#[cfg(feature = "test-rules")]
955+
(Ruff, "920") => (RuleGroup::Deprecated, rules::ruff::rules::DeprecatedTestRule),
956+
#[cfg(feature = "test-rules")]
957+
(Ruff, "921") => (RuleGroup::Deprecated, rules::ruff::rules::AnotherDeprecatedTestRule),
958+
#[cfg(feature = "test-rules")]
959+
(Ruff, "930") => (RuleGroup::Removed, rules::ruff::rules::RemovedTestRule),
960+
#[cfg(feature = "test-rules")]
961+
(Ruff, "931") => (RuleGroup::Removed, rules::ruff::rules::AnotherRemovedTestRule),
954962

955963

956964
// flake8-django

crates/ruff_linter/src/linter.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ pub fn check_path(
236236
}
237237
Rule::NurseryTestRule => test_rules::NurseryTestRule::diagnostic(locator, indexer),
238238
Rule::PreviewTestRule => test_rules::PreviewTestRule::diagnostic(locator, indexer),
239+
Rule::DeprecatedTestRule => {
240+
test_rules::DeprecatedTestRule::diagnostic(locator, indexer)
241+
}
242+
Rule::AnotherDeprecatedTestRule => {
243+
test_rules::AnotherDeprecatedTestRule::diagnostic(locator, indexer)
244+
}
245+
Rule::RemovedTestRule => test_rules::RemovedTestRule::diagnostic(locator, indexer),
246+
Rule::AnotherRemovedTestRule => {
247+
test_rules::AnotherRemovedTestRule::diagnostic(locator, indexer)
248+
}
239249
_ => unreachable!("All test rules must have an implementation"),
240250
};
241251
if let Some(diagnostic) = diagnostic {

0 commit comments

Comments
 (0)