mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-02 14:52:01 +00:00
Extend assertEquals check to all deprecated unittest aliases (#380)
This commit is contained in:
parent
a2f78ba2c7
commit
71ebd39f35
8 changed files with 116 additions and 48 deletions
56
src/plugins/deprecated_unittest_alias.rs
Normal file
56
src/plugins/deprecated_unittest_alias.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
use rustpython_ast::{Expr, ExprKind, Location};
|
||||
|
||||
use crate::ast::types::Range;
|
||||
use crate::autofix::fixer;
|
||||
use crate::check_ast::Checker;
|
||||
use crate::checks::{Check, CheckKind, Fix};
|
||||
|
||||
static DEPRECATED_ALIASES: Lazy<BTreeMap<&'static str, &'static str>> = Lazy::new(|| {
|
||||
BTreeMap::from([
|
||||
("failUnlessEqual", "assertEqual"),
|
||||
("assertEquals", "assertEqual"),
|
||||
("failIfEqual", "assertNotEqual"),
|
||||
("assertNotEquals", "assertNotEqual"),
|
||||
("failUnless", "assertTrue"),
|
||||
("assert_", "assertTrue"),
|
||||
("failIf", "assertFalse"),
|
||||
("failUnlessRaises", "assertRaises"),
|
||||
("failUnlessAlmostEqual", "assertAlmostEqual"),
|
||||
("assertAlmostEquals", "assertAlmostEqual"),
|
||||
("failIfAlmostEqual", "assertNotAlmostEqual"),
|
||||
("assertNotAlmostEquals", "assertNotAlmostEqual"),
|
||||
("assertRegexpMatches", "assertRegex"),
|
||||
("assertNotRegexpMatches", "assertNotRegex"),
|
||||
("assertRaisesRegexp", "assertRaisesRegex"),
|
||||
])
|
||||
});
|
||||
|
||||
pub fn deprecated_unittest_alias(checker: &mut Checker, expr: &Expr) {
|
||||
if let ExprKind::Attribute { value, attr, .. } = &expr.node {
|
||||
if let Some(target) = DEPRECATED_ALIASES.get(attr.as_str()) {
|
||||
if let ExprKind::Name { id, .. } = &value.node {
|
||||
if id == "self" {
|
||||
let mut check = Check::new(
|
||||
CheckKind::DeprecatedUnittestAlias(attr.to_string(), target.to_string()),
|
||||
Range::from_located(expr),
|
||||
);
|
||||
if matches!(checker.autofix, fixer::Mode::Generate | fixer::Mode::Apply) {
|
||||
check.amend(Fix {
|
||||
content: format!("self.{}", target),
|
||||
location: Location::new(expr.location.row(), expr.location.column()),
|
||||
end_location: Location::new(
|
||||
expr.end_location.row(),
|
||||
expr.end_location.column(),
|
||||
),
|
||||
applied: false,
|
||||
});
|
||||
}
|
||||
checker.add_check(check);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue