Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/usethis/_tool/impl/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,11 @@ def apply_rule_config(self, rule_config: RuleConfig) -> None:
alert_only=(is_selected or is_ignored) or usethis_config.alert_only,
instruct_only=(is_selected or is_ignored) or usethis_config.instruct_only,
):
self.ignore_rules_in_glob(
rule_config.tests_unmanaged_ignored, glob="tests/**"
)
# Only add test directory ignore rules if the tests directory exists
if (usethis_config.cpd() / "tests").exists():
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.
Expand Down
5 changes: 5 additions & 0 deletions tests/usethis/_core/test_core_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ def test_no_duplicate_inp_rules(self, tmp_path: Path):

# Arrange
(tmp_path / "ruff.toml").touch()
(tmp_path / "tests").mkdir()

with change_cwd(tmp_path), files_manager():
# ... preparing for duplicate call
Expand Down Expand Up @@ -1496,6 +1497,7 @@ def test_inp_rules_not_selected_for_tests_dir(
):
# Arrange
(uv_init_dir / "ruff.toml").touch()
(uv_init_dir / "tests").mkdir()

with change_cwd(uv_init_dir), files_manager():
# Act
Expand Down Expand Up @@ -1532,6 +1534,7 @@ def test_message_for_already_selected_but_needs_ignoring(
[lint]
select = ["INP"]
""")
(uv_init_dir / "tests").mkdir()

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

with change_cwd(uv_init_dir), files_manager():
# Act
Expand Down Expand Up @@ -2718,6 +2722,7 @@ def test_import_linter_inp_rules(self, tmp_path: Path):
with change_cwd(tmp_path), files_manager():
# Arrange
use_import_linter()
(tmp_path / "tests").mkdir()

# Act
(tmp_path / "ruff.toml").touch()
Expand Down
39 changes: 39 additions & 0 deletions tests/usethis/_ui/interface/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,45 @@ def test_how(self, tmp_path: Path):
""" # noqa: RUF001
)

def test_when_test_dir_doesnt_exist(self, tmp_path: Path):
# Arrange
(tmp_path / "src").mkdir()
(tmp_path / "src" / "example").mkdir()
(tmp_path / "src" / "example" / "__init__.py").touch()
(tmp_path / "src" / "example" / "module.py").touch()
(tmp_path / "pyproject.toml").write_text("""\
[project]
name = "example"
version = "0.1.0"

[tool.ruff]
line-length = 88
""")
# Note: No tests directory created

# Act
runner = CliRunner()
with change_cwd(tmp_path):
result = runner.invoke_safe(app, ["import-linter", "--frozen"])

# Assert
assert result.exit_code == 0, result.output

with files_manager():
pyproject = PyprojectTOMLManager()
# Should not have per-file-ignores for tests/** since tests dir doesn't exist
if ["tool", "ruff", "lint", "per-file-ignores"] in pyproject:
per_file_ignores = pyproject[
["tool", "ruff", "lint", "per-file-ignores"]
]
assert "tests/**" not in per_file_ignores, (
"Should not add tests/** ignore when tests directory doesn't exist"
)
else:
# If per-file-ignores doesn't exist at all, that's also correct
# (no tests directory means no test-specific ignores needed)
pass


class TestPyprojectTOML:
def test_add(self, tmp_path: Path):
Expand Down