Skip to content

skip tests requiring auth, remove hardcoded gh install #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
import os
from pathlib import Path

import pytest

from codegen.sdk.testing.models import Size

Expand Down Expand Up @@ -74,3 +77,39 @@ def pytest_configure(config):
filename=f"build/logs/tests_{worker_id}.log",
level=config.getini("log_file_level"),
)


def is_git_lfs_pointer(file_path: Path) -> bool:
"""Check if a file is a git LFS pointer file"""
try:
with open(file_path) as f:
first_line = f.readline().strip()
return first_line == "version https://git-lfs.github.com/spec/v1"
except Exception:
return False


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()

if report.when == "call" and report.failed:
if "NodeJS or npm is not installed" in str(report.longrepr):
pytest.skip("Test requires NodeJS and npm to be installed")


@pytest.fixture(autouse=True)
def skip_lfs_tests(request):
"""Skip tests that depend on git LFS files if they haven't been pulled"""
# Get the test module path
test_path = Path(request.module.__file__)

# Only run for integration tests
if not str(test_path).startswith(str(Path.cwd() / "tests" / "integration")):
return

# Get the expected diff path from the test's expected fixture
expected = request.getfixturevalue("expected")
if isinstance(expected, Path) and is_git_lfs_pointer(expected):
pytest.skip(f"Test requires git LFS file {expected} which hasn't been pulled")
12 changes: 9 additions & 3 deletions src/codegen/sdk/testing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,23 @@ class Repo(BaseModel):
def from_json(cls, json_str: str) -> "Repo":
return cls.model_validate(json.loads(json_str))

def to_op(self, name: str, token: str | None) -> LocalRepoOperator:
def to_op(self, name: str, token: str | None) -> LocalRepoOperator | tuple[None, str | None]:
base_path = BASE_TMP_DIR / ("extra_repos" if self.extra_repo else "oss_repos") / name
base_path.mkdir(exist_ok=True, parents=True)
url = self.url
if token:
url = url.replace("://", f"://{token}@")
elif self.repo_id is not None:
print("Setting up auth using the github cli")
# TODO: this is a very messy hack to check whether we should prompt the user for auth
# if REPO_ID_TO_URL is not set, we probably don't need auth. this is assuming that for
# OSS repos, we don't need to pull any private repos.
if not REPO_ID_TO_URL:
return (None, "Could not create repo operator - skipping test")

if not which("gh"):
os.system("brew install gh")
return (None, "GitHub CLI (gh) is not installed. Please install it first. Skipping test.")
if '[credential "https://github.codegen.app"]' not in (Path.home() / ".gitconfig").read_text():
print("Setting up auth using the github cli")
os.system("gh auth login -h github.codegen.app")
os.system("gh auth setup-git -h github.codegen.app")
return LocalRepoOperator.create_from_commit(str(base_path), self.default_branch, self.commit, url)
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/codemod/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def token(request):
def op(repo: Repo, token: str | None) -> YieldFixture[LocalRepoOperator]:
with filelock.FileLock(BASE_TMP_DIR / "locks" / repo.name):
op = repo.to_op(repo.name, token)
if isinstance(op, tuple):
# This means we got (None, error_message)
_, msg = op
pytest.skip(msg or "Could not create repo operator")
yield op


Expand Down
4 changes: 2 additions & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.