Skip to content

Commit 54c3798

Browse files
Feature/84 bug pt rule added even when pytest is not used (#87)
* Fix bug where ruff rules for all tools get added * Fix bug where all ruff rules are always enabled even for unused tools
1 parent cbba529 commit 54c3798

File tree

4 files changed

+146
-31
lines changed

4 files changed

+146
-31
lines changed

src/usethis/_interface/tool.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ def _ruff(*, remove: bool = False, offline: bool = False) -> None:
101101

102102
rules = []
103103
for _tool in ALL_TOOLS:
104-
with contextlib.suppress(NotImplementedError):
105-
rules += _tool.get_associated_ruff_rules()
104+
if _tool.is_used() or _tool.name == "ruff":
105+
with contextlib.suppress(NotImplementedError):
106+
rules += _tool.get_associated_ruff_rules()
106107

107108
if not remove:
108109
add_deps_to_group(tool.dev_deps, "dev", offline=offline)

tests/usethis/_integrations/bitbucket/test_cache.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
from pathlib import Path
22

3-
from usethis._integrations.bitbucket.cache import Cache, add_cache, get_caches
3+
import pytest
4+
5+
from usethis._integrations.bitbucket.cache import (
6+
Cache,
7+
CacheAlreadyExistsError,
8+
add_cache,
9+
get_caches,
10+
)
411
from usethis._utils._test import change_cwd
512

613

@@ -15,3 +22,14 @@ def test_in_caches(self, uv_init_dir: Path):
1522
# Assert
1623
caches = get_caches()
1724
assert caches == [cache]
25+
26+
def test_already_exists(self, uv_init_dir: Path):
27+
# Arrange
28+
cache = Cache(name="example", path="~/.cache/example")
29+
30+
with change_cwd(uv_init_dir):
31+
add_cache(cache)
32+
33+
# Act, Assert
34+
with pytest.raises(CacheAlreadyExistsError):
35+
add_cache(cache, exists_ok=False)

tests/usethis/_interface/test_ci.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,28 @@ def test_not_mentioned_if_not_used(self, uv_init_dir: Path):
105105
assert "pytest" not in contents
106106

107107
class TestRemove:
108-
def test_config_file_gone(self, uv_init_dir: Path):
109-
# Arrange
110-
(uv_init_dir / "bitbucket-pipelines.yml").touch()
108+
class TestPyproject:
109+
def test_removed(self, uv_init_dir: Path):
110+
# Arrange
111+
(uv_init_dir / "bitbucket-pipelines.yml").touch()
111112

112-
# Act
113-
with change_cwd(uv_init_dir):
114-
_bitbucket(remove=True, offline=is_offline())
113+
# Act
114+
with change_cwd(uv_init_dir):
115+
_bitbucket(remove=True, offline=is_offline())
115116

116-
# Assert
117-
assert not (uv_init_dir / "bitbucket-pipelines.yml").exists()
117+
# Assert
118+
assert not (uv_init_dir / "bitbucket-pipelines.yml").exists()
118119

119-
def test_message(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
120-
# Arrange
121-
(uv_init_dir / "bitbucket-pipelines.yml").touch()
120+
def test_message(
121+
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
122+
):
123+
# Arrange
124+
(uv_init_dir / "bitbucket-pipelines.yml").touch()
122125

123-
# Act
124-
with change_cwd(uv_init_dir):
125-
_bitbucket(remove=True, offline=is_offline())
126+
# Act
127+
with change_cwd(uv_init_dir):
128+
_bitbucket(remove=True, offline=is_offline())
126129

127-
# Assert
128-
out, _ = capfd.readouterr()
129-
assert out == "✔ Removing 'bitbucket-pipelines.yml' file.\n"
130+
# Assert
131+
out, _ = capfd.readouterr()
132+
assert out == "✔ Removing 'bitbucket-pipelines.yml' file.\n"

tests/usethis/_interface/test_tool.py

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def test_stdout(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
362362
assert out == (
363363
"✔ Adding 'ruff' to the 'dev' dependency group.\n"
364364
"✔ Adding ruff config to 'pyproject.toml'.\n"
365-
"✔ Enabling ruff rules 'C4', 'E4', 'E7', 'E9', 'F', 'FURB', 'I', 'PLE', 'PLR', \n'PT', 'RUF', 'SIM', 'UP' in 'pyproject.toml'.\n"
365+
"✔ Enabling ruff rules 'C4', 'E4', 'E7', 'E9', 'F', 'FURB', 'I', 'PLE', 'PLR', \n'RUF', 'SIM', 'UP' in 'pyproject.toml'.\n"
366366
"☐ Call the 'ruff check --fix' command to run the ruff linter with autofixes.\n"
367367
"☐ Call the 'ruff format' command to run the ruff formatter.\n"
368368
)
@@ -442,13 +442,106 @@ def test_roundtrip(self, uv_init_dir: Path):
442442

443443

444444
class TestPytest:
445-
def test_dep(self, uv_init_dir: Path):
446-
with change_cwd(uv_init_dir):
447-
_pytest(offline=is_offline())
448-
449-
assert {
450-
"pytest",
451-
"pytest-md",
452-
"pytest-cov",
453-
"coverage",
454-
} <= set(get_deps_from_group("test"))
445+
class TestAdd:
446+
def test_dep(self, uv_init_dir: Path):
447+
with change_cwd(uv_init_dir):
448+
_pytest(offline=is_offline())
449+
450+
assert {
451+
"pytest",
452+
"pytest-md",
453+
"pytest-cov",
454+
"coverage",
455+
} <= set(get_deps_from_group("test"))
456+
457+
class TestRemove:
458+
class TestRuffIntegration:
459+
def test_deselected(self, uv_init_dir: Path):
460+
# Arrange
461+
(uv_init_dir / "pyproject.toml").write_text(
462+
"""\
463+
[tool.ruff.lint]
464+
select = ["E", "PT"]
465+
"""
466+
)
467+
468+
# Act
469+
with change_cwd(uv_init_dir):
470+
_pytest(remove=True, offline=is_offline())
471+
472+
# Assert
473+
assert (uv_init_dir / "pyproject.toml").read_text() == (
474+
"""\
475+
[tool.ruff.lint]
476+
select = ["E"]
477+
"""
478+
)
479+
480+
def test_message(
481+
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
482+
):
483+
# Arrange
484+
(uv_init_dir / "pyproject.toml").write_text(
485+
"""\
486+
[tool.ruff.lint]
487+
select = ["PT"]
488+
"""
489+
)
490+
491+
# Act
492+
with change_cwd(uv_init_dir):
493+
_pytest(remove=True, offline=is_offline())
494+
495+
# Assert
496+
out, _ = capfd.readouterr()
497+
assert out == ("✔ Disabling ruff rule 'PT' in 'pyproject.toml'.\n")
498+
499+
class TestPyproject:
500+
def test_removed(self, uv_init_dir: Path):
501+
# Arrange
502+
(uv_init_dir / "pyproject.toml").write_text(
503+
"""\
504+
[tool.pytest]
505+
foo = "bar"
506+
"""
507+
)
508+
509+
# Act
510+
with change_cwd(uv_init_dir):
511+
_pytest(remove=True, offline=is_offline())
512+
513+
# Assert
514+
assert (uv_init_dir / "pyproject.toml").read_text() == ""
515+
516+
def test_message(
517+
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
518+
):
519+
# Arrange
520+
(uv_init_dir / "pyproject.toml").write_text(
521+
"""\
522+
[tool.pytest]
523+
foo = "bar"
524+
"""
525+
)
526+
527+
# Act
528+
with change_cwd(uv_init_dir):
529+
_pytest(remove=True, offline=is_offline())
530+
531+
# Assert
532+
out, _ = capfd.readouterr()
533+
# N.B. we don't put `pytest` in quotes because we are referring to the
534+
# tool, not the package.
535+
assert out == "✔ Removing pytest config from 'pyproject.toml'.\n"
536+
537+
class Dependencies:
538+
def test_removed(self, uv_init_dir: Path):
539+
# Arrange
540+
add_deps_to_group(["pytest"], "test", offline=is_offline())
541+
542+
# Act
543+
with change_cwd(uv_init_dir):
544+
_pytest(remove=True, offline=is_offline())
545+
546+
# Assert
547+
assert not get_deps_from_group("test")

0 commit comments

Comments
 (0)