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 89217a1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 81 deletions.
46 changes: 46 additions & 0 deletions src/towncrier/test/helpers.py
Original file line number Diff line number Diff line change
@@ -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()
43 changes: 7 additions & 36 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, setup_simple_project, with_isolated_runner


class TestCli(TestCase):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
"""\
Expand Down
27 changes: 5 additions & 22 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os.path
import sys

from pathlib import Path
from subprocess import PIPE, Popen, call

from click.testing import CliRunner
Expand All @@ -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"])

Expand All @@ -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
Expand Down
27 changes: 4 additions & 23 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 All @@ -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:
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=config, mkdir_newsfragments=False)
os.mkdir("releasenotes")

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

0 comments on commit 89217a1

Please sign in to comment.