Skip to content
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

Refactored test_build.py #565

Merged
merged 2 commits into from
Nov 9, 2023
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/towncrier/newsfragments/562.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved structure and readability of some tests for building the changelog.
76 changes: 75 additions & 1 deletion src/towncrier/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from functools import wraps
from pathlib import Path
from subprocess import call
from typing import Any, Callable

from click.testing import CliRunner
Expand Down Expand Up @@ -62,10 +63,83 @@ def setup_simple_project(
) -> None:
if config is None:
config = "[tool.towncrier]\n" 'package = "foo"\n' + extra_config

else:
config = textwrap.dedent(config)
Path(pyproject_path).write_text(config)
Path("foo").mkdir()
Path("foo/__init__.py").write_text('__version__ = "1.2.3"\n')

if mkdir_newsfragments:
Path("foo/newsfragments").mkdir()


def with_project(
*,
config: str | None = None,
pyproject_path: str = "pyproject.toml",
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
"""Decorator to run a test with an isolated directory containing a simple
project.

The files are not managed by git.

`config` is the content of the config file.
It will be automatically dedented.

`pyproject_path` is the path where to store the config file.
"""

def decorator(fn: Callable[..., Any]) -> Callable[..., Any]:
@wraps(fn)
def test(*args: Any, **kw: Any) -> Any:
runner = CliRunner()
with runner.isolated_filesystem():
setup_simple_project(
config=config,
pyproject_path=pyproject_path,
)

return fn(*args, runner=runner, **kw)

return test

return decorator


def with_git_project(
*,
config: str | None = None,
pyproject_path: str = "pyproject.toml",
) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
"""Decorator to run a test with an isolated directory containing a simple
project checked into git.
Use `config` to tweak the content of the config file.
Use `pyproject_path` to tweak the location of the config file.
"""

def decorator(fn: Callable[..., Any]) -> Callable[..., Any]:
def _commit() -> None:
call(["git", "add", "."])
call(["git", "commit", "-m", "Second Commit"])

@wraps(fn)
def test(*args: Any, **kw: Any) -> Any:
runner = CliRunner()
with runner.isolated_filesystem():
setup_simple_project(
config=config,
pyproject_path=pyproject_path,
)

call(["git", "init"])
call(["git", "config", "user.name", "user"])
call(["git", "config", "user.email", "user@example.com"])
call(["git", "config", "commit.gpgSign", "false"])
call(["git", "add", "."])
call(["git", "commit", "-m", "Initial Commit"])

return fn(*args, runner=runner, commit=_commit, **kw)

return test

return decorator
Loading
Loading