Skip to content

Commit 0d752e5

Browse files
committed
Add tests for redirected rules (#9754)
Extends #9752 adding internal test rules for redirection Fixes a bug where we did not see warnings for exact codes that are redirected (just prefixes)
1 parent 46c0937 commit 0d752e5

File tree

6 files changed

+195
-6
lines changed

6 files changed

+195
-6
lines changed

crates/ruff/tests/integration_test.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,8 @@ fn nursery_prefix() {
826826
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
827827
-:1:1: RUF920 Hey this is a deprecated test rule.
828828
-:1:1: RUF921 Hey this is another deprecated test rule.
829-
Found 6 errors.
829+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
830+
Found 7 errors.
830831
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
831832
832833
----- stderr -----
@@ -850,7 +851,8 @@ fn nursery_all() {
850851
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
851852
-:1:1: RUF920 Hey this is a deprecated test rule.
852853
-:1:1: RUF921 Hey this is another deprecated test rule.
853-
Found 7 errors.
854+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
855+
Found 8 errors.
854856
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
855857
856858
----- stderr -----
@@ -929,7 +931,8 @@ fn preview_enabled_prefix() {
929931
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
930932
-:1:1: RUF911 Hey this is a preview test rule.
931933
-:1:1: RUF912 Hey this is a nursery test rule.
932-
Found 6 errors.
934+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
935+
Found 7 errors.
933936
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
934937
935938
----- stderr -----
@@ -953,7 +956,8 @@ fn preview_enabled_all() {
953956
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
954957
-:1:1: RUF911 Hey this is a preview test rule.
955958
-:1:1: RUF912 Hey this is a nursery test rule.
956-
Found 8 errors.
959+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
960+
Found 9 errors.
957961
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
958962
959963
----- stderr -----
@@ -1088,7 +1092,8 @@ fn preview_enabled_group_ignore() {
10881092
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
10891093
-:1:1: RUF911 Hey this is a preview test rule.
10901094
-:1:1: RUF912 Hey this is a nursery test rule.
1091-
Found 6 errors.
1095+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
1096+
Found 7 errors.
10921097
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
10931098
10941099
----- stderr -----
@@ -1145,6 +1150,53 @@ fn removed_indirect() {
11451150
"###);
11461151
}
11471152

1153+
#[test]
1154+
fn redirect_direct() {
1155+
// Selection of a redirected rule directly should use the new rule and warn
1156+
let mut cmd = RuffCheck::default().args(["--select", "RUF940"]).build();
1157+
assert_cmd_snapshot!(cmd, @r###"
1158+
success: false
1159+
exit_code: 1
1160+
----- stdout -----
1161+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
1162+
Found 1 error.
1163+
1164+
----- stderr -----
1165+
warning: `RUF940` has been remapped to `RUF950`.
1166+
"###);
1167+
}
1168+
1169+
#[test]
1170+
fn redirect_indirect() {
1171+
// Selection _including_ a redirected rule without matching should not fail
1172+
// nor should the rule be used
1173+
let mut cmd = RuffCheck::default().args(["--select", "RUF94"]).build();
1174+
assert_cmd_snapshot!(cmd, @r###"
1175+
success: true
1176+
exit_code: 0
1177+
----- stdout -----
1178+
1179+
----- stderr -----
1180+
"###);
1181+
}
1182+
1183+
#[test]
1184+
fn redirect_prefix() {
1185+
// Selection using a redirected prefix should switch to all rules in the
1186+
// new prefix
1187+
let mut cmd = RuffCheck::default().args(["--select", "RUF96"]).build();
1188+
assert_cmd_snapshot!(cmd, @r###"
1189+
success: false
1190+
exit_code: 1
1191+
----- stdout -----
1192+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
1193+
Found 1 error.
1194+
1195+
----- stderr -----
1196+
warning: `RUF96` has been remapped to `RUF95`.
1197+
"###);
1198+
}
1199+
11481200
#[test]
11491201
fn deprecated_direct() {
11501202
// Selection of a deprecated rule without preview enabled should still work
@@ -1770,7 +1822,8 @@ extend-safe-fixes = ["RUF9"]
17701822
-:1:1: RUF903 Hey this is a stable test rule with a display only fix.
17711823
-:1:1: RUF920 Hey this is a deprecated test rule.
17721824
-:1:1: RUF921 Hey this is another deprecated test rule.
1773-
Found 6 errors.
1825+
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
1826+
Found 7 errors.
17741827
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).
17751828
17761829
----- stderr -----

crates/ruff_linter/src/codes.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,12 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
959959
(Ruff, "930") => (RuleGroup::Removed, rules::ruff::rules::RemovedTestRule),
960960
#[cfg(feature = "test-rules")]
961961
(Ruff, "931") => (RuleGroup::Removed, rules::ruff::rules::AnotherRemovedTestRule),
962+
#[cfg(feature = "test-rules")]
963+
(Ruff, "940") => (RuleGroup::Removed, rules::ruff::rules::RedirectedFromTestRule),
964+
#[cfg(feature = "test-rules")]
965+
(Ruff, "950") => (RuleGroup::Stable, rules::ruff::rules::RedirectedToTestRule),
966+
#[cfg(feature = "test-rules")]
967+
(Ruff, "960") => (RuleGroup::Removed, rules::ruff::rules::RedirectedFromPrefixTestRule),
962968

963969

964970
// flake8-django

crates/ruff_linter/src/linter.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ pub fn check_path(
246246
Rule::AnotherRemovedTestRule => {
247247
test_rules::AnotherRemovedTestRule::diagnostic(locator, indexer)
248248
}
249+
Rule::RedirectedToTestRule => {
250+
test_rules::RedirectedToTestRule::diagnostic(locator, indexer)
251+
}
252+
Rule::RedirectedFromTestRule => {
253+
test_rules::RedirectedFromTestRule::diagnostic(locator, indexer)
254+
}
255+
Rule::RedirectedFromPrefixTestRule => {
256+
test_rules::RedirectedFromPrefixTestRule::diagnostic(locator, indexer)
257+
}
249258
_ => unreachable!("All test rules must have an implementation"),
250259
};
251260
if let Some(diagnostic) = diagnostic {

crates/ruff_linter/src/rule_redirects.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,11 @@ static REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
100100
("T004", "FIX004"),
101101
("RUF011", "B035"),
102102
("TCH006", "TCH010"),
103+
// Test redirect by exact code
104+
#[cfg(feature = "test-rules")]
105+
("RUF940", "RUF950"),
106+
// Test redirect by prefix
107+
#[cfg(feature = "test-rules")]
108+
("RUF96", "RUF95"),
103109
])
104110
});

crates/ruff_linter/src/rules/ruff/rules/test_rules.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub(crate) const TEST_RULES: &[Rule] = &[
4343
Rule::AnotherDeprecatedTestRule,
4444
Rule::RemovedTestRule,
4545
Rule::AnotherRemovedTestRule,
46+
Rule::RedirectedFromTestRule,
47+
Rule::RedirectedToTestRule,
48+
Rule::RedirectedFromPrefixTestRule,
4649
];
4750

4851
pub(crate) trait TestRule {
@@ -438,3 +441,111 @@ impl TestRule for AnotherRemovedTestRule {
438441
))
439442
}
440443
}
444+
445+
/// ## What it does
446+
/// Fake rule for testing.
447+
///
448+
/// ## Why is this bad?
449+
/// Tests must pass!
450+
///
451+
/// ## Example
452+
/// ```python
453+
/// foo
454+
/// ```
455+
///
456+
/// Use instead:
457+
/// ```python
458+
/// bar
459+
/// ```
460+
#[violation]
461+
pub struct RedirectedFromTestRule;
462+
463+
impl Violation for RedirectedFromTestRule {
464+
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;
465+
466+
#[derive_message_formats]
467+
fn message(&self) -> String {
468+
format!("Hey this is a test rule that was redirected to another.")
469+
}
470+
}
471+
472+
impl TestRule for RedirectedFromTestRule {
473+
fn diagnostic(_locator: &Locator, _indexer: &Indexer) -> Option<Diagnostic> {
474+
Some(Diagnostic::new(
475+
RedirectedFromTestRule,
476+
ruff_text_size::TextRange::default(),
477+
))
478+
}
479+
}
480+
481+
/// ## What it does
482+
/// Fake rule for testing.
483+
///
484+
/// ## Why is this bad?
485+
/// Tests must pass!
486+
///
487+
/// ## Example
488+
/// ```python
489+
/// foo
490+
/// ```
491+
///
492+
/// Use instead:
493+
/// ```python
494+
/// bar
495+
/// ```
496+
#[violation]
497+
pub struct RedirectedToTestRule;
498+
499+
impl Violation for RedirectedToTestRule {
500+
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;
501+
502+
#[derive_message_formats]
503+
fn message(&self) -> String {
504+
format!("Hey this is a test rule that was redirected from another.")
505+
}
506+
}
507+
508+
impl TestRule for RedirectedToTestRule {
509+
fn diagnostic(_locator: &Locator, _indexer: &Indexer) -> Option<Diagnostic> {
510+
Some(Diagnostic::new(
511+
RedirectedToTestRule,
512+
ruff_text_size::TextRange::default(),
513+
))
514+
}
515+
}
516+
517+
/// ## What it does
518+
/// Fake rule for testing.
519+
///
520+
/// ## Why is this bad?
521+
/// Tests must pass!
522+
///
523+
/// ## Example
524+
/// ```python
525+
/// foo
526+
/// ```
527+
///
528+
/// Use instead:
529+
/// ```python
530+
/// bar
531+
/// ```
532+
#[violation]
533+
pub struct RedirectedFromPrefixTestRule;
534+
535+
impl Violation for RedirectedFromPrefixTestRule {
536+
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;
537+
538+
#[derive_message_formats]
539+
fn message(&self) -> String {
540+
format!("Hey this is a test rule that was redirected to another by prefix.")
541+
}
542+
}
543+
544+
impl TestRule for RedirectedFromPrefixTestRule {
545+
fn diagnostic(_locator: &Locator, _indexer: &Indexer) -> Option<Diagnostic> {
546+
Some(Diagnostic::new(
547+
RedirectedFromPrefixTestRule,
548+
ruff_text_size::TextRange::default(),
549+
))
550+
}
551+
}

crates/ruff_workspace/src/configuration.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,10 @@ impl LintConfiguration {
940940
if let RuleSelector::Prefix {
941941
prefix,
942942
redirected_from: Some(redirect_from),
943+
}
944+
| RuleSelector::Rule {
945+
prefix,
946+
redirected_from: Some(redirect_from),
943947
} = selector
944948
{
945949
redirects.insert(redirect_from, prefix);

0 commit comments

Comments
 (0)