Skip to content

Commit

Permalink
Skip tests that require access to the token
Browse files Browse the repository at this point in the history
Raise an exception when the Github token is not available and use it in
order to skip tests that require access to this token.
This will allow to run a smaller set of tests from other forks and, most
importantly, won't block their merge.
  • Loading branch information
tuliom committed Feb 4, 2025
1 parent f5421dc commit c770c07
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions snapshot_manager/snapshot_manager/github_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class Reaction(enum.StrEnum):
EYES = "EYES" # Represents the :eyes: emoji.


class MissingToken(Exception):
"""Could not retrieve a Github token."""


class GithubClient:
dirname = pathlib.Path(os.path.dirname(__file__))

Expand All @@ -52,6 +56,10 @@ def __init__(self, config: config.Config, github_token: str = None, **kwargs):
f"Reading Github token from this environment variable: {self.config.github_token_env}"
)
github_token = os.getenv(self.config.github_token_env)
if github_token is None or len(github_token) == 0:
# We can't proceed without a Github token, otherwise we'll trigger
# an assertion failure.
raise MissingToken("Could not retrieve the token")
auth = github.Auth.Token(github_token)
self.github = github.Github(auth=auth)
self.gql = github_graphql.GithubGraphQL(token=github_token, raise_on_error=True)
Expand Down
16 changes: 14 additions & 2 deletions snapshot_manager/tests/github_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ def test_get_todays_issue(self):
cfg = self.config
cfg.datetime = datetime.datetime(year=2024, month=2, day=27)
self.assertEqual("20240227", cfg.yyyymmdd)
gh = github_util.GithubClient(config=cfg)

try:
gh = github_util.GithubClient(config=cfg)
except github_util.MissingToken:
pytest.skip(
"Skip test because this execution doesn't have access to a Github token"
)

issue = gh.get_todays_github_issue(
strategy="big-merge", github_repo="fedora-llvm-team/llvm-snapshots"
Expand Down Expand Up @@ -104,7 +110,13 @@ def test_flip_test_label(self):
pass

def test_get_workflow(self):
gh = github_util.GithubClient(config=self.config)
try:
gh = github_util.GithubClient(config=self.config)
except github_util.MissingToken:
pytest.skip(
"Skip test because this execution doesn't have access to a Github token"
)

repo = gh.github.get_repo("fedora-llvm-team/llvm-snapshots")
workflow = repo.get_workflow("check-snapshots.yml")
self.assertIsNotNone(workflow)
Expand Down
9 changes: 8 additions & 1 deletion snapshot_manager/tests/testing_farm_util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime

import pytest
import tests.base_test as base_test

import snapshot_manager.github_util as github_util
Expand All @@ -13,7 +14,13 @@ def test_make_with_missing_compose(self):
cfg = self.config
cfg.datetime = datetime.datetime(year=2024, month=2, day=27)
self.assertEqual("20240227", cfg.yyyymmdd)
gh = github_util.GithubClient(config=cfg)

try:
gh = github_util.GithubClient(config=cfg)
except github_util.MissingToken:
pytest.skip(
"Skip test because this execution doesn't have access to a Github token"
)

issue = gh.get_todays_github_issue(
strategy="big-merge", github_repo="fedora-llvm-team/llvm-snapshots"
Expand Down

0 comments on commit c770c07

Please sign in to comment.