From 29a39ae8f39e31da8bbab5ebb4f9a2f63cfc03f2 Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Fri, 18 Jul 2025 07:20:08 +1200 Subject: [PATCH 1/2] For Import Linter, Fix INP config glob pattern, add its config when adding ruff --- src/usethis/_core/tool.py | 3 +-- src/usethis/_tool/impl/ruff.py | 4 +--- tests/usethis/_core/test_core_tool.py | 29 +++++++++++++++++++++++++++ tests/usethis/_interface/test_tool.py | 14 +++++++++++++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/usethis/_core/tool.py b/src/usethis/_core/tool.py index 62ac3846..b3bce896 100644 --- a/src/usethis/_core/tool.py +++ b/src/usethis/_core/tool.py @@ -449,8 +449,7 @@ def use_ruff( tool.add_configs() if linter: - tool.select_rules(rule_config.get_all_selected()) - tool.ignore_rules(rule_config.get_all_ignored()) + tool.apply_rule_config(rule_config) if PreCommitTool().is_used(): tool.add_pre_commit_config() else: diff --git a/src/usethis/_tool/impl/ruff.py b/src/usethis/_tool/impl/ruff.py index f4d6efd0..f69b07b3 100644 --- a/src/usethis/_tool/impl/ruff.py +++ b/src/usethis/_tool/impl/ruff.py @@ -409,9 +409,7 @@ def apply_rule_config(self, rule_config: RuleConfig) -> None: """ self.select_rules(rule_config.get_all_selected()) self.ignore_rules(rule_config.get_all_ignored()) - self.ignore_rules_in_glob( - rule_config.tests_unmanaged_ignored, glob="*/tests/**" - ) + self.ignore_rules_in_glob(rule_config.tests_unmanaged_ignored, glob="tests/**") def remove_rule_config(self, rule_config: RuleConfig) -> None: """Remove the Ruff rules associated with a rule config from the project. diff --git a/tests/usethis/_core/test_core_tool.py b/tests/usethis/_core/test_core_tool.py index a354f910..0414d942 100644 --- a/tests/usethis/_core/test_core_tool.py +++ b/tests/usethis/_core/test_core_tool.py @@ -1373,6 +1373,20 @@ def test_inp_rules_not_selected_for_tests_dir(self, tmp_path: Path): """ ) + @pytest.mark.usefixtures("_vary_network_conn") + def test_ruff_passes(self, tmp_path: Path): + with change_cwd(tmp_path), files_manager(): + # Arrange + use_ruff() + use_pytest() + + # Act + use_import_linter() + + # Assert + with change_cwd(tmp_path), files_manager(): + call_uv_subprocess(["run", "ruff", "check", "."], change_toml=False) + class TestRemove: def test_config_file(self, uv_init_repo_dir: Path): # Arrange @@ -2419,6 +2433,21 @@ def test_foo(): # Assert (that this doesn't raise an error) call_uv_subprocess(["run", "pytest"], change_toml=False) + @pytest.mark.usefixtures("_vary_network_conn") + def test_import_linter_inp_rules(self, tmp_path: Path): + with change_cwd(tmp_path), files_manager(): + # Arrange + use_import_linter() + + # Act + (tmp_path / "ruff.toml").touch() + use_ruff() + + # Assert + with change_cwd(tmp_path), files_manager(): + contents = (tmp_path / "ruff.toml").read_text() + assert """"tests/**" = ["INP"]""" in contents + class TestRemove: class TestRuffIntegration: def test_deselected(self, uv_init_dir: Path): diff --git a/tests/usethis/_interface/test_tool.py b/tests/usethis/_interface/test_tool.py index 9e22fcce..6f547976 100644 --- a/tests/usethis/_interface/test_tool.py +++ b/tests/usethis/_interface/test_tool.py @@ -404,6 +404,20 @@ def test_how(self, tmp_path: Path): """ ) + @pytest.mark.usefixtures("_vary_network_conn") + def test_passes_using_all_tools(self, uv_init_dir: Path): + """Test that pytest runs with all tools installed.""" + with change_cwd(uv_init_dir): + # Arrange, Act + for cmd in ALL_TOOL_COMMANDS: + if not usethis_config.offline: + call_subprocess(["usethis", "tool", cmd]) + else: + call_subprocess(["usethis", "tool", cmd, "--offline"]) + + # Act, Assert + call_uv_subprocess(["run", "ruff", "check", "."], change_toml=False) + class TestPytest: @pytest.mark.usefixtures("_vary_network_conn") From f5837a2bac9d23ad222dca1c5e4c37f05bb3df1d Mon Sep 17 00:00:00 2001 From: Nathan McDougall Date: Fri, 18 Jul 2025 07:31:05 +1200 Subject: [PATCH 2/2] Update test to reflect new behaviour --- tests/usethis/_core/test_core_tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/usethis/_core/test_core_tool.py b/tests/usethis/_core/test_core_tool.py index 0414d942..97af2ae6 100644 --- a/tests/usethis/_core/test_core_tool.py +++ b/tests/usethis/_core/test_core_tool.py @@ -1369,7 +1369,7 @@ def test_inp_rules_not_selected_for_tests_dir(self, tmp_path: Path): select = ["INP"] [lint.per-file-ignores] -"*/tests/**" = ["INP"] +"tests/**" = ["INP"] """ )