droboticflow/tests/release/test_mainline_guard.py
2026-11-10 01:35:25 -08:00

90 lines
3.6 KiB
Python

"""Release source identity checks use isolated Git repos, never a company remote."""
import importlib.util
from pathlib import Path
import subprocess
import tempfile
import unittest
from unittest.mock import patch
ROOT = Path(__file__).resolve().parents[2]
spec = importlib.util.spec_from_file_location('mainline_release', ROOT / 'scripts/release.py')
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
class MainlineGuardTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.base = Path(self.tmp.name)
self.repo = self.base / 'repo'
self.repo.mkdir()
self.git('init', '-q', '--bare', '-b', 'dev', str(self.base / 'origin.git'))
self.git('init', '-q', '-b', 'dev')
self.git('config', 'user.name', 'ReleaseTest')
self.git('config', 'user.email', 'test@example.invalid')
(self.repo / 'fixture').write_text('base\n')
self.git('add', 'fixture')
self.git('commit', '-qm', 'base')
self.git('remote', 'add', 'origin', str(self.base / 'origin.git'))
self.git('push', '-q', '-u', 'origin', 'dev')
self.commit = self.git('rev-parse', 'HEAD')
patcher = patch.object(m, 'ROOT', self.repo)
patcher.start()
self.addCleanup(patcher.stop)
def git(self, *args):
return subprocess.check_output(['git', *args], cwd=self.repo, text=True, stderr=subprocess.PIPE).strip()
def next_commit(self):
(self.repo / 'fixture').write_text('next\n')
self.git('add', 'fixture')
self.git('commit', '-qm', 'next')
return self.git('rev-parse', 'HEAD')
def test_synced_tip_and_detached_build_are_allowed(self):
m.require_mainline(self.commit, refresh=True)
self.git('switch', '--detach', self.commit)
m.require_mainline(self.commit)
def test_unmerged_feature_commit_is_blocked(self):
self.git('switch', '-c', 'feature')
candidate = self.next_commit()
with self.assertRaises(m.Blocked):
m.require_mainline(candidate)
def test_local_only_dev_commit_is_blocked(self):
candidate = self.next_commit()
with self.assertRaises(m.Blocked):
m.require_mainline(candidate)
def test_remote_advance_is_fetched_and_blocks_old_plan(self):
self.git('switch', '-c', 'feature')
newer = self.next_commit()
self.git('push', '-q', 'origin', 'feature')
subprocess.run(['git', '--git-dir', str(self.base / 'origin.git'), 'update-ref', 'refs/heads/dev', newer], check=True)
self.git('switch', 'dev')
self.assertEqual(self.git('rev-parse', 'origin/dev'), self.commit)
with self.assertRaises(m.Blocked):
m.require_mainline(self.commit, refresh=True)
self.assertEqual(self.git('rev-parse', 'origin/dev'), newer)
def test_missing_remote_or_local_dev_is_blocked(self):
self.git('update-ref', '-d', 'refs/remotes/origin/dev')
with self.assertRaises(m.Blocked):
m.require_mainline(self.commit)
self.git('update-ref', 'refs/remotes/origin/dev', self.commit)
self.git('switch', '--detach', self.commit)
self.git('branch', '-d', 'dev')
with self.assertRaises(m.Blocked):
m.require_mainline(self.commit)
def test_unreachable_remote_blocks_deployment_check(self):
self.git('remote', 'set-url', 'origin', str(self.base / 'missing.git'))
with self.assertRaises(m.Blocked):
m.require_mainline(self.commit, refresh=True)
if __name__ == '__main__':
unittest.main()