Skip to content

Commit

Permalink
Add test for redirected prefix
Browse files Browse the repository at this point in the history
# Conflicts:
#	crates/ruff_linter/src/rule_redirects.rs
  • Loading branch information
zanieb committed Feb 1, 2024
1 parent efa88d0 commit 8d4d156
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
17 changes: 17 additions & 0 deletions crates/ruff/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,23 @@ fn redirect_indirect() {
"###);
}

#[test]
fn redirect_prefix() {
// Selection using a redirected prefix should switch to all rules in the
// new prefix
let mut cmd = RuffCheck::default().args(["--select", "RUF96"]).build();
assert_cmd_snapshot!(cmd, @r###"
success: false
exit_code: 1
----- stdout -----
-:1:1: RUF950 Hey this is a test rule that was redirected from another.
Found 1 error.
----- stderr -----
warning: `RUF96` has been remapped to `RUF95`.
"###);
}

#[test]
fn deprecated_direct() {
// Selection of a deprecated rule without preview enabled should still work
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,8 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Ruff, "940") => (RuleGroup::Removed, rules::ruff::rules::RedirectedFromTestRule),
#[cfg(feature = "test-rules")]
(Ruff, "950") => (RuleGroup::Stable, rules::ruff::rules::RedirectedToTestRule),
#[cfg(feature = "test-rules")]
(Ruff, "960") => (RuleGroup::Removed, rules::ruff::rules::RedirectedFromPrefixTestRule),


// flake8-django
Expand Down
3 changes: 3 additions & 0 deletions crates/ruff_linter/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ pub fn check_path(
Rule::RedirectedFromTestRule => {
test_rules::RedirectedFromTestRule::diagnostic(locator, indexer)
}
Rule::RedirectedFromPrefixTestRule => {
test_rules::RedirectedFromPrefixTestRule::diagnostic(locator, indexer)
}
_ => unreachable!("All test rules must have an implementation"),
};
if let Some(diagnostic) = diagnostic {
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_linter/src/rule_redirects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ static REDIRECTS: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
("T004", "FIX004"),
("RUF011", "B035"),
("TCH006", "TCH010"),
// Test redirect by exact code
#[cfg(feature = "test-rules")]
("RUF940", "RUF950"),
// Test redirect by prefix
#[cfg(feature = "test-rules")]
("RUF96", "RUF95"),
])
});
37 changes: 37 additions & 0 deletions crates/ruff_linter/src/rules/ruff/rules/test_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub(crate) const TEST_RULES: &[Rule] = &[
Rule::AnotherRemovedTestRule,
Rule::RedirectedFromTestRule,
Rule::RedirectedToTestRule,
Rule::RedirectedFromPrefixTestRule,
];

pub(crate) trait TestRule {
Expand Down Expand Up @@ -512,3 +513,39 @@ impl TestRule for RedirectedToTestRule {
))
}
}

/// ## What it does
/// Fake rule for testing.
///
/// ## Why is this bad?
/// Tests must pass!
///
/// ## Example
/// ```python
/// foo
/// ```
///
/// Use instead:
/// ```python
/// bar
/// ```
#[violation]
pub struct RedirectedFromPrefixTestRule;

impl Violation for RedirectedFromPrefixTestRule {
const FIX_AVAILABILITY: FixAvailability = FixAvailability::None;

#[derive_message_formats]
fn message(&self) -> String {
format!("Hey this is a test rule that was redirected to another by prefix.")
}
}

impl TestRule for RedirectedFromPrefixTestRule {
fn diagnostic(_locator: &Locator, _indexer: &Indexer) -> Option<Diagnostic> {
Some(Diagnostic::new(
RedirectedFromPrefixTestRule,
ruff_text_size::TextRange::default(),
))
}
}

0 comments on commit 8d4d156

Please sign in to comment.