Skip to content

Commit

Permalink
Merge pull request #291 from tekktrik/dev/ignore-token-tests
Browse files Browse the repository at this point in the history
Allow for using pytest with/without tokens
  • Loading branch information
kattni authored Aug 23, 2022
2 parents 70cf49e + d12770f commit b5fc1d6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
20 changes: 18 additions & 2 deletions adabot/lib/circuitpython_library_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from adabot.lib import common_funcs
from adabot.lib import assign_hacktober_label as hacktober

GH_INTERFACE = pygithub.Github(os.environ["ADABOT_GITHUB_ACCESS_TOKEN"])
GH_INTERFACE = pygithub.Github(os.environ.get("ADABOT_GITHUB_ACCESS_TOKEN"))


# Define constants for error strings to make checking against them more robust:
Expand Down Expand Up @@ -176,6 +176,14 @@
"good first issue": {"color": "7057ff"},
}

_TOKEN_FUNCTIONS = []


def uses_token(func):
"""Decorator for recording functions that use tokens"""
_TOKEN_FUNCTIONS.append(func.__name__)
return func


class LibraryValidator:
"""Class to hold instance variables needed to traverse the calling
Expand Down Expand Up @@ -221,6 +229,12 @@ def rtd_yml_base(self):

return self._rtd_yaml_base

@staticmethod
def get_token_methods():
"""Return a list of method names that require authentication"""

return _TOKEN_FUNCTIONS

def run_repo_validation(self, repo):
"""Run all the current validation functions on the provided repository and
return their results as a list of string errors.
Expand Down Expand Up @@ -296,7 +310,7 @@ def validate_release_state(self, repo):
since the last release. Only files that drive user-facing changes
will be considered when flagging a repo as needing a release.
If 2), categorize by length of time passed since oldest commit after the release,
If 2), categorize by length of ti#me passed since oldest commit after the release,
and return the number of days that have passed since the oldest commit.
"""

Expand Down Expand Up @@ -802,6 +816,7 @@ def __check_lib_name(

return errors

@uses_token
def validate_readthedocs(self, repo):
"""Method to check the status of `repo`'s ReadTheDocs."""

Expand Down Expand Up @@ -1137,6 +1152,7 @@ def validate_labels(self, repo):

return errors

@uses_token
def validate_actions_state(self, repo):
"""Validate if the most recent GitHub Actions run on the default branch
has passed.
Expand Down
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""Configuration file for pytest (along with `pytest.ini`)"""


def pytest_addoption(parser):
"""Add options to the `pytest` command"""
parser.addoption(
"--use-tokens",
action="store_true",
default=False,
help="Test commands that use environment tokens",
)
38 changes: 34 additions & 4 deletions tests/integration/test_circuitpython_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from adabot import github_requests
from adabot import circuitpython_libraries

from adabot.lib import circuitpython_library_validators

# pylint: disable=unused-argument
def mock_list_repos(*args, **kwargs):
"""Function to monkeypatch `common_funcs.list_repos()` for a shorter set of repos."""
Expand All @@ -18,23 +20,51 @@ def mock_list_repos(*args, **kwargs):
]


def test_circuitpython_libraries(monkeypatch):
def test_circuitpython_libraries(monkeypatch, pytestconfig):
"""Test main function of 'circuitpyton_libraries.py', without writing an output file."""

monkeypatch.setattr(common_funcs, "list_repos", mock_list_repos)

circuitpython_libraries.main(validator="all")
# Delete specific tests that require repository secrets
# They can't be tested via, so let's remove them and test the others
if not pytestconfig.getoption("--use-tokens"):
vals = [
validator[0]
for validator in circuitpython_libraries.default_validators
if validator[0]
not in circuitpython_library_validators.LibraryValidator.get_token_methods()
]
vals_str = ",".join(vals)
else:
vals_str = "all"

circuitpython_libraries.main(validator=vals_str)


# pylint: disable=invalid-name
def test_circuitpython_libraries_output_file(monkeypatch, tmp_path, capsys):
def test_circuitpython_libraries_output_file(
monkeypatch, pytestconfig, tmp_path, capsys
):
"""Test main funciton of 'circuitpython_libraries.py', with writing an output file."""

monkeypatch.setattr(common_funcs, "list_repos", mock_list_repos)

# Delete specific tests that require repository secrets
# They can't be tested via, so let's remove them and test the others
if not pytestconfig.getoption("--use-tokens"):
vals = [
validator[0]
for validator in circuitpython_libraries.default_validators
if validator[0]
not in circuitpython_library_validators.LibraryValidator.get_token_methods()
]
vals_str = ",".join(vals)
else:
vals_str = "all"

tmp_output_file = tmp_path / "output_test.txt"

circuitpython_libraries.main(validator="all", output_file=tmp_output_file)
circuitpython_libraries.main(validator=vals_str, output_file=tmp_output_file)

captured = capsys.readouterr()

Expand Down

0 comments on commit b5fc1d6

Please sign in to comment.