diff --git a/src/towncrier/test/helpers.py b/src/towncrier/test/helpers.py new file mode 100644 index 00000000..a6f7838a --- /dev/null +++ b/src/towncrier/test/helpers.py @@ -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() diff --git a/src/towncrier/test/test_build.py b/src/towncrier/test/test_build.py index 1ca5349d..b63dfbfa 100644 --- a/src/towncrier/test/test_build.py +++ b/src/towncrier/test/test_build.py @@ -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 @@ -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): diff --git a/src/towncrier/test/test_create.py b/src/towncrier/test/test_create.py index f66cf91f..0921b304 100644 --- a/src/towncrier/test/test_create.py +++ b/src/towncrier/test/test_create.py @@ -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): @@ -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) @@ -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"])