Skip to content

Commit 5f048a9

Browse files
committed
Organize test helpers
1 parent b657909 commit 5f048a9

File tree

4 files changed

+45
-59
lines changed

4 files changed

+45
-59
lines changed

src/towncrier/test/helpers.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from functools import wraps
2+
from pathlib import Path
3+
4+
from click.testing import CliRunner
5+
6+
7+
def read_all(filename):
8+
return Path(filename).read_text()
9+
10+
11+
def with_isolated_runner(fn):
12+
"""
13+
Run *fn* within an isolated filesystem and add the kwarg *runner* to its
14+
arguments.
15+
"""
16+
17+
@wraps(fn)
18+
def test(*args, **kw):
19+
runner = CliRunner()
20+
with runner.isolated_filesystem():
21+
return fn(*args, runner=runner, **kw)
22+
23+
return test
24+
25+
26+
def setup_simple_project(
27+
*, config=None, pyproject_path="pyproject.toml", mkdir_newsfragments=True
28+
):
29+
if config is None:
30+
config = "[tool.towncrier]\n" 'package = "foo"\n'
31+
32+
Path(pyproject_path).write_text(config)
33+
Path("foo").mkdir()
34+
Path("foo/__init__.py").write_text('__version__ = "1.2.3"\n')
35+
36+
if mkdir_newsfragments:
37+
Path("foo/newsfragments").mkdir()

src/towncrier/test/test_build.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import tempfile
66

77
from datetime import date
8-
from functools import wraps
98
from pathlib import Path
109
from subprocess import call
1110
from textwrap import dedent
@@ -16,35 +15,7 @@
1615

1716
from .._shell import cli
1817
from ..build import _main
19-
20-
21-
def setup_simple_project():
22-
with open("pyproject.toml", "w") as f:
23-
f.write("[tool.towncrier]\n" 'package = "foo"\n')
24-
os.mkdir("foo")
25-
with open("foo/__init__.py", "w") as f:
26-
f.write('__version__ = "1.2.3"\n')
27-
os.mkdir("foo/newsfragments")
28-
29-
30-
def read_all(filename):
31-
with open(filename) as f:
32-
return f.read()
33-
34-
35-
def with_isolated_runner(fn):
36-
"""
37-
Run *fn* within an isolated filesystem and add the kwarg *runner* to its
38-
arguments.
39-
"""
40-
41-
@wraps(fn)
42-
def test(*args, **kw):
43-
runner = CliRunner()
44-
with runner.isolated_filesystem():
45-
return fn(*args, runner=runner, **kw)
46-
47-
return test
18+
from .helpers import read_all, setup_simple_project, with_isolated_runner
4819

4920

5021
class TestCli(TestCase):

src/towncrier/test/test_check.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
from towncrier import check
1414
from towncrier.check import _main as towncrier_check
1515

16+
from .helpers import setup_simple_project
17+
1618

1719
def create_project(pyproject_path="pyproject.toml", main_branch="main"):
1820
"""
1921
Create the project files in the main branch that already has a
2022
news-fragment and then switch to a new in-work branch.
2123
"""
22-
with open(pyproject_path, "w") as f:
23-
f.write("[tool.towncrier]\n" 'package = "foo"\n')
24-
os.mkdir("foo")
25-
with open("foo/__init__.py", "w") as f:
26-
f.write('__version__ = "1.2.3"\n')
27-
os.mkdir("foo/newsfragments")
24+
setup_simple_project(pyproject_path=pyproject_path)
2825
fragment_path = "foo/newsfragments/123.feature"
2926
with open(fragment_path, "w") as f:
3027
f.write("Adds levitation")

src/towncrier/test/test_create.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,7 @@
1010
from twisted.trial.unittest import TestCase
1111

1212
from ..create import _main
13-
14-
15-
def setup_simple_project(config=None, mkdir=True):
16-
if not config:
17-
config = dedent(
18-
"""\
19-
[tool.towncrier]
20-
package = "foo"
21-
"""
22-
)
23-
24-
with open("pyproject.toml", "w") as f:
25-
f.write(config)
26-
27-
os.mkdir("foo")
28-
with open("foo/__init__.py", "w") as f:
29-
f.write('__version__ = "1.2.3"\n')
30-
31-
if mkdir:
32-
os.mkdir("foo/newsfragments")
13+
from .helpers import setup_simple_project
3314

3415

3516
class TestCli(TestCase):
@@ -41,7 +22,7 @@ def _test_success(
4122
runner = CliRunner()
4223

4324
with runner.isolated_filesystem():
44-
setup_simple_project(config, mkdir)
25+
setup_simple_project(config=config, mkdir_newsfragments=mkdir)
4526

4627
args = ["123.feature.rst"]
4728
if content is None:
@@ -95,7 +76,7 @@ def test_edit_abort(self):
9576
runner = CliRunner()
9677

9778
with runner.isolated_filesystem():
98-
setup_simple_project(config=None, mkdir=True)
79+
setup_simple_project(config=None, mkdir_newsfragments=True)
9980
result = runner.invoke(_main, ["123.feature.rst", "--edit"])
10081
self.assertEqual([], os.listdir("foo/newsfragments"))
10182
self.assertEqual(1, result.exit_code)
@@ -141,7 +122,7 @@ def test_different_directory(self):
141122
)
142123

143124
with runner.isolated_filesystem():
144-
setup_simple_project(config, mkdir=False)
125+
setup_simple_project(config=config, mkdir_newsfragments=False)
145126
os.mkdir("releasenotes")
146127

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

0 commit comments

Comments
 (0)