Skip to content

Commit 77109a6

Browse files
committed
Mock PyGithub in unit tests, rename API token env var
1 parent b523f8e commit 77109a6

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

cookie_python/manage/github.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
from functools import cached_property, lru_cache
44
from typing import Optional
55

6-
from github import Github
6+
import github
77
from github.PullRequest import PullRequest
88
from github.Repository import Repository
99

1010

1111
class GithubRepo:
1212
def __init__(self) -> None:
13-
self._gh = Github(os.environ["GITHUB_ACCESS_TOKEN"])
13+
self._gh = github.Github(os.environ["GITHUB_API_TOKEN"])
1414

1515
@cached_property
1616
def username(self) -> str:

tests/test_manage_cookie.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
from pathlib import Path
77
from types import SimpleNamespace
88
from typing import Any, Dict, Iterable, Iterator, Tuple
9-
from unittest.mock import patch
9+
from unittest.mock import MagicMock, patch
1010

11+
import github
1112
import pytest
1213

1314
from cookie_python.manage.main import main as manage_cookie_main
@@ -27,6 +28,7 @@ def environ() -> Iterator[None]:
2728
GIT_AUTHOR_EMAIL=AUTHOR_EMAIL,
2829
GIT_COMMITTER_NAME=AUTHOR_NAME,
2930
GIT_COMMITTER_EMAIL=AUTHOR_EMAIL,
31+
GITHUB_API_TOKEN="unittest_token",
3032
)
3133
)
3234
with patch.dict(os.environ, env):
@@ -62,6 +64,25 @@ def new_cookie(
6264
yield Path(temp_dir) / PROJECT_NAME
6365

6466

67+
@pytest.fixture(autouse=True)
68+
def mock_pygithub(new_cookie: Path) -> Iterator[MagicMock]:
69+
with patch.object(github, "Github") as obj:
70+
gh = obj.return_value
71+
gh.get_repo.return_value = SimpleNamespace(
72+
name=PROJECT_NAME,
73+
full_name=f"{AUTHOR_NAME}/{PROJECT_NAME}",
74+
ssh_url=str(new_cookie),
75+
get_pulls=MagicMock(
76+
return_value=[
77+
SimpleNamespace(
78+
url="https://unittest.example.com/repo/pulls/1138"
79+
)
80+
],
81+
),
82+
)
83+
yield obj
84+
85+
6586
@pytest.fixture
6687
def new_cookie_with_lock(new_cookie: Path, temp_dir: str) -> Iterator[Path]:
6788
for cmd in (

0 commit comments

Comments
 (0)