Skip to content

630 dont create pyprojecttoml in usethis tool with remove #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a3ccea6
Test that removing codespell doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
6d218a8
Pass test that removing codespell doesn't create pyproject.toml
nathanjmcdougall May 19, 2025
c5b9266
Test that removing Coverage.py doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
3b12024
Test that removing deptry doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
beed73a
Pass test that removing de[try doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
1b9b423
Test that removing Import Linter doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
db42983
Pass test that removing Import Linter doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
552b7c0
Test that removing pre-commit doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
7463bdf
Pass test that removing pre-commit doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
846b8b7
Test that removing pyproject-fmt doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
fc2859a
Pass test that removing pyproject-fmt doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
0d53276
Test that removing pyproject.toml doesnt ever try to create pyproject…
nathanjmcdougall May 19, 2025
260f09b
Pass test that removing pyproject.toml doesnt ever try to create pypr…
nathanjmcdougall May 19, 2025
9bc58e5
Test that removing pytest doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
9be4947
Pass test that removing pytest doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
2be1181
Test that removing requirements.txt doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
898ba2d
Pass test that removing requirements.txt doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
179b212
Test that removing Ruff doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
0571310
Pass test that removing Ruff doesnt create pyproject.toml
nathanjmcdougall May 19, 2025
40dfc1a
Only test file existence in Import Linter test_doesnt_add_pyproject, …
nathanjmcdougall May 19, 2025
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
39 changes: 19 additions & 20 deletions src/usethis/_core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def use_codespell(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

if not remove:
ensure_pyproject_toml()

if not PreCommitTool().is_used():
tool.add_dev_deps()
tool.update_bitbucket_steps()
Expand All @@ -74,9 +74,9 @@ def use_coverage_py(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

if not remove:
ensure_pyproject_toml()

tool.add_test_deps()
tool.add_configs()
tool.print_how_to_use()
Expand All @@ -93,9 +93,9 @@ def use_deptry(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

if not remove:
ensure_pyproject_toml()

tool.add_dev_deps()
if PreCommitTool().is_used():
tool.add_pre_commit_repo_configs()
Expand All @@ -118,9 +118,9 @@ def use_import_linter(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

if not remove:
ensure_pyproject_toml()

tool.add_dev_deps()
tool.add_configs()
if PreCommitTool().is_used():
Expand Down Expand Up @@ -148,9 +148,9 @@ def use_pre_commit(*, remove: bool = False, how: bool = False) -> None:
codespell_tool = CodespellTool()
requirements_txt_tool = RequirementsTxtTool()

ensure_pyproject_toml()

if not remove:
ensure_pyproject_toml()

tool.add_dev_deps()
_add_all_tools_pre_commit_configs()

Expand Down Expand Up @@ -234,9 +234,9 @@ def use_pyproject_fmt(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

if not remove:
ensure_pyproject_toml()

if not PreCommitTool().is_used():
tool.add_dev_deps()
tool.update_bitbucket_steps()
Expand All @@ -260,10 +260,9 @@ def use_pyproject_toml(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

if not remove:
ensure_pyproject_toml()

ensure_pyproject_validity()
tool.print_how_to_use()
else:
Expand All @@ -277,11 +276,11 @@ def use_pytest(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

rule_config = tool.get_rule_config()

if not remove:
ensure_pyproject_toml()

tool.add_test_deps()
tool.add_configs()
if RuffTool().is_used():
Expand Down Expand Up @@ -318,11 +317,11 @@ def use_requirements_txt(*, remove: bool = False, how: bool = False) -> None:
tool.print_how_to_use()
return

ensure_pyproject_toml()

path = Path.cwd() / "requirements.txt"

if not remove:
ensure_pyproject_toml()

is_pre_commit = PreCommitTool().is_used()

if is_pre_commit:
Expand Down Expand Up @@ -368,8 +367,6 @@ def use_ruff(*, remove: bool = False, how: bool = False, minimal: bool = False)
tool.print_how_to_use()
return

ensure_pyproject_toml()

# Only add ruff rules if the user doesn't already have a select/ignore list.
# Otherwise, we should leave them alone.

Expand All @@ -384,6 +381,8 @@ def use_ruff(*, remove: bool = False, how: bool = False, minimal: bool = False)
rule_config = RuleConfig()

if not remove:
ensure_pyproject_toml()

tool.add_dev_deps()
tool.add_configs()
tool.select_rules(rule_config.get_all_selected())
Expand Down
130 changes: 130 additions & 0 deletions tests/usethis/_core/test_core_tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import os
import subprocess
import unittest
import unittest.mock
from pathlib import Path
from unittest.mock import MagicMock

import pytest

Expand All @@ -14,6 +17,7 @@
use_import_linter,
use_pre_commit,
use_pyproject_fmt,
use_pyproject_toml,
use_pytest,
use_requirements_txt,
use_ruff,
Expand Down Expand Up @@ -257,6 +261,16 @@ def test_config_file(
assert not err
assert out == ("✔ Removing Codespell config from 'pyproject.toml'.\n")

def test_doesnt_add_pyproject(self, tmp_path: Path):
# No pyproject.toml file to begin with...

# Act
with change_cwd(tmp_path), files_manager():
use_codespell(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()


class TestCoverage:
class TestAdd:
Expand Down Expand Up @@ -471,6 +485,20 @@ def test_pytest_integration(
"✔ Removing dependencies 'coverage', 'pytest-cov' from the 'test' group in \n'pyproject.toml'.\n"
)

def test_doesnt_add_pyproject(
self, tmp_path: Path, capfd: pytest.CaptureFixture[str]
):
# No pyproject.toml file to begin with...

# Act
with change_cwd(tmp_path), files_manager():
use_coverage_py(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()
out, err = capfd.readouterr()
assert not out


class TestDeptry:
class TestAdd:
Expand Down Expand Up @@ -695,6 +723,21 @@ def test_roundtrip(self, uv_init_dir: Path):
"""
)

def test_doesnt_add_pyproject(
self, tmp_path: Path, capfd: pytest.CaptureFixture[str]
):
# No pyproject.toml file to begin with...

# Act
with change_cwd(tmp_path), files_manager():
use_deptry(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()
out, err = capfd.readouterr()
assert not out
assert not err

class TestPreCommitIntegration:
@pytest.mark.usefixtures("_vary_network_conn")
def test_pre_commit_first(
Expand Down Expand Up @@ -1295,6 +1338,16 @@ def test_config_file(self, uv_init_repo_dir: Path):
# Assert
assert not (uv_init_repo_dir / ".importlinter").exists()

def test_doesnt_add_pyproject(self, tmp_path: Path):
# No pyproject.toml file to begin with...

# Act
with change_cwd(tmp_path), files_manager():
use_import_linter(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()

class TestPreCommitIntegration:
def test_config(
self, uv_init_repo_dir: Path, capfd: pytest.CaptureFixture[str]
Expand Down Expand Up @@ -1626,6 +1679,20 @@ def test_codepsell_used(
"☐ Run 'codespell' to run the Codespell spellchecker.\n"
)

def test_doesnt_add_pyproject(
self, uv_init_repo_dir: Path, capfd: pytest.CaptureFixture[str]
):
# Arrange
# We use uv_init_repo_dir to get a git repo
(uv_init_repo_dir / "pyproject.toml").unlink()

# Act
with change_cwd(uv_init_repo_dir), files_manager():
use_pre_commit(remove=True)

# Assert
assert not (uv_init_repo_dir / "pyproject.toml").exists()

class TestBitbucketCIIntegration:
def test_prexisting(self, uv_init_repo_dir: Path):
# Arrange
Expand Down Expand Up @@ -1848,6 +1915,18 @@ def test_bitbucket_integration(self, uv_init_dir: Path):
contents = (uv_init_dir / "bitbucket-pipelines.yml").read_text()
assert "pyproject-fmt" not in contents

def test_doesnt_add_pyproject(
self, tmp_path: Path, capfd: pytest.CaptureFixture[str]
):
# No pyproject.toml file to begin with...

# Act
with change_cwd(tmp_path), files_manager():
use_pyproject_fmt(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()

class TestPreCommitIntegration:
@pytest.mark.usefixtures("_vary_network_conn")
def test_use_first(self, uv_init_repo_dir: Path):
Expand Down Expand Up @@ -1964,6 +2043,27 @@ def test_extra_warning_when_config_exists(
)


class TestPyprojectTOML:
class TestRemove:
def test_doesnt_invoke_ensure_pyproject_toml(self, tmp_path: Path):
# Arrange
# Mock the ensure_pyproject_toml function to raise an error

mock = MagicMock()

# Act
with (
unittest.mock.patch("usethis._core.tool.ensure_pyproject_toml", mock),
change_cwd(tmp_path),
files_manager(),
):
use_pyproject_toml(remove=True)

# Assert
assert not mock.called
assert not (tmp_path / "pyproject.toml").exists()


class TestPytest:
class TestAdd:
@pytest.mark.usefixtures("_vary_network_conn")
Expand Down Expand Up @@ -2358,6 +2458,17 @@ def test_coverage_integration(
"☐ Run 'uv run coverage help' to see available Coverage.py commands.\n"
)

def test_doesnt_create_pyproject_toml(self, tmp_path: Path):
# Arrange
(tmp_path / "pyproject.toml").unlink(missing_ok=True)

# Act
with change_cwd(tmp_path), files_manager():
use_pytest(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()

class TestUpdateBitbucketSteps:
def test_new_file(self, uv_init_dir: Path, capfd: pytest.CaptureFixture[str]):
with change_cwd(uv_init_dir), files_manager():
Expand Down Expand Up @@ -2748,6 +2859,17 @@ def test_roundtrip(self, uv_init_dir: Path):
"""
)

def test_doesnt_create_pyproject_toml(self, tmp_path: Path):
# Arrange
(tmp_path / "pyproject.toml").unlink(missing_ok=True)

# Act
with change_cwd(tmp_path), files_manager():
use_ruff(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()

class TestPrecommitIntegration:
@pytest.mark.usefixtures("_vary_network_conn")
def test_use_first(self, uv_init_repo_dir: Path):
Expand Down Expand Up @@ -3045,3 +3167,11 @@ def test_roundtrip(self, tmp_path: Path):

# Assert
assert not (tmp_path / "requirements.txt").exists()

def test_doesnt_create_pyproject_toml(self, tmp_path: Path):
# Act
with change_cwd(tmp_path), files_manager():
use_requirements_txt(remove=True)

# Assert
assert not (tmp_path / "pyproject.toml").exists()