-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made use of more generic with_[git_]project decorators.
- Loading branch information
Showing
3 changed files
with
492 additions
and
513 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Improved structure and readability of some tests for building the changelog. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 |
Oops, something went wrong.