Files
ai-job-search/tests/test_upstream_checkout.py
T
52f0b2719b Fix fork-skip guards for local test runs (#524 rebased, ssh:// origin form folded in) (#563)
* Fix fork-skip guards for local test runs

The fork skip read GITHUB_REPOSITORY with an upstream fallback, so local runs on a fork always ran the upstream-only guards. Share one helper that checks the origin remote when the variable is unset. Fixes #496.

* test: accept the ssh:// origin form in the upstream-checkout helper

An upstream clone whose origin is ssh://git@github.com/MadsLorentzen/ai-job-search(.git)
must keep running the placeholder guards; the helper only knew the https and
scp-style forms. Folded in at maintainer rebase per the #524 review.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Omarinho12345 <omarottelli720@gmail.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-15 11:59:57 +02:00

106 lines
3.9 KiB
Python

"""Unit tests for the shared fork-detection helper (issue #496).
The placeholder guards must skip on personalized forks even in a local run
where GITHUB_REPOSITORY is unset. All branches are exercised with the env
and subprocess.run patched, so no git remote or CI variable is needed.
"""
import os
import subprocess
import unittest
from unittest import mock
try:
from tests import _upstream
from tests._upstream import is_upstream_checkout
except ImportError: # `python -m unittest discover -s tests` imports test modules top-level
import _upstream
from _upstream import is_upstream_checkout
UPSTREAM = "MadsLorentzen/ai-job-search"
def _no_github_env(testcase):
"""Clear GITHUB_REPOSITORY so the origin-remote branch is exercised."""
patcher = mock.patch.dict(os.environ)
env = patcher.start()
testcase.addCleanup(patcher.stop)
env.pop("GITHUB_REPOSITORY", None)
return env
def _completed(stdout="", returncode=0):
return subprocess.CompletedProcess(
args=["git", "remote", "get-url", "origin"],
returncode=returncode,
stdout=stdout,
)
class UpstreamCheckoutTests(unittest.TestCase):
def test_env_says_upstream(self):
with mock.patch.dict(os.environ, {"GITHUB_REPOSITORY": UPSTREAM}):
with mock.patch.object(_upstream.subprocess, "run") as run:
self.assertTrue(is_upstream_checkout())
run.assert_not_called()
def test_env_says_fork(self):
with mock.patch.dict(os.environ, {"GITHUB_REPOSITORY": "someone-else/ai-job-search"}):
with mock.patch.object(_upstream.subprocess, "run") as run:
self.assertFalse(is_upstream_checkout())
run.assert_not_called()
def test_no_env_and_origin_is_fork(self):
_no_github_env(self)
with mock.patch.object(
_upstream.subprocess, "run", return_value=_completed("git@github.com:someone-else/ai-job-search.git\n")
):
self.assertFalse(is_upstream_checkout())
def test_no_env_and_git_is_unavailable(self):
_no_github_env(self)
with mock.patch.object(
_upstream.subprocess, "run", side_effect=OSError("no git on PATH")
):
self.assertTrue(is_upstream_checkout())
def test_no_env_and_git_times_out(self):
_no_github_env(self)
with mock.patch.object(
_upstream.subprocess,
"run",
side_effect=subprocess.TimeoutExpired(cmd="git", timeout=10),
):
self.assertTrue(is_upstream_checkout())
def test_no_env_and_git_exits_nonzero(self):
_no_github_env(self)
with mock.patch.object(
_upstream.subprocess, "run", return_value=_completed("", returncode=128)
):
self.assertTrue(is_upstream_checkout())
def test_origin_url_forms(self):
cases = [
("https://github.com/MadsLorentzen/ai-job-search", True),
("https://github.com/MadsLorentzen/ai-job-search.git", True),
("git@github.com:MadsLorentzen/ai-job-search", True),
("git@github.com:MadsLorentzen/ai-job-search.git", True),
("ssh://git@github.com/MadsLorentzen/ai-job-search.git", True),
("ssh://git@github.com/MadsLorentzen/ai-job-search", True),
("ssh://git@github.com/someone-else/ai-job-search.git", False),
("https://github.com/someone-else/ai-job-search.git", False),
("git@github.com:someone-else/ai-job-search", False),
("https://gitlab.com/MadsLorentzen/ai-job-search.git", False),
]
for url, expected in cases:
with self.subTest(url=url):
_no_github_env(self)
with mock.patch.object(
_upstream.subprocess, "run", return_value=_completed(url + "\n")
):
self.assertEqual(is_upstream_checkout(), expected)
if __name__ == "__main__":
unittest.main()