Skip to content

Commit

Permalink
Organize test helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Sep 16, 2022
1 parent b657909 commit d9699f6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 52 deletions.
35 changes: 35 additions & 0 deletions src/towncrier/test/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from functools import wraps
from pathlib import Path

from click.testing import CliRunner


def read_all(filename):
return Path(filename).read_text()


def with_isolated_runner(fn):
"""
Run *fn* within an isolated filesystem and add the kwarg *runner* to its
arguments.
"""

@wraps(fn)
def test(*args, **kw):
runner = CliRunner()
with runner.isolated_filesystem():
return fn(*args, runner=runner, **kw)

return test


def setup_simple_project(config=None, mkdir_newsfragments=True):
if config is None:
config = "[tool.towncrier]\n" 'package = "foo"\n'

Path("pyproject.toml").write_text(config)
Path("foo").mkdir()
Path("foo/__init__.py").write_text('__version__ = "1.2.3"\n')

if mkdir_newsfragments:
Path("foo/newsfragments").mkdir()
31 changes: 1 addition & 30 deletions src/towncrier/test/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import tempfile

from datetime import date
from functools import wraps
from pathlib import Path
from subprocess import call
from textwrap import dedent
Expand All @@ -16,35 +15,7 @@

from .._shell import cli
from ..build import _main


def setup_simple_project():
with open("pyproject.toml", "w") as f:
f.write("[tool.towncrier]\n" 'package = "foo"\n')
os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')
os.mkdir("foo/newsfragments")


def read_all(filename):
with open(filename) as f:
return f.read()


def with_isolated_runner(fn):
"""
Run *fn* within an isolated filesystem and add the kwarg *runner* to its
arguments.
"""

@wraps(fn)
def test(*args, **kw):
runner = CliRunner()
with runner.isolated_filesystem():
return fn(*args, runner=runner, **kw)

return test
from .helpers import read_all, setup_simple_project, with_isolated_runner


class TestCli(TestCase):
Expand Down
25 changes: 3 additions & 22 deletions src/towncrier/test/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,7 @@
from twisted.trial.unittest import TestCase

from ..create import _main


def setup_simple_project(config=None, mkdir=True):
if not config:
config = dedent(
"""\
[tool.towncrier]
package = "foo"
"""
)

with open("pyproject.toml", "w") as f:
f.write(config)

os.mkdir("foo")
with open("foo/__init__.py", "w") as f:
f.write('__version__ = "1.2.3"\n')

if mkdir:
os.mkdir("foo/newsfragments")
from .helpers import setup_simple_project


class TestCli(TestCase):
Expand Down Expand Up @@ -95,7 +76,7 @@ def test_edit_abort(self):
runner = CliRunner()

with runner.isolated_filesystem():
setup_simple_project(config=None, mkdir=True)
setup_simple_project(config=None, mkdir_newsfragments=True)
result = runner.invoke(_main, ["123.feature.rst", "--edit"])
self.assertEqual([], os.listdir("foo/newsfragments"))
self.assertEqual(1, result.exit_code)
Expand Down Expand Up @@ -141,7 +122,7 @@ def test_different_directory(self):
)

with runner.isolated_filesystem():
setup_simple_project(config, mkdir=False)
setup_simple_project(config, mkdir_newsfragments=False)
os.mkdir("releasenotes")

result = runner.invoke(_main, ["123.feature.rst"])
Expand Down

0 comments on commit d9699f6

Please sign in to comment.