From eed6866b7ed7b458660a6b56810ef752085f86f6 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 19 Apr 2023 16:04:22 -0400 Subject: [PATCH] Add relative-path tests for `banned-api` (#4033) --- .../{TID252 => TID}/my_package/__init__.py | 0 .../my_package/sublib/__init__.py | 0 .../my_package/sublib/api/__init__.py | 0 .../my_package/sublib/api/application.py | 0 .../my_package/sublib/api/logger.py | 0 .../my_package/sublib/api/models.py | 0 .../sublib/protocol/UpperCaseModule.py | 0 .../my_package/sublib/protocol/__init__.py | 0 .../my_package/sublib/protocol/commands.py | 0 .../my_package/sublib/protocol/definitions.py | 0 .../my_package/sublib/protocol/responses.py | 0 .../my_package/sublib/server/__init__.py | 0 .../my_package/sublib/server/example.py | 0 .../TID251_false_positives.py | 11 ------- .../rules/flake8_tidy_imports/banned_api.rs | 32 ++++++++++++++++++- .../flake8_tidy_imports/relative_imports.rs | 2 +- ...ports__banned_api__tests__banned_api.snap} | 0 ...banned_api__tests__banned_api_package.snap | 31 ++++++++++++++++++ 18 files changed, 63 insertions(+), 13 deletions(-) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/__init__.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/__init__.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/api/__init__.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/api/application.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/api/logger.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/api/models.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/protocol/UpperCaseModule.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/protocol/__init__.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/protocol/commands.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/protocol/definitions.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/protocol/responses.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/server/__init__.py (100%) rename crates/ruff/resources/test/fixtures/flake8_tidy_imports/{TID252 => TID}/my_package/sublib/server/example.py (100%) delete mode 100644 crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID251_false_positives.py rename crates/ruff/src/rules/flake8_tidy_imports/snapshots/{ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api_true_positives.snap => ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api.snap} (100%) create mode 100644 crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api_package.snap diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/__init__.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/__init__.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/__init__.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/__init__.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/__init__.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/__init__.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/__init__.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/__init__.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/__init__.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/__init__.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/__init__.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/__init__.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/application.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/application.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/application.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/application.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/logger.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/logger.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/logger.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/logger.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/models.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/models.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/api/models.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/api/models.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/UpperCaseModule.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/UpperCaseModule.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/UpperCaseModule.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/UpperCaseModule.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/__init__.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/__init__.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/__init__.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/__init__.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/commands.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/commands.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/commands.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/commands.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/definitions.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/definitions.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/definitions.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/definitions.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/responses.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/responses.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/protocol/responses.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/protocol/responses.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/server/__init__.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/server/__init__.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/server/__init__.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/server/__init__.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/server/example.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/server/example.py similarity index 100% rename from crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID252/my_package/sublib/server/example.py rename to crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID/my_package/sublib/server/example.py diff --git a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID251_false_positives.py b/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID251_false_positives.py deleted file mode 100644 index c61b936c85023..0000000000000 --- a/crates/ruff/resources/test/fixtures/flake8_tidy_imports/TID251_false_positives.py +++ /dev/null @@ -1,11 +0,0 @@ -# module members cannot be imported with that syntax -import typing.TypedDict - -# we don't track reassignments -import typing, other -typing = other -typing.TypedDict() - -# yet another false positive -def foo(typing): - typing.TypedDict() diff --git a/crates/ruff/src/rules/flake8_tidy_imports/banned_api.rs b/crates/ruff/src/rules/flake8_tidy_imports/banned_api.rs index 66dbf0c523f34..56bc0ea95242a 100644 --- a/crates/ruff/src/rules/flake8_tidy_imports/banned_api.rs +++ b/crates/ruff/src/rules/flake8_tidy_imports/banned_api.rs @@ -121,7 +121,7 @@ mod tests { use super::ApiBan; #[test] - fn banned_api_true_positives() -> Result<()> { + fn banned_api() -> Result<()> { let diagnostics = test_path( Path::new("flake8_tidy_imports/TID251.py"), &Settings { @@ -148,4 +148,34 @@ mod tests { assert_messages!(diagnostics); Ok(()) } + + #[test] + fn banned_api_package() -> Result<()> { + let diagnostics = test_path( + Path::new("flake8_tidy_imports/TID/my_package/sublib/api/application.py"), + &Settings { + flake8_tidy_imports: super::super::Settings { + banned_api: FxHashMap::from_iter([ + ( + "attrs".to_string(), + ApiBan { + msg: "The attrs module is deprecated.".to_string(), + }, + ), + ( + "my_package.sublib.protocol".to_string(), + ApiBan { + msg: "The protocol module is deprecated.".to_string(), + }, + ), + ]), + ..Default::default() + }, + namespace_packages: vec![Path::new("my_package").to_path_buf()], + ..Settings::for_rules(vec![Rule::BannedApi]) + }, + )?; + assert_messages!(diagnostics); + Ok(()) + } } diff --git a/crates/ruff/src/rules/flake8_tidy_imports/relative_imports.rs b/crates/ruff/src/rules/flake8_tidy_imports/relative_imports.rs index 5082a58736fb0..04aaa7b0490ae 100644 --- a/crates/ruff/src/rules/flake8_tidy_imports/relative_imports.rs +++ b/crates/ruff/src/rules/flake8_tidy_imports/relative_imports.rs @@ -204,7 +204,7 @@ mod tests { #[test] fn ban_parent_imports_package() -> Result<()> { let diagnostics = test_path( - Path::new("flake8_tidy_imports/TID252/my_package/sublib/api/application.py"), + Path::new("flake8_tidy_imports/TID/my_package/sublib/api/application.py"), &Settings { flake8_tidy_imports: super::super::Settings { ban_relative_imports: Strictness::Parents, diff --git a/crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api_true_positives.snap b/crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api.snap similarity index 100% rename from crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api_true_positives.snap rename to crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api.snap diff --git a/crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api_package.snap b/crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api_package.snap new file mode 100644 index 0000000000000..d11d98610f288 --- /dev/null +++ b/crates/ruff/src/rules/flake8_tidy_imports/snapshots/ruff__rules__flake8_tidy_imports__banned_api__tests__banned_api_package.snap @@ -0,0 +1,31 @@ +--- +source: crates/ruff/src/rules/flake8_tidy_imports/banned_api.rs +--- +application.py:3:8: TID251 `attrs` is banned: The attrs module is deprecated. + | +3 | from typing import TYPE_CHECKING, Any, ClassVar +4 | +5 | import attrs + | ^^^^^ TID251 +6 | +7 | from ....import unknown + | + +application.py:6:1: TID251 `my_package.sublib.protocol` is banned: The protocol module is deprecated. + | +6 | from ....import unknown +7 | from ..protocol import commands, definitions, responses + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TID251 +8 | from ..server import example +9 | from .. import server + | + +application.py:10:1: TID251 `my_package.sublib.protocol` is banned: The protocol module is deprecated. + | +10 | from .. import server +11 | from . import logger, models +12 | from ..protocol.UpperCaseModule import some_function + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TID251 + | + +