From 4dd0f15c2c2f36a95e5b77b02b58471372531cde Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Fri, 16 Sep 2022 14:50:58 +0200 Subject: [PATCH] Add test for fragments directory configuration --- src/towncrier/test/helpers.py | 10 +++-- src/towncrier/test/test_check.py | 65 +++++++++++++++++++------------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/towncrier/test/helpers.py b/src/towncrier/test/helpers.py index 3845a8c5..a3ba7629 100644 --- a/src/towncrier/test/helpers.py +++ b/src/towncrier/test/helpers.py @@ -13,7 +13,7 @@ def write(path, contents): Create a file with given contents including any missing parent directories """ p = Path(path) - p.parent.mkdir(parents=True) + p.parent.mkdir(parents=True, exist_ok=True) p.write_text(contents) @@ -33,10 +33,14 @@ def test(*args, **kw): def setup_simple_project( - *, config=None, pyproject_path="pyproject.toml", mkdir_newsfragments=True + *, + config=None, + extra_config="", + pyproject_path="pyproject.toml", + mkdir_newsfragments=True, ): if config is None: - config = "[tool.towncrier]\n" 'package = "foo"\n' + config = "[tool.towncrier]\n" 'package = "foo"\n' + extra_config Path(pyproject_path).write_text(config) Path("foo").mkdir() diff --git a/src/towncrier/test/test_check.py b/src/towncrier/test/test_check.py index d6db0372..f652c83f 100644 --- a/src/towncrier/test/test_check.py +++ b/src/towncrier/test/test_check.py @@ -14,15 +14,17 @@ from towncrier import check from towncrier.check import _main as towncrier_check -from .helpers import setup_simple_project, write +from .helpers import setup_simple_project, with_isolated_runner, write -def create_project(pyproject_path="pyproject.toml", main_branch="main"): +def create_project( + pyproject_path="pyproject.toml", main_branch="main", extra_config="" +): """ Create the project files in the main branch that already has a news-fragment and then switch to a new in-work branch. """ - setup_simple_project(pyproject_path=pyproject_path) + setup_simple_project(pyproject_path=pyproject_path, extra_config=extra_config) Path("foo/newsfragments/123.feature").write_text("Adds levitation") initial_commit(branch=main_branch) call(["git", "checkout", "-b", "otherbranch"]) @@ -102,35 +104,46 @@ def _test_no_changes_made(self, pyproject_path, invoke): result.output, ) - def test_fragment_exists(self): - runner = CliRunner() + @with_isolated_runner + def test_fragment_exists(self, runner): + create_project("pyproject.toml") - with runner.isolated_filesystem(): - create_project("pyproject.toml", main_branch="master") + write("foo/somefile.py", "import os") + commit("add a file") - file_path = "foo/somefile.py" - with open(file_path, "w") as f: - f.write("import os") + fragment_path = Path("foo/newsfragments/1234.feature").resolve() + write(fragment_path, "Adds gravity back") + commit("add a newsfragment") - call(["git", "add", "foo/somefile.py"]) - call(["git", "commit", "-m", "add a file"]) + result = runner.invoke(towncrier_check, ["--compare-with", "main"]) - fragment_path = "foo/newsfragments/1234.feature" - with open(fragment_path, "w") as f: - f.write("Adds gravity back") + self.assertTrue( + result.output.endswith("Found:\n1. " + str(fragment_path) + "\n"), + result.output, + ) + self.assertEqual(0, result.exit_code, result.output) - call(["git", "add", fragment_path]) - call(["git", "commit", "-m", "add a newsfragment"]) + @with_isolated_runner + def test_fragment_exists_hidden(self, runner): + """ + Location of fragments can be configured using tool.towncrier.directory. + """ + create_project("pyproject.toml", extra_config="directory = 'deep/fragz'\n") - result = runner.invoke(towncrier_check, ["--compare-with", "master"]) + write("foo/bar/somefile.py", "import os") + commit("add a file") - self.assertTrue( - result.output.endswith( - "Found:\n1. " + os.path.abspath(fragment_path) + "\n" - ), - result, - ) - self.assertEqual(0, result.exit_code, result) + fragment_path = Path("deep/fragz/1234.feature").resolve() + write(fragment_path, "Adds gravity back") + commit("add a newsfragment") + + result = runner.invoke(towncrier_check, ["--compare-with", "main"]) + + self.assertTrue( + result.output.endswith("Found:\n1. " + str(fragment_path) + "\n"), + result.output, + ) + self.assertEqual(0, result.exit_code, result.output) def test_fragment_missing(self): runner = CliRunner() @@ -256,7 +269,7 @@ def test_release_branch(self): self.assertEqual(0, result.exit_code, (result, result.output)) self.assertIn("Checks SKIPPED: news file changes detected", result.output) - def test_get_default_compare_branch_missingf(self): + def test_get_default_compare_branch_missing(self): """ If there's no recognized remote origin, exit with an error. """