Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor _get_default_compare_branch #414

Merged
merged 2 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)