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
1 change: 1 addition & 0 deletions src/usethis/_integrations/pyproject_toml/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


def get_name() -> str:
"""Get the project name from pyproject.toml."""
project_dict = get_project_dict()

try:
Expand Down
10 changes: 10 additions & 0 deletions src/usethis/_integrations/uv/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path

from usethis._console import tick_print
from usethis._integrations.pyproject_toml.core import set_pyproject_value
from usethis._integrations.pyproject_toml.errors import PyprojectTOMLInitError
from usethis._integrations.uv import call
from usethis._integrations.uv.errors import UVSubprocessFailedError
Expand All @@ -21,9 +22,18 @@ def ensure_pyproject_toml() -> None:
"--bare",
"--vcs=none",
"--author-from=auto",
"--build-backend", # https://github.com/nathanjmcdougall/usethis-python/issues/347
"hatch", # until https://github.com/astral-sh/uv/issues/3957
],
change_toml=True,
)
except UVSubprocessFailedError as err:
msg = f"Failed to create a pyproject.toml file:\n{err}"
raise PyprojectTOMLInitError(msg) from None

if not ((Path.cwd() / "src").exists() and (Path.cwd() / "src").is_dir()):
# hatch needs to know where to find the package
set_pyproject_value(
id_keys=["tool", "hatch", "build", "targets", "wheel"],
value={"packages": ["."]},
)
6 changes: 3 additions & 3 deletions tests/usethis/_core/test_core_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ def test_file_gone(self, tmp_path: Path):
(tmp_path / "requirements.txt").touch()

# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
use_requirements_txt(remove=True)

# Assert
Expand All @@ -1715,7 +1715,7 @@ def test_requirements_dir(self, tmp_path: Path):
(tmp_path / "requirements.txt").mkdir()

# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
use_requirements_txt(remove=True)

# Assert
Expand All @@ -1733,7 +1733,7 @@ def test_precommit_integration(self, tmp_path: Path):
)

# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
use_requirements_txt(remove=True)

# Assert
Expand Down
34 changes: 24 additions & 10 deletions tests/usethis/_integrations/uv/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import pytest

import usethis._integrations.uv.call
from usethis._integrations.pyproject_toml.core import do_pyproject_id_keys_exist
from usethis._integrations.pyproject_toml.errors import PyprojectTOMLInitError
from usethis._integrations.pyproject_toml.io_ import PyprojectTOMLManager
from usethis._integrations.uv.errors import UVSubprocessFailedError
from usethis._integrations.uv.init import ensure_pyproject_toml
from usethis._test import change_cwd
Expand All @@ -13,7 +15,7 @@
class TestEnsurePyprojectTOML:
def test_created(self, tmp_path: Path, capfd: pytest.CaptureFixture[str]):
# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
Expand All @@ -29,7 +31,7 @@ def test_already_exists_unchanged(
(tmp_path / "pyproject.toml").write_text("test")

# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
Expand All @@ -43,7 +45,7 @@ def test_hello_py_respected(self, tmp_path: Path):
(tmp_path / "hello.py").write_text("test")

# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
Expand All @@ -55,7 +57,7 @@ def test_main_py_respected(self, tmp_path: Path):
(tmp_path / "main.py").write_text("test")

# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
Expand All @@ -64,39 +66,39 @@ def test_main_py_respected(self, tmp_path: Path):

def test_no_hello_py_created(self, tmp_path: Path):
# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
assert not (tmp_path / "hello.py").exists()

def test_no_main_py_created(self, tmp_path: Path):
# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
assert not (tmp_path / "main.py").exists()

def test_no_readme(self, tmp_path: Path):
# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
assert not (tmp_path / "README.md").exists()

def test_no_pin_python(self, tmp_path: Path):
# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
assert not (tmp_path / ".python-version").exists()

def test_no_vcs(self, tmp_path: Path):
# Act
with change_cwd(tmp_path):
with change_cwd(tmp_path), PyprojectTOMLManager():
ensure_pyproject_toml()

# Assert
Expand All @@ -114,5 +116,17 @@ def mock_call_uv_subprocess(*args: Any, **kwargs: Any) -> None:
)

# Act
with change_cwd(tmp_path), pytest.raises(PyprojectTOMLInitError):
with (
change_cwd(tmp_path),
PyprojectTOMLManager(),
pytest.raises(PyprojectTOMLInitError),
):
ensure_pyproject_toml()

def test_build_backend(self, tmp_path: Path):
with change_cwd(tmp_path), PyprojectTOMLManager():
# Act
ensure_pyproject_toml()

# Assert
assert do_pyproject_id_keys_exist(id_keys=["build-system"])
10 changes: 10 additions & 0 deletions tests/usethis/_interface/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ def test_add(self, tmp_path: Path):

@pytest.mark.benchmark
def test_several_tools_add_and_remove(tmp_path: Path):
# Arrange
# The rationale for using src layout is to avoid writing
# hatch config unnecessarily slowing down I/O
tmp_path = tmp_path / "benchmark" # To get a fixed project name
tmp_path.mkdir(exist_ok=True)
(tmp_path / "src").mkdir(exist_ok=True)
(tmp_path / "src" / "benchmark").mkdir(exist_ok=True)
(tmp_path / "src" / "benchmark" / "__init__.py").touch(exist_ok=True)

# Act
runner = CliRunner()
with change_cwd(tmp_path):
runner.invoke(app, ["pytest"])
Expand Down