From 6a4db3001888f71fdccfffd1da861f0f4a4ba2de Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Thu, 1 Sep 2022 17:48:12 +0200 Subject: [PATCH 1/2] Refactor _get_default_compare_branch Make it a computation instead of an action and drop the patch/mock. --- src/towncrier/check.py | 16 +++++++++------- src/towncrier/test/test_check.py | 19 ++----------------- 2 files changed, 11 insertions(+), 24 deletions(-) diff --git a/src/towncrier/check.py b/src/towncrier/check.py index fb2a723a..3e40965c 100644 --- a/src/towncrier/check.py +++ b/src/towncrier/check.py @@ -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: @@ -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: diff --git a/src/towncrier/test/test_check.py b/src/towncrier/test/test_check.py index f918318a..6ad70f36 100644 --- a/src/towncrier/test/test_check.py +++ b/src/towncrier/test/test_check.py @@ -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 @@ -292,14 +291,7 @@ 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) @@ -307,13 +299,6 @@ 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) From f7492ecbaa30eb582fd4573525c98d8e8514ed8c Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Thu, 1 Sep 2022 17:54:26 +0200 Subject: [PATCH 2/2] Add newsfragment --- src/towncrier/newsfragments/414.misc | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/towncrier/newsfragments/414.misc diff --git a/src/towncrier/newsfragments/414.misc b/src/towncrier/newsfragments/414.misc new file mode 100644 index 00000000..e69de29b