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
18 changes: 15 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import subprocess
import sys
import tempfile
from typing import TextIO

# Constants for message titles
Expand Down Expand Up @@ -234,6 +235,17 @@ def build_check_args() -> list[str]:
return [flag for flag, enabled in flags if enabled]


def get_result_path() -> str:
"""Return a safe path for the result file using a temp directory.

In GitHub Actions this uses ``RUNNER_TEMP`` which is cleaned up
automatically after the job. Falls back to ``tempfile.gettempdir()``
for local testing.
"""
base = os.environ.get("RUNNER_TEMP") or tempfile.gettempdir()
return os.path.join(base, "commit-check-result.txt")


def run_commit_check() -> int:
"""Runs all enabled checks and returns the overall exit code.

Expand All @@ -248,7 +260,7 @@ def run_commit_check() -> int:
exit_code = 0
emitted_failure_output = False

with open("result.txt", "w", encoding="utf-8") as result_file:
with open(get_result_path(), "w", encoding="utf-8") as result_file:
# ---- 1. PR title check ------------------------------------------------
# Always label the PR title section and suppress its banner so the
# output flows consistently with the commit-message section labels:
Expand Down Expand Up @@ -294,8 +306,8 @@ def run_commit_check() -> int:

def read_result_file() -> str | None:
"""Reads the result.txt file and removes ANSI color codes."""
if os.path.getsize("result.txt") > 0:
with open("result.txt", "r", encoding="utf-8") as result_file:
if os.path.getsize(get_result_path()) > 0:
with open(get_result_path(), "r", encoding="utf-8") as result_file:
result_text = re.sub(
r"\x1B\[[0-9;]*[a-zA-Z]", "", result_file.read()
) # Remove ANSI colors
Expand Down
22 changes: 16 additions & 6 deletions main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,12 @@ def setUp(self):
import tempfile

self._tmpdir = tempfile.mkdtemp()
os.environ["RUNNER_TEMP"] = self._tmpdir
os.chdir(self._tmpdir)

def tearDown(self):
os.chdir(self._orig_dir)
os.environ.pop("RUNNER_TEMP", None)

def test_pr_path_calls_pr_message_checks(self):
with (
Expand Down Expand Up @@ -433,7 +435,7 @@ def test_pr_title_check_runs_when_enabled(self):
self.assertEqual(rc, 0)
self.assertEqual(
mock_cmd.call_args[0][0],
["--message"],
["--message", "--no-banner"],
)
self.assertEqual(mock_cmd.call_args[1]["input_text"], "feat: a feature")

Expand Down Expand Up @@ -528,7 +530,7 @@ def test_result_txt_is_created(self):
patch("main.run_check_command", return_value=0),
):
main.run_commit_check()
self.assertTrue(os.path.exists(os.path.join(self._tmpdir, "result.txt")))
self.assertTrue(os.path.exists(main.get_result_path()))

def test_other_args_excludes_message(self):
captured_args = []
Expand Down Expand Up @@ -557,13 +559,15 @@ def setUp(self):

self._orig_dir = os.getcwd()
self._tmpdir = tempfile.mkdtemp()
os.environ["RUNNER_TEMP"] = self._tmpdir
os.chdir(self._tmpdir)

def tearDown(self):
os.chdir(self._orig_dir)
os.environ.pop("RUNNER_TEMP", None)

def _write_result(self, content: str):
with open("result.txt", "w", encoding="utf-8") as file_obj:
with open(main.get_result_path(), "w", encoding="utf-8") as file_obj:
file_obj.write(content)

def test_empty_file_returns_none(self):
Expand Down Expand Up @@ -595,12 +599,14 @@ def setUp(self):

self._orig_dir = os.getcwd()
self._tmpdir = tempfile.mkdtemp()
os.environ["RUNNER_TEMP"] = self._tmpdir
os.chdir(self._tmpdir)
with open("result.txt", "w", encoding="utf-8"):
with open(main.get_result_path(), "w", encoding="utf-8"):
pass

def tearDown(self):
os.chdir(self._orig_dir)
os.environ.pop("RUNNER_TEMP", None)

def test_false_skips(self):
with patch("main.JOB_SUMMARY_ENABLED", False):
Expand Down Expand Up @@ -641,12 +647,14 @@ def setUp(self):

self._orig_dir = os.getcwd()
self._tmpdir = tempfile.mkdtemp()
os.environ["RUNNER_TEMP"] = self._tmpdir
os.chdir(self._tmpdir)
with open("result.txt", "w", encoding="utf-8"):
with open(main.get_result_path(), "w", encoding="utf-8"):
pass

def tearDown(self):
os.chdir(self._orig_dir)
os.environ.pop("RUNNER_TEMP", None)

def test_disabled_returns_zero(self):
with patch("main.PR_COMMENTS_ENABLED", False):
Expand Down Expand Up @@ -777,12 +785,14 @@ def setUp(self):

self._orig_dir = os.getcwd()
self._tmpdir = tempfile.mkdtemp()
os.environ["RUNNER_TEMP"] = self._tmpdir
os.chdir(self._tmpdir)
with open("result.txt", "w", encoding="utf-8"):
with open(main.get_result_path(), "w", encoding="utf-8"):
pass

def tearDown(self):
os.chdir(self._orig_dir)
os.environ.pop("RUNNER_TEMP", None)

def test_success_path(self):
with (
Expand Down
Loading