Skip to content

Commit

Permalink
Refactored test_build.py
Browse files Browse the repository at this point in the history
Made use of more generic with_[git_]project decorators.
  • Loading branch information
mdellweg committed Nov 8, 2023
1 parent 3b27ce2 commit 875df57
Show file tree
Hide file tree
Showing 3 changed files with 492 additions and 513 deletions.
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.
71 changes: 70 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,78 @@ 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.
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]:
@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", "[email protected]"])
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

0 comments on commit 875df57

Please sign in to comment.