Skip to content

Commit 0400aa5

Browse files
test: add codebase.create_pr tests (#422)
1 parent ad71d73 commit 0400aa5

File tree

6 files changed

+68
-10
lines changed

6 files changed

+68
-10
lines changed

src/codegen/git/repo_operator/local_repo_operator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from codegen.git.schemas.repo_config import RepoConfig
1616
from codegen.git.utils.clone_url import url_to_github
1717
from codegen.git.utils.file_utils import create_files
18-
from codegen.shared.configs.config import config
1918

2019
logger = logging.getLogger(__name__)
2120

@@ -41,7 +40,7 @@ def __init__(
4140
github_api_key: str | None = None,
4241
bot_commit: bool = False,
4342
) -> None:
44-
self._github_api_key = github_api_key or config.secrets.github_token
43+
self._github_api_key = github_api_key
4544
self._remote_git_repo = None
4645
super().__init__(repo_config, bot_commit)
4746
os.makedirs(self.repo_path, exist_ok=True)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
3+
import pytest
4+
from git import Repo as GitRepo
5+
6+
from codegen.git.repo_operator.local_repo_operator import LocalRepoOperator
7+
from codegen.git.schemas.repo_config import RepoConfig
8+
from codegen.git.utils.clone_url import get_authenticated_clone_url_for_repo_config
9+
from codegen.sdk.codebase.config import ProjectConfig
10+
from codegen.sdk.core.codebase import Codebase
11+
from codegen.shared.configs.config import config
12+
13+
14+
@pytest.fixture
15+
def repo_config(tmpdir):
16+
repo_config = RepoConfig(
17+
name="Kevin-s-Adventure-Game",
18+
full_name="codegen-sh/Kevin-s-Adventure-Game",
19+
organization_name="codegen-sh",
20+
base_dir=str(tmpdir),
21+
)
22+
yield repo_config
23+
24+
25+
@pytest.fixture
26+
def op(repo_config):
27+
os.chdir(repo_config.base_dir)
28+
GitRepo.clone_from(url=get_authenticated_clone_url_for_repo_config(repo_config, token=config.secrets.github_token), to_path=os.path.join(repo_config.base_dir, repo_config.name), depth=1)
29+
op = LocalRepoOperator(repo_config=repo_config, github_api_key=config.secrets.github_token)
30+
yield op
31+
32+
33+
@pytest.fixture
34+
def codebase(op: LocalRepoOperator):
35+
project_config = ProjectConfig(repo_operator=op)
36+
codebase = Codebase(projects=[project_config])
37+
yield codebase
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import uuid
2+
3+
import pytest
4+
5+
from codegen.sdk.core.codebase import Codebase
6+
7+
8+
def test_codebase_create_pr_active_branch(codebase: Codebase):
9+
head = f"test-create-pr-{uuid.uuid4()}"
10+
codebase.checkout(branch=head, create_if_missing=True)
11+
codebase.files[0].remove()
12+
codebase.commit()
13+
pr = codebase.create_pr(title="test-create-pr title", body="test-create-pr body")
14+
assert pr.title == "test-create-pr title"
15+
assert pr.body == "test-create-pr body"
16+
assert pr.draft is False
17+
assert pr.state == "open"
18+
assert pr.head.ref == head
19+
assert pr.base.ref == "main"
20+
21+
22+
def test_codebase_create_pr_detached_head(codebase: Codebase):
23+
codebase.checkout(commit=codebase._op.git_cli.head.commit) # move to detached head state
24+
with pytest.raises(ValueError) as exc_info:
25+
codebase.create_pr(title="test-create-pr title", body="test-create-pr body")
26+
assert "Cannot make a PR from a detached HEAD" in str(exc_info.value)

tests/integration/codegen/git/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from codegen.git.schemas.repo_config import RepoConfig
66

77

8-
@pytest.fixture(autouse=True)
8+
@pytest.fixture()
99
def mock_config():
1010
"""Mock Config instance to prevent actual environment variable access during tests."""
1111
mock_config = MagicMock()
@@ -14,7 +14,7 @@ def mock_config():
1414
yield mock_config
1515

1616

17-
@pytest.fixture(autouse=True)
17+
@pytest.fixture()
1818
def repo_config(tmpdir):
1919
repo_config = RepoConfig(
2020
name="Kevin-s-Adventure-Game",

tests/integration/codegen/git/repo_operator/test_remote_repo_operator.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212

1313
@pytest.fixture
1414
def op(repo_config, request):
15-
op = RemoteRepoOperator(
16-
repo_config,
17-
shallow=request.param if hasattr(request, "param") else True,
18-
bot_commit=False,
19-
)
15+
op = RemoteRepoOperator(repo_config, shallow=request.param if hasattr(request, "param") else True, bot_commit=False)
2016
yield op
2117

2218

tests/integration/codegen/runner/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_free_port():
2222
return port
2323

2424

25-
@pytest.fixture(autouse=True)
25+
@pytest.fixture()
2626
def repo_config(tmpdir) -> Generator[RepoConfig, None, None]:
2727
yield RepoConfig(
2828
name="Kevin-s-Adventure-Game",

0 commit comments

Comments
 (0)