Skip to content

Commit

Permalink
Refactored test_build.py (#565)
Browse files Browse the repository at this point in the history
* Refactored test_build.py

Made use of more generic with_[git_]project decorators.

Co-authored-by: Adi Roiban <adiroiban@gmail.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Adi Roiban <adiroiban@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 9, 2023
1 parent 4d317bf commit d871ea3
Show file tree
Hide file tree
Showing 3 changed files with 497 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.
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

0 comments on commit d871ea3

Please sign in to comment.