diff --git a/src/towncrier/test/helpers.py b/src/towncrier/test/helpers.py new file mode 100644 index 00000000..3845a8c5 --- /dev/null +++ b/src/towncrier/test/helpers.py @@ -0,0 +1,46 @@ +from functools import wraps +from pathlib import Path + +from click.testing import CliRunner + + +def read(filename): + return Path(filename).read_text() + + +def write(path, contents): + """ + Create a file with given contents including any missing parent directories + """ + p = Path(path) + p.parent.mkdir(parents=True) + p.write_text(contents) + + +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, pyproject_path="pyproject.toml", mkdir_newsfragments=True +): + if config is None: + config = "[tool.towncrier]\n" 'package = "foo"\n' + + 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() diff --git a/src/towncrier/test/test_build.py b/src/towncrier/test/test_build.py index 1ca5349d..ee5c991b 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, setup_simple_project, with_isolated_runner class TestCli(TestCase): @@ -211,7 +182,7 @@ def test_no_newsfragments(self): result = runner.invoke(_main, ["--date", "01-01-2001"]) - news = read_all("NEWS.rst") + news = read("NEWS.rst") self.assertEqual(0, result.exit_code) self.assertIn("No significant changes.\n", news) @@ -720,8 +691,8 @@ def do_build_once_with(version, fragment_file, fragment): self.assertTrue(os.path.exists("7.9.0-notes.rst"), os.listdir(".")) outputs = [] - outputs.append(read_all("7.8.9-notes.rst")) - outputs.append(read_all("7.9.0-notes.rst")) + outputs.append(read("7.8.9-notes.rst")) + outputs.append(read("7.9.0-notes.rst")) self.assertEqual( outputs[0], @@ -830,7 +801,7 @@ def do_build_once_with(version, fragment_file, fragment): ) self.assertTrue(os.path.exists("{version}-notes.rst"), os.listdir(".")) - output = read_all("{version}-notes.rst") + output = read("{version}-notes.rst") self.assertEqual( output, @@ -896,7 +867,7 @@ def test_bullet_points_false(self): ) self.assertEqual(0, result.exit_code, result.output) - output = read_all("NEWS.rst") + output = read("NEWS.rst") self.assertEqual( output, @@ -1107,7 +1078,7 @@ def test_start_string(self): self.assertEqual(0, result.exit_code, result.output) self.assertTrue(os.path.exists("NEWS.rst"), os.listdir(".")) - output = read_all("NEWS.rst") + output = read("NEWS.rst") expected_output = dedent( """\ diff --git a/src/towncrier/test/test_check.py b/src/towncrier/test/test_check.py index 6ad70f36..d6db0372 100644 --- a/src/towncrier/test/test_check.py +++ b/src/towncrier/test/test_check.py @@ -5,6 +5,7 @@ import os.path import sys +from pathlib import Path from subprocess import PIPE, Popen, call from click.testing import CliRunner @@ -13,22 +14,16 @@ from towncrier import check from towncrier.check import _main as towncrier_check +from .helpers import setup_simple_project, write + def create_project(pyproject_path="pyproject.toml", main_branch="main"): """ Create the project files in the main branch that already has a news-fragment and then switch to a new in-work branch. """ - with open(pyproject_path, "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") - fragment_path = "foo/newsfragments/123.feature" - with open(fragment_path, "w") as f: - f.write("Adds levitation") - + setup_simple_project(pyproject_path=pyproject_path) + Path("foo/newsfragments/123.feature").write_text("Adds levitation") initial_commit(branch=main_branch) call(["git", "checkout", "-b", "otherbranch"]) @@ -43,18 +38,6 @@ def commit(message): call(["git", "commit", "-m", message]) -def write(path, contents): - """Create a file with given contents including any missing parent directories""" - dir = os.path.dirname(path) - if dir: - try: - os.makedirs(dir) - except OSError: # pragma: no cover - pass - with open(path, "w") as f: - f.write(contents) - - def initial_commit(branch="main"): """ Create a git repo, configure it and make an initial commit diff --git a/src/towncrier/test/test_create.py b/src/towncrier/test/test_create.py index f66cf91f..e2af46c4 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): @@ -41,7 +22,7 @@ def _test_success( runner = CliRunner() with runner.isolated_filesystem(): - setup_simple_project(config, mkdir) + setup_simple_project(config=config, mkdir_newsfragments=mkdir) args = ["123.feature.rst"] if content is None: @@ -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=config, mkdir_newsfragments=False) os.mkdir("releasenotes") result = runner.invoke(_main, ["123.feature.rst"])