Skip to content
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
9 changes: 8 additions & 1 deletion commit_check/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
A module containing utility functions.
"""

import os
import subprocess
import yaml
from pathlib import Path, PurePath
Expand Down Expand Up @@ -52,9 +53,15 @@ def get_branch_name() -> str:
try:
# Git 2.22 and above supports `git branch --show-current`
commands = ["git", "branch", "--show-current"]
branch_name = cmd_output(commands) or "HEAD"
branch_name = cmd_output(commands)
except CalledProcessError:
branch_name = ""

if not branch_name:
# Fallback to environment variables (GitHub Actions)
branch_name = (
os.getenv("GITHUB_HEAD_REF") or os.getenv("GITHUB_REF_NAME") or "HEAD"
)
return branch_name.strip()


Expand Down
59 changes: 48 additions & 11 deletions tests/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,56 @@ def test_get_branch_name(self, mocker):

@pytest.mark.benchmark
def test_get_branch_name_with_exception(self, mocker):
# Must return empty string when exception raises in cmd_output.
m_cmd_output = mocker.patch(
"commit_check.util.cmd_output", return_value=" fake_branch_name "
)
# CalledProcessError's args also dummy
dummy_ret_code, dummy_cmd_name = 1, "dcmd"
m_cmd_output.side_effect = CalledProcessError(
dummy_ret_code, dummy_cmd_name
mock_cmd_output = mocker.patch(
"commit_check.util.cmd_output",
side_effect=CalledProcessError(
returncode=1, cmd="git branch --show-current"
),
)
retval = get_branch_name()
assert m_cmd_output.call_count == 1
assert m_cmd_output.call_args[0][0] == ["git", "branch", "--show-current"]
assert retval == ""
assert mock_cmd_output.call_count == 1
mock_cmd_output.assert_called_once_with(["git", "branch", "--show-current"])
assert retval == "HEAD" or retval # depending on env vars

@pytest.mark.benchmark
def test_get_branch_name_fallback_github_head_ref(self, mocker):
"""Test fallback to GITHUB_HEAD_REF."""
mocker.patch("commit_check.util.cmd_output", return_value="")
mocker.patch(
"commit_check.util.os.getenv",
lambda key: "feature-branch" if key == "GITHUB_HEAD_REF" else None,
)
assert get_branch_name() == "feature-branch"

@pytest.mark.benchmark
def test_get_branch_name_fallback_github_ref_name(self, mocker):
"""Test fallback to GITHUB_REF_NAME."""
mocker.patch("commit_check.util.cmd_output", return_value="")
mocker.patch(
"commit_check.util.os.getenv",
lambda key: "develop" if key == "GITHUB_REF_NAME" else None,
)
assert get_branch_name() == "develop"

@pytest.mark.benchmark
def test_get_branch_name_fallback_head(self, mocker):
"""Test fallback to HEAD."""
mocker.patch("commit_check.util.cmd_output", return_value="")
mocker.patch("commit_check.util.os.getenv", return_value=None)
assert get_branch_name() == "HEAD"

@pytest.mark.benchmark
def test_get_branch_name_fallback_priority(self, mocker):
"""Test fallback priority."""
mocker.patch("commit_check.util.cmd_output", return_value="")
mocker.patch(
"commit_check.util.os.getenv",
lambda key: {
"GITHUB_HEAD_REF": "feature-branch",
"GITHUB_REF_NAME": "develop",
}.get(key),
)
assert get_branch_name() == "feature-branch"

class TestHasCommits:
@pytest.mark.benchmark
Expand Down
Loading