|
| 1 | +"""End-to-end tests for ghpr clone. |
| 2 | +
|
| 3 | +These tests require `gh` CLI authentication and network access. |
| 4 | +Skip with: pytest -m 'not e2e' |
| 5 | +""" |
| 6 | + |
| 7 | +import os |
| 8 | +import subprocess |
| 9 | +from pathlib import Path |
| 10 | +from shutil import which |
| 11 | +from tempfile import TemporaryDirectory |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | + |
| 16 | +def gh_authenticated() -> bool: |
| 17 | + """Check if `gh` CLI is installed and authenticated.""" |
| 18 | + if not which('gh'): |
| 19 | + return False |
| 20 | + try: |
| 21 | + result = subprocess.run( |
| 22 | + ['gh', 'auth', 'status'], |
| 23 | + capture_output=True, timeout=10, |
| 24 | + ) |
| 25 | + return result.returncode == 0 |
| 26 | + except Exception: |
| 27 | + return False |
| 28 | + |
| 29 | + |
| 30 | +requires_gh = pytest.mark.skipif( |
| 31 | + not gh_authenticated(), |
| 32 | + reason='requires authenticated gh CLI', |
| 33 | +) |
| 34 | +e2e = pytest.mark.e2e |
| 35 | + |
| 36 | + |
| 37 | +@requires_gh |
| 38 | +@e2e |
| 39 | +class TestCloneE2E: |
| 40 | + """End-to-end clone tests using real GitHub PRs.""" |
| 41 | + |
| 42 | + def test_clone_pr_no_gist(self): |
| 43 | + """Clone a known PR with --no-gist, verify directory structure.""" |
| 44 | + with TemporaryDirectory() as tmpdir: |
| 45 | + result = subprocess.run( |
| 46 | + [ |
| 47 | + 'ghpr', 'clone', |
| 48 | + 'https://github.com/runsascoded/ghpr/pull/6', |
| 49 | + '--no-gist', |
| 50 | + '-d', os.path.join(tmpdir, 'pr6'), |
| 51 | + ], |
| 52 | + capture_output=True, text=True, timeout=30, |
| 53 | + ) |
| 54 | + assert result.returncode == 0, f"clone failed: {result.stderr}" |
| 55 | + |
| 56 | + pr_dir = Path(tmpdir) / 'pr6' |
| 57 | + assert pr_dir.is_dir() |
| 58 | + |
| 59 | + # Should have a description file |
| 60 | + desc_files = list(pr_dir.glob('*.md')) |
| 61 | + assert len(desc_files) >= 1, f"No .md files in {pr_dir}" |
| 62 | + |
| 63 | + # Description file should be repo#number.md |
| 64 | + desc_names = [f.name for f in desc_files if not f.name.startswith('z')] |
| 65 | + assert 'ghpr#6.md' in desc_names |
| 66 | + |
| 67 | + # Should have git config set |
| 68 | + owner = subprocess.run( |
| 69 | + ['git', 'config', 'pr.owner'], |
| 70 | + capture_output=True, text=True, cwd=pr_dir, |
| 71 | + ).stdout.strip() |
| 72 | + assert owner == 'runsascoded' |
| 73 | + |
| 74 | + repo = subprocess.run( |
| 75 | + ['git', 'config', 'pr.repo'], |
| 76 | + capture_output=True, text=True, cwd=pr_dir, |
| 77 | + ).stdout.strip() |
| 78 | + assert repo == 'ghpr' |
| 79 | + |
| 80 | + pr_type = subprocess.run( |
| 81 | + ['git', 'config', 'pr.type'], |
| 82 | + capture_output=True, text=True, cwd=pr_dir, |
| 83 | + ).stdout.strip() |
| 84 | + assert pr_type == 'pr' |
| 85 | + |
| 86 | + def test_clone_pr_with_gist_reuse(self): |
| 87 | + """Clone a PR that has an existing gist footer; verify gist is reused.""" |
| 88 | + with TemporaryDirectory() as tmpdir: |
| 89 | + result = subprocess.run( |
| 90 | + [ |
| 91 | + 'ghpr', 'clone', |
| 92 | + 'https://github.com/marin-community/marin/pull/1723', |
| 93 | + '--no-comments', |
| 94 | + '-d', os.path.join(tmpdir, 'pr1723'), |
| 95 | + ], |
| 96 | + capture_output=True, text=True, timeout=60, |
| 97 | + ) |
| 98 | + assert result.returncode == 0, f"clone failed: {result.stderr}" |
| 99 | + |
| 100 | + pr_dir = Path(tmpdir) / 'pr1723' |
| 101 | + assert pr_dir.is_dir() |
| 102 | + |
| 103 | + # Should have detected and reused existing gist |
| 104 | + assert 'Found existing gist' in result.stderr, ( |
| 105 | + f"Expected gist reuse message in stderr:\n{result.stderr}" |
| 106 | + ) |
| 107 | + |
| 108 | + # Gist remote should be configured |
| 109 | + gist_remote = subprocess.run( |
| 110 | + ['git', 'config', 'pr.gist'], |
| 111 | + capture_output=True, text=True, cwd=pr_dir, |
| 112 | + ).stdout.strip() |
| 113 | + assert gist_remote, "pr.gist config not set (gist not reused)" |
| 114 | + |
| 115 | + # Gist should be added as a git remote |
| 116 | + remotes = subprocess.run( |
| 117 | + ['git', 'remote', '-v'], |
| 118 | + capture_output=True, text=True, cwd=pr_dir, |
| 119 | + ).stdout |
| 120 | + assert 'gist.github.com' in remotes |
| 121 | + |
| 122 | + def test_clone_issue_no_gist(self): |
| 123 | + """Clone a known issue, verify it's detected as an issue.""" |
| 124 | + with TemporaryDirectory() as tmpdir: |
| 125 | + result = subprocess.run( |
| 126 | + [ |
| 127 | + 'ghpr', 'clone', |
| 128 | + 'https://github.com/marin-community/marin/issues/1773', |
| 129 | + '--no-gist', '--no-comments', |
| 130 | + '-d', os.path.join(tmpdir, 'issue1773'), |
| 131 | + ], |
| 132 | + capture_output=True, text=True, timeout=30, |
| 133 | + ) |
| 134 | + assert result.returncode == 0, f"clone failed: {result.stderr}" |
| 135 | + |
| 136 | + pr_dir = Path(tmpdir) / 'issue1773' |
| 137 | + assert pr_dir.is_dir() |
| 138 | + |
| 139 | + pr_type = subprocess.run( |
| 140 | + ['git', 'config', 'pr.type'], |
| 141 | + capture_output=True, text=True, cwd=pr_dir, |
| 142 | + ).stdout.strip() |
| 143 | + assert pr_type == 'issue' |
0 commit comments