Skip to content

Commit

Permalink
Fix issue with CI failing due to GitHub rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
tekktrik committed Mar 15, 2024
1 parent 66678e7 commit d44fbdd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
1 change: 0 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
max-parallel: 3
matrix:
py-version: [
"3.8",
Expand Down
2 changes: 1 addition & 1 deletion circfirm/cli/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def query_board_ids(regex: str) -> None:
circfirm.cli.maybe_support(
"Boards list will now be synchronized with the git repository."
)
if not gh_token:
if not gh_token: # pragma: no cover
circfirm.cli.maybe_support(
"Please note that this operation can only be performed 60 times per hour due to GitHub rate limiting."
)
Expand Down
2 changes: 0 additions & 2 deletions tests/backend/test_backend_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"""

import os
import pathlib
import shutil

import pytest

Expand Down
23 changes: 11 additions & 12 deletions tests/cli/test_cli_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ def simulate_no_connection(arg: str) -> NoReturn:
raise requests.ConnectionError


def test_query_board_ids(monkeypatch: pytest.MonkeyPatch) -> None:
@tests.helpers.with_token(os.environ["GH_TOKEN"])
def test_query_board_ids() -> None:
"""Tests the ability to query the boards using the CLI."""
# Test an unauthenticated request with supporting text
# Test an authenticated request with supporting text
board_ids = tests.helpers.get_board_ids_from_git()
pre_expected_output = "".join([f"{board}\n" for board in board_ids])
expected_output = "\n".join(
[
"Boards list will now be synchronized with the git repository.",
"Please note that this operation can only be performed 60 times per hour due to GitHub rate limiting.",
"Fetching boards list... done",
pre_expected_output,
]
Expand All @@ -45,26 +45,24 @@ def test_query_board_ids(monkeypatch: pytest.MonkeyPatch) -> None:
assert result.output == expected_output

# Test an authenticated request without supporting text
result = RUNNER.invoke(
cli, ["config", "edit", "token.github", os.environ["GH_TOKEN"]]
)
assert result.exit_code == 0
result = RUNNER.invoke(cli, ["config", "edit", "output.supporting.silence", "true"])
assert result.exit_code == 0
result = RUNNER.invoke(cli, ["query", "board-ids"])
assert result.exit_code == 0
assert result.output == pre_expected_output

# Test a request with a faulty token
result = RUNNER.invoke(cli, ["config", "edit", "token.github", "badtoken"])
assert result.exit_code == 0

@tests.helpers.with_token("badtoken")
def test_query_board_ids_bad_token() -> None:
"""Test a request with a faulty token."""
result = RUNNER.invoke(cli, ["query", "board-ids"])
assert result.exit_code != 0

result = RUNNER.invoke(cli, ["config", "reset"])
assert result.exit_code == 0

# Tests failure when cannot fetch results due to no network connection
@tests.helpers.with_token(os.environ["GH_TOKEN"], True)
def test_query_board_ids_no_network(monkeypatch: pytest.MonkeyPatch) -> None:
"""Tests failure when cannot fetch results due to no network connection."""
monkeypatch.setattr(
circfirm.backend.github, "get_board_id_list", simulate_no_connection
)
Expand Down Expand Up @@ -100,6 +98,7 @@ def test_query_versions() -> None:
assert result.exit_code == 0
assert result.output == expected_output

# Test the regex flag
result = RUNNER.invoke(
cli,
[
Expand Down
38 changes: 37 additions & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import platform
import shutil
import time
from typing import Callable, List, TypeVar
from typing import Any, Callable, Dict, List, TypeVar

import pytest
import yaml

import circfirm
import circfirm.backend
Expand Down Expand Up @@ -141,3 +144,36 @@ def get_board_ids_from_git() -> List[str]:
return sorted(
[board_path.name for board_path in board_paths if board_path.is_dir()]
)


def with_token(token: str, use_monkeypatch: bool = False) -> None:
"""Perform a test with the given token in the configuration settings."""

def set_token(new_token: str) -> str:
"""Set a new token."""
with open(circfirm.SETTINGS_FILE, encoding="utf-8") as setfile:
contents = yaml.safe_load(setfile)
prev_token = contents["token"]["github"]
contents["token"]["github"] = new_token
with open(circfirm.SETTINGS_FILE, mode="w", encoding="utf-8") as setfile:
yaml.safe_dump(contents, setfile)
return prev_token

def with_token_set(func: Callable) -> None:
def with_token_set_wrapper(*args: Any, **kwargs: Dict[str, Any]) -> None:
prev_token = set_token(token)
func(*args, **kwargs)
set_token(prev_token)

def with_token_set_wrapper_monkeypatch(
monkeypatch: pytest.MonkeyPatch, *args: Any, **kwargs: Dict[str, Any]
) -> None:
prev_token = set_token(token)
func(monkeypatch, *args, **kwargs)
set_token(prev_token)

if use_monkeypatch:
return with_token_set_wrapper_monkeypatch
return with_token_set_wrapper

return with_token_set

0 comments on commit d44fbdd

Please sign in to comment.