Skip to content

Commit a2319be

Browse files
Fix codespell-pre-commit integration (#293)
* Fix codespell-pre-commit integration * Add test for codespell integration on pre-commit removal
1 parent 726f9e4 commit a2319be

4 files changed

Lines changed: 64 additions & 11 deletions

File tree

src/usethis/_core/tool.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,23 @@ def use_deptry(*, remove: bool = False) -> None:
134134
def use_pre_commit(*, remove: bool = False) -> None:
135135
tool = PreCommitTool()
136136
pyproject_fmt_tool = PyprojectFmtTool()
137+
codespell_tool = CodespellTool()
137138

138139
ensure_pyproject_toml()
139140

140141
if not remove:
141142
add_deps_to_group(tool.dev_deps, "dev")
142143
_add_all_tools_pre_commit_configs()
143144

145+
# We will use pre-commit instead of project-installed dependencies:
144146
if pyproject_fmt_tool.is_used():
145-
# We will use pre-commit instead of the dev-dep.
146147
remove_deps_from_group(pyproject_fmt_tool.dev_deps, "dev")
147148
pyproject_fmt_tool.add_pyproject_configs()
148149
_pyproject_fmt_instructions_pre_commit()
150+
if codespell_tool.is_used():
151+
remove_deps_from_group(codespell_tool.dev_deps, "dev")
152+
codespell_tool.add_pyproject_configs()
153+
_codespell_instructions_pre_commit()
149154

150155
if RequirementsTxtTool().is_used():
151156
_requirements_txt_instructions_pre_commit()
@@ -178,6 +183,9 @@ def use_pre_commit(*, remove: bool = False) -> None:
178183
if pyproject_fmt_tool.is_used():
179184
add_deps_to_group(pyproject_fmt_tool.dev_deps, "dev")
180185
_pyproject_fmt_instructions_basic()
186+
if codespell_tool.is_used():
187+
add_deps_to_group(codespell_tool.dev_deps, "dev")
188+
_codespell_instructions_basic()
181189

182190
# Likewise, explain how to manually generate the requirements.txt file, since
183191
# they're not going to do it via pre-commit anymore.

src/usethis/_integrations/pre_commit/core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ def uninstall_pre_commit_hooks() -> None:
5151
in a git repo.
5252
"""
5353
if usethis_config.frozen:
54-
box_print(
55-
"Run 'uv run pre-commit uninstall' to deregister pre-commit with git."
56-
)
54+
box_print("Run 'uvx pre-commit uninstall' to deregister pre-commit with git.")
5755
return
5856

5957
tick_print("Ensuring pre-commit hooks are uninstalled.")

tests/usethis/_core/test_ci.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,10 @@ def test_content(
263263
)
264264

265265
class TestCodespellIntegration:
266-
def test_content(
267-
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
268-
):
266+
def test_content(self, uv_init_dir: Path):
269267
with change_cwd(uv_init_dir):
270268
# Arrange
271269
use_codespell()
272-
capfd.readouterr()
273270

274271
# Act
275272
use_ci_bitbucket()

tests/usethis/_core/test_tool.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,14 @@ def test_pre_commit_integration(
115115
use_codespell()
116116

117117
# Assert
118+
# Check dependencies - shouldn't have installed codespell
119+
dev_deps = get_deps_from_group("dev")
120+
assert all(dep.name != "codespell" for dep in dev_deps)
121+
122+
# Check hook names
118123
hook_names = get_hook_names()
119-
assert "codespell" in hook_names
124+
assert "codespell" in hook_names
125+
# Check output
120126
out, err = capfd.readouterr()
121127
assert not err
122128
assert out == (
@@ -676,9 +682,28 @@ def test_pyproject_fmt_used(self, uv_init_repo_dir: Path):
676682

677683
# Assert
678684
hook_names = get_hook_names()
685+
assert "pyproject-fmt" in hook_names
686+
679687
dev_deps = get_deps_from_group("dev")
680-
assert "pyproject-fmt" in hook_names
681-
assert "pyproject-fmt" not in dev_deps
688+
for dev_dep in dev_deps:
689+
assert dev_dep.name != "pyproject-fmt"
690+
691+
@pytest.mark.usefixtures("_vary_network_conn")
692+
def test_codespell_used(self, uv_init_repo_dir: Path):
693+
with change_cwd(uv_init_repo_dir):
694+
# Arrange
695+
use_codespell()
696+
697+
# Act
698+
use_pre_commit()
699+
700+
# Assert
701+
hook_names = get_hook_names()
702+
assert "codespell" in hook_names
703+
704+
dev_deps = get_deps_from_group("dev")
705+
for dep in dev_deps:
706+
assert dep.name != "codespell"
682707

683708
class TestRemove:
684709
@pytest.mark.usefixtures("_vary_network_conn")
@@ -776,6 +801,31 @@ def test_pyproject_fmt_used(
776801
"☐ Run 'pyproject-fmt pyproject.toml' to run pyproject-fmt.\n"
777802
)
778803

804+
@pytest.mark.usefixtures("_vary_network_conn")
805+
def test_codepsell_used(
806+
self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]
807+
):
808+
with change_cwd(uv_init_dir):
809+
# Arrange
810+
with usethis_config.set(quiet=True):
811+
use_pre_commit()
812+
use_codespell()
813+
capfd.readouterr()
814+
815+
# Act
816+
use_pre_commit(remove=True)
817+
818+
# Assert
819+
out, err = capfd.readouterr()
820+
assert not err
821+
assert out == (
822+
"☐ Run 'uvx pre-commit uninstall' to deregister pre-commit with git.\n"
823+
"✔ Removing '.pre-commit-config.yaml'.\n"
824+
"✔ Removing dependency 'pre-commit' from the 'dev' group in 'pyproject.toml'.\n"
825+
"✔ Adding dependency 'codespell' to the 'dev' group in 'pyproject.toml'.\n"
826+
"☐ Run 'codespell' to run the Codespell spellchecker.\n"
827+
)
828+
779829
class TestBitbucketCIIntegration:
780830
def test_prexisting(self, uv_init_repo_dir: Path):
781831
# Arrange

0 commit comments

Comments
 (0)