Skip to content

Commit

Permalink
Add test for fragments directory configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hynek committed Sep 16, 2022
1 parent 89217a1 commit 4be2f44
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 29 deletions.
10 changes: 7 additions & 3 deletions src/towncrier/test/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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()
Expand Down
65 changes: 39 additions & 26 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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.ouput)

def test_fragment_missing(self):
runner = CliRunner()
Expand Down Expand Up @@ -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.
"""
Expand Down

0 comments on commit 4be2f44

Please sign in to comment.