Skip to content

Commit ccedc04

Browse files
authored
Merge pull request #414 from twisted/drop-mock
Refactor _get_default_compare_branch
2 parents 3f532de + f7492ec commit ccedc04

3 files changed

Lines changed: 11 additions & 24 deletions

File tree

src/towncrier/check.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ def _run(args, **kwargs):
1919
return check_output(args, **kwargs)
2020

2121

22-
def get_default_compare_branch(base_directory, encoding):
23-
branches = (
24-
_run(["git", "branch", "-r"], cwd=base_directory).decode(encoding).splitlines()
25-
)
26-
branches = [branch.strip() for branch in branches]
22+
def _get_remote_branches(base_directory, encoding):
23+
output = _run(["git", "branch", "-r"], cwd=base_directory).decode(encoding)
24+
25+
return [branch.strip() for branch in output.splitlines()]
26+
27+
28+
def _get_default_compare_branch(branches):
2729
if "origin/main" in branches:
2830
return "origin/main"
2931
if "origin/master" in branches:
@@ -78,8 +80,8 @@ def __main(comparewith, directory, config):
7880
# and also some CI such as GitHub Actions).
7981
encoding = getattr(sys.stdout, "encoding", "utf8")
8082
if comparewith is None:
81-
comparewith = get_default_compare_branch(
82-
base_directory=base_directory, encoding=encoding
83+
comparewith = _get_default_compare_branch(
84+
_get_remote_branches(base_directory=base_directory, encoding=encoding)
8385
)
8486

8587
if comparewith is None:

src/towncrier/newsfragments/414.misc

Whitespace-only changes.

src/towncrier/test/test_check.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import sys
77

88
from subprocess import PIPE, Popen, call
9-
from unittest.mock import patch
109

1110
from click.testing import CliRunner
1211
from twisted.trial.unittest import TestCase
@@ -292,28 +291,14 @@ def test_get_default_compare_branch_main(self):
292291
"""
293292
If there's a remote branch origin/main, prefer it over everything else.
294293
"""
295-
runner = CliRunner()
296-
297-
with runner.isolated_filesystem():
298-
create_project()
299-
300-
with patch("towncrier.check._run") as m:
301-
m.return_value = b" origin/master\n origin/main\n\n"
302-
branch = check.get_default_compare_branch(".", "utf-8")
294+
branch = check._get_default_compare_branch(["origin/master", "origin/main"])
303295

304296
self.assertEqual("origin/main", branch)
305297

306298
def test_get_default_compare_branch_fallback(self):
307299
"""
308300
If there's origin/master and no main, use it.
309301
"""
310-
runner = CliRunner()
311-
312-
with runner.isolated_filesystem():
313-
create_project()
314-
315-
with patch("towncrier.check._run") as m:
316-
m.return_value = b" origin/master\n origin/foo\n\n"
317-
branch = check.get_default_compare_branch(".", "utf-8")
302+
branch = check._get_default_compare_branch(["origin/master", "origin/foo"])
318303

319304
self.assertEqual("origin/master", branch)

0 commit comments

Comments
 (0)