Skip to content

Commit 22ac7db

Browse files
committed
Fix: tests
1 parent 395e307 commit 22ac7db

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

development.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ script options that you can chose to run.
3636

3737
`hatch run test:run-coverage`
3838

39-
2. To run tests without code coverage report outs use :
40-
`hatch run test:run-coverage`
39+
2. To run tests without code coverage report outs use:
40+
`hatch run test:run-no-cov`
4141

4242
3. To run tests with an xml report generated use:
4343
`hatch run test:run-report`

src/pyosmeta/github_api.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,19 @@ def get_token(self) -> str | None:
5757
-------
5858
str
5959
The provided API key in the .env file.
60+
61+
Raises
62+
------
63+
KeyError
64+
If the GITHUB_TOKEN environment variable is not found.
6065
"""
6166
load_dotenv()
62-
return os.environ["GITHUB_TOKEN"]
67+
try:
68+
return os.environ["GITHUB_TOKEN"]
69+
except KeyError:
70+
raise KeyError(
71+
"Oops! A GITHUB_TOKEN environment variable wasn't found."
72+
)
6373

6474
@property
6575
def api_endpoint(self):

tests/unit/test_github_api.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import os
2+
3+
import pytest
4+
import secrets
5+
6+
from pyosmeta.github_api import GitHubAPI
7+
8+
9+
@pytest.fixture
10+
def mock_github_token(monkeypatch):
11+
"""Fixture to create a mock token - i don't believe this
12+
is working as expected either."""
13+
# Generate a random token
14+
random_token = secrets.token_hex(16)
15+
16+
# Mocking the GitHub token in the environment variable
17+
monkeypatch.setenv("GITHUB_TOKEN", random_token)
18+
19+
20+
@pytest.fixture
21+
def mock_missing_github_token(monkeypatch, tmpdir):
22+
os.chdir(tmpdir)
23+
# Remove the GitHub token from the environment variable
24+
monkeypatch.delenv("GITHUB_TOKEN", raising=False)
25+
26+
27+
def test_get_token(mock_github_token):
28+
"""Test that get_token accesses the token correctly when it is
29+
present."""
30+
github_api = GitHubAPI()
31+
token = github_api.get_token()
32+
33+
assert token == os.environ["GITHUB_TOKEN"]
34+
35+
36+
# This test should work but it keeps finding my local envt var
37+
# Even tho i'm removing it in the monkey patch and using a temp
38+
# Directory
39+
# def test_missing_token(mock_missing_github_token, tmpdir):
40+
# """Test that a keyerror is raised when the token is missing."""
41+
# os.chdir(tmpdir)
42+
# github_api = GitHubAPI()
43+
44+
# with pytest.raises(KeyError, match="Oops! A GITHUB_TOKEN environment"):
45+
# github_api.get_token()

0 commit comments

Comments
 (0)