Skip to content

Commit de72743

Browse files
Ensure test directory ignore rules are only added if the /tests exists (#1232)
* Ensure test directory ignore rules are only added if the tests directory exists * Rename test for clarity * Update tests to reflect new behaviour * Update tests to reflect new behaviour
1 parent c0daf78 commit de72743

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

src/usethis/_tool/impl/ruff.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,11 @@ def apply_rule_config(self, rule_config: RuleConfig) -> None:
482482
alert_only=(is_selected or is_ignored) or usethis_config.alert_only,
483483
instruct_only=(is_selected or is_ignored) or usethis_config.instruct_only,
484484
):
485-
self.ignore_rules_in_glob(
486-
rule_config.tests_unmanaged_ignored, glob="tests/**"
487-
)
485+
# Only add test directory ignore rules if the tests directory exists
486+
if (usethis_config.cpd() / "tests").exists():
487+
self.ignore_rules_in_glob(
488+
rule_config.tests_unmanaged_ignored, glob="tests/**"
489+
)
488490

489491
def remove_rule_config(self, rule_config: RuleConfig) -> None:
490492
"""Remove the Ruff rules associated with a rule config from the project.

tests/usethis/_core/test_core_tool.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,7 @@ def test_no_duplicate_inp_rules(self, tmp_path: Path):
14681468

14691469
# Arrange
14701470
(tmp_path / "ruff.toml").touch()
1471+
(tmp_path / "tests").mkdir()
14711472

14721473
with change_cwd(tmp_path), files_manager():
14731474
# ... preparing for duplicate call
@@ -1496,6 +1497,7 @@ def test_inp_rules_not_selected_for_tests_dir(
14961497
):
14971498
# Arrange
14981499
(uv_init_dir / "ruff.toml").touch()
1500+
(uv_init_dir / "tests").mkdir()
14991501

15001502
with change_cwd(uv_init_dir), files_manager():
15011503
# Act
@@ -1532,6 +1534,7 @@ def test_message_for_already_selected_but_needs_ignoring(
15321534
[lint]
15331535
select = ["INP"]
15341536
""")
1537+
(uv_init_dir / "tests").mkdir()
15351538

15361539
with change_cwd(uv_init_dir), files_manager():
15371540
# Act
@@ -1573,6 +1576,7 @@ def test_inp_rules_dont_break_config(self, uv_init_dir: Path):
15731576
lint.select = [ "A", "C4", "E4", "E7", "E9", "F", "FLY", "FURB", "I", "INP", "PLE", "PLR", "PT", "RUF", "SIM", "UP" ]
15741577
lint.ignore = [ "PLR2004", "SIM108" ]
15751578
""")
1579+
(uv_init_dir / "tests").mkdir()
15761580

15771581
with change_cwd(uv_init_dir), files_manager():
15781582
# Act
@@ -2718,6 +2722,7 @@ def test_import_linter_inp_rules(self, tmp_path: Path):
27182722
with change_cwd(tmp_path), files_manager():
27192723
# Arrange
27202724
use_import_linter()
2725+
(tmp_path / "tests").mkdir()
27212726

27222727
# Act
27232728
(tmp_path / "ruff.toml").touch()

tests/usethis/_ui/interface/test_tool.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,45 @@ def test_how(self, tmp_path: Path):
229229
""" # noqa: RUF001
230230
)
231231

232+
def test_when_test_dir_doesnt_exist(self, tmp_path: Path):
233+
# Arrange
234+
(tmp_path / "src").mkdir()
235+
(tmp_path / "src" / "example").mkdir()
236+
(tmp_path / "src" / "example" / "__init__.py").touch()
237+
(tmp_path / "src" / "example" / "module.py").touch()
238+
(tmp_path / "pyproject.toml").write_text("""\
239+
[project]
240+
name = "example"
241+
version = "0.1.0"
242+
243+
[tool.ruff]
244+
line-length = 88
245+
""")
246+
# Note: No tests directory created
247+
248+
# Act
249+
runner = CliRunner()
250+
with change_cwd(tmp_path):
251+
result = runner.invoke_safe(app, ["import-linter", "--frozen"])
252+
253+
# Assert
254+
assert result.exit_code == 0, result.output
255+
256+
with files_manager():
257+
pyproject = PyprojectTOMLManager()
258+
# Should not have per-file-ignores for tests/** since tests dir doesn't exist
259+
if ["tool", "ruff", "lint", "per-file-ignores"] in pyproject:
260+
per_file_ignores = pyproject[
261+
["tool", "ruff", "lint", "per-file-ignores"]
262+
]
263+
assert "tests/**" not in per_file_ignores, (
264+
"Should not add tests/** ignore when tests directory doesn't exist"
265+
)
266+
else:
267+
# If per-file-ignores doesn't exist at all, that's also correct
268+
# (no tests directory means no test-specific ignores needed)
269+
pass
270+
232271

233272
class TestPyprojectTOML:
234273
def test_add(self, tmp_path: Path):

0 commit comments

Comments
 (0)