Skip to content

Commit a69ded9

Browse files
author
tomcodgen
authored
skip tests requiring auth, remove hardcoded gh install (#47)
1 parent d825f76 commit a69ded9

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

tests/conftest.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import logging
22
import os
3+
from pathlib import Path
4+
5+
import pytest
36

47
from tests.shared.codemod.models import Size
58

@@ -74,3 +77,39 @@ def pytest_configure(config):
7477
filename=f"build/logs/tests_{worker_id}.log",
7578
level=config.getini("log_file_level"),
7679
)
80+
81+
82+
def is_git_lfs_pointer(file_path: Path) -> bool:
83+
"""Check if a file is a git LFS pointer file"""
84+
try:
85+
with open(file_path) as f:
86+
first_line = f.readline().strip()
87+
return first_line == "version https://git-lfs.github.com/spec/v1"
88+
except Exception:
89+
return False
90+
91+
92+
@pytest.hookimpl(hookwrapper=True)
93+
def pytest_runtest_makereport(item, call):
94+
outcome = yield
95+
report = outcome.get_result()
96+
97+
if report.when == "call" and report.failed:
98+
if "NodeJS or npm is not installed" in str(report.longrepr):
99+
pytest.skip("Test requires NodeJS and npm to be installed")
100+
101+
102+
@pytest.fixture(autouse=True)
103+
def skip_lfs_tests(request):
104+
"""Skip tests that depend on git LFS files if they haven't been pulled"""
105+
# Get the test module path
106+
test_path = Path(request.module.__file__)
107+
108+
# Only run for integration tests
109+
if not str(test_path).startswith(str(Path.cwd() / "tests" / "integration")):
110+
return
111+
112+
# Get the expected diff path from the test's expected fixture
113+
expected = request.getfixturevalue("expected")
114+
if isinstance(expected, Path) and is_git_lfs_pointer(expected):
115+
pytest.skip(f"Test requires git LFS file {expected} which hasn't been pulled")

tests/integration/codemod/conftest.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ def token(request):
141141
def op(repo: Repo, token: str | None) -> YieldFixture[LocalRepoOperator]:
142142
with filelock.FileLock(BASE_TMP_DIR / "locks" / repo.name):
143143
op = repo.to_op(repo.name, token)
144+
if isinstance(op, tuple):
145+
# This means we got (None, error_message)
146+
_, msg = op
147+
pytest.skip(msg or "Could not create repo operator")
144148
yield op
145149

146150

tests/shared/codemod/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,23 @@ class Repo(BaseModel):
6060
def from_json(cls, json_str: str) -> "Repo":
6161
return cls.model_validate(json.loads(json_str))
6262

63-
def to_op(self, name: str, token: str | None) -> LocalRepoOperator:
63+
def to_op(self, name: str, token: str | None) -> LocalRepoOperator | tuple[None, str | None]:
6464
base_path = BASE_TMP_DIR / ("extra_repos" if self.extra_repo else "oss_repos") / name
6565
base_path.mkdir(exist_ok=True, parents=True)
6666
url = self.url
6767
if token:
6868
url = url.replace("://", f"://{token}@")
6969
elif self.repo_id is not None:
70-
print("Setting up auth using the github cli")
70+
# TODO: this is a very messy hack to check whether we should prompt the user for auth
71+
# if REPO_ID_TO_URL is not set, we probably don't need auth. this is assuming that for
72+
# OSS repos, we don't need to pull any private repos.
73+
if not REPO_ID_TO_URL:
74+
return (None, "Could not create repo operator - skipping test")
75+
7176
if not which("gh"):
72-
os.system("brew install gh")
77+
return (None, "GitHub CLI (gh) is not installed. Please install it first. Skipping test.")
7378
if '[credential "https://github.codegen.app"]' not in (Path.home() / ".gitconfig").read_text():
79+
print("Setting up auth using the github cli")
7480
os.system("gh auth login -h github.codegen.app")
7581
os.system("gh auth setup-git -h github.codegen.app")
7682
return LocalRepoOperator.create_from_commit(str(base_path), self.default_branch, self.commit, url)

uv.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)