Skip to content

Commit

Permalink
Merge pull request #414 from twisted/drop-mock
Browse files Browse the repository at this point in the history
Refactor _get_default_compare_branch
  • Loading branch information
hynek authored Sep 1, 2022
2 parents 3f532de + f7492ec commit ccedc04
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
16 changes: 9 additions & 7 deletions src/towncrier/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def _run(args, **kwargs):
return check_output(args, **kwargs)


def get_default_compare_branch(base_directory, encoding):
branches = (
_run(["git", "branch", "-r"], cwd=base_directory).decode(encoding).splitlines()
)
branches = [branch.strip() for branch in branches]
def _get_remote_branches(base_directory, encoding):
output = _run(["git", "branch", "-r"], cwd=base_directory).decode(encoding)

return [branch.strip() for branch in output.splitlines()]


def _get_default_compare_branch(branches):
if "origin/main" in branches:
return "origin/main"
if "origin/master" in branches:
Expand Down Expand Up @@ -78,8 +80,8 @@ def __main(comparewith, directory, config):
# and also some CI such as GitHub Actions).
encoding = getattr(sys.stdout, "encoding", "utf8")
if comparewith is None:
comparewith = get_default_compare_branch(
base_directory=base_directory, encoding=encoding
comparewith = _get_default_compare_branch(
_get_remote_branches(base_directory=base_directory, encoding=encoding)
)

if comparewith is None:
Expand Down
Empty file.
19 changes: 2 additions & 17 deletions src/towncrier/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import sys

from subprocess import PIPE, Popen, call
from unittest.mock import patch

from click.testing import CliRunner
from twisted.trial.unittest import TestCase
Expand Down Expand Up @@ -292,28 +291,14 @@ def test_get_default_compare_branch_main(self):
"""
If there's a remote branch origin/main, prefer it over everything else.
"""
runner = CliRunner()

with runner.isolated_filesystem():
create_project()

with patch("towncrier.check._run") as m:
m.return_value = b" origin/master\n origin/main\n\n"
branch = check.get_default_compare_branch(".", "utf-8")
branch = check._get_default_compare_branch(["origin/master", "origin/main"])

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

def test_get_default_compare_branch_fallback(self):
"""
If there's origin/master and no main, use it.
"""
runner = CliRunner()

with runner.isolated_filesystem():
create_project()

with patch("towncrier.check._run") as m:
m.return_value = b" origin/master\n origin/foo\n\n"
branch = check.get_default_compare_branch(".", "utf-8")
branch = check._get_default_compare_branch(["origin/master", "origin/foo"])

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

0 comments on commit ccedc04

Please sign in to comment.