from pathlib import Path import unittest REPO_ROOT = Path(__file__).resolve().parents[1] WORKFLOWS = REPO_ROOT / ".gitea" / "workflows" class WorkflowPipelineTest(unittest.TestCase): def read_workflow(self, name: str) -> str: return (WORKFLOWS / name).read_text(encoding="utf-8") def test_prepare_fetches_once_into_shared_bare_repository(self) -> None: workflow = self.read_workflow("prepare.yml") self.assertIn('group: prepare-${{ github.repository }}', workflow) self.assertIn('cancel-in-progress: false', workflow) self.assertIn('REPOSITORY_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/repository.git"', workflow) self.assertIn('git init --bare "$REPOSITORY_DIR"', workflow) self.assertIn('git --git-dir="$REPOSITORY_DIR" fetch', workflow) self.assertNotIn("/tmp/reused-repo", workflow) self.assertNotIn("git clone", workflow) def test_prepare_leaves_worktree_ownership_to_consumers(self) -> None: workflow = self.read_workflow("prepare.yml") self.assertNotIn("ensure_worktree", workflow) self.assertNotIn("worktree add", workflow) self.assertNotIn("worktree remove", workflow) def assert_print_workflow(self, filename: str, role: str) -> None: workflow = self.read_workflow(filename) self.assertIn('workflows: ["Prepare"]', workflow) self.assertIn("github.event.workflow_run.conclusion == 'success'", workflow) self.assertIn(f'group: {role}-${{{{ github.repository }}}}', workflow) self.assertIn('cancel-in-progress: false', workflow) self.assertIn('HEAD_SHA="${{ github.event.workflow_run.head_sha }}"', workflow) self.assertIn('REPOSITORY_DIR="${WORKSPACE_ROOT}/${REPO_NAME}/repository.git"', workflow) self.assertIn(f'WORKTREE_DIR="${{WORKSPACE_ROOT}}/${{REPO_NAME}}/worktrees/{role}"', workflow) self.assertIn('WORKTREE_LOCK="${WORKSPACE_ROOT}/${REPO_NAME}/worktree-admin.lock"', workflow) self.assertGreaterEqual(workflow.count('if [ ! -f "$WORKTREE_DIR/.git" ]; then'), 2) self.assertIn('exec 9>"$WORKTREE_LOCK"', workflow) self.assertIn("flock 9", workflow) self.assertIn( 'git --git-dir="$REPOSITORY_DIR" worktree add --detach "$WORKTREE_DIR" "$HEAD_SHA"', workflow, ) self.assertIn('git -C "$WORKTREE_DIR" checkout --detach --force "$HEAD_SHA"', workflow) self.assertIn('git -C "$WORKTREE_DIR" reset --hard "$HEAD_SHA"', workflow) self.assertIn('git -C "$WORKTREE_DIR" clean -ffdx', workflow) self.assertIn('git -C "$WORKTREE_DIR" log -1 --oneline', workflow) self.assertNotIn("git fetch", workflow) self.assertNotIn("git pull", workflow) def test_print_one_uses_its_persistent_worktree(self) -> None: self.assert_print_workflow("print1.yml", "print1") def test_print_two_uses_its_persistent_worktree(self) -> None: self.assert_print_workflow("print2.yml", "print2") if __name__ == "__main__": unittest.main()