Skip to content

Commit 671d282

Browse files
authored
Merge pull request #227 from sommersoft/testing
Add Some Basic Tests
2 parents bcbf6a8 + d99f120 commit 671d282

14 files changed

+154
-141
lines changed

.github/workflows/test.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Test Adabot
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
check-repo-owner:
7+
# This job is so the entire workflow will end successfully and give some
8+
# output to explain why it hasn't run on a non-Adafruit fork.
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: repository
12+
env:
13+
OWNER_IS_ADAFRUIT: ${{ startswith(github.repository, 'adafruit/') }}
14+
run: |
15+
echo "This workflow will only run if Adafruit is the repository owner."
16+
echo "Repository owner is Adafruit: $OWNER_IS_ADAFRUIT"
17+
run-tests:
18+
runs-on: ubuntu-latest
19+
# Only run the build on Adafruit's repository. Forks won't have the secrets.
20+
# Its necessary to do this here, since 'schedule' events cannot (currently)
21+
# be limited (they run on all forks' default branches).
22+
if: startswith(github.repository, 'adafruit/')
23+
services:
24+
redis:
25+
image: redis
26+
ports:
27+
- 6379/tcp
28+
options: --entrypoint redis-server
29+
steps:
30+
- name: Dump GitHub context
31+
env:
32+
GITHUB_CONTEXT: ${{ toJson(github) }}
33+
run: echo "$GITHUB_CONTEXT"
34+
- name: Set up Python 3.8
35+
uses: actions/setup-python@v1
36+
with:
37+
python-version: 3.8
38+
- name: Versions
39+
run: |
40+
python3 --version
41+
- uses: actions/checkout@v1
42+
with:
43+
submodules: true
44+
- name: Install deps
45+
run: |
46+
pip install -r requirements.txt
47+
- name: Run Tests
48+
env:
49+
ADABOT_EMAIL: ${{ secrets.ADABOT_EMAIL }}
50+
ADABOT_GITHUB_USER: ${{ secrets.ADABOT_GITHUB_USER }}
51+
ADABOT_GITHUB_ACCESS_TOKEN: ${{ secrets.ADABOT_GITHUB_ACCESS_TOKEN }}
52+
REDIS_PORT: ${{ job.services.redis.ports[6379] }}
53+
run: |
54+
python3 -u -m pytest

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ env.sh
88
.libraries/*
99
.cp_org/*
1010
.blinka/*
11+
.vscode

adabot/__init__.py

Whitespace-only changes.

adabot/arduino_libraries.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,8 @@ def run_arduino_lib_checks():
236236
print_list_output("Libraries that is missing library.properties file: ({})", missing_library_properties_list)
237237

238238

239-
if __name__ == "__main__":
240-
cmd_line_args = cmd_line_parser.parse_args()
241-
verbosity = cmd_line_args.verbose
242-
if cmd_line_args.output_file:
243-
output_filename = cmd_line_args.output_file
239+
def main(verbosity=1, output_filename=None):
240+
244241
try:
245242
reply = requests.get("http://downloads.arduino.cc/libraries/library_index.json")
246243
if not reply.ok:
@@ -268,3 +265,11 @@ def run_arduino_lib_checks():
268265
with open(output_filename, 'w') as f:
269266
for line in file_data:
270267
f.write(str(line) + "\n")
268+
269+
if __name__ == "__main__":
270+
cmd_line_args = cmd_line_parser.parse_args()
271+
main(
272+
verbosity=cmd_line_args.verbose,
273+
output_filename=cmd_line_args.output_file
274+
)
275+

adabot/circuitpython_libraries.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
'Adafruit_Python_Extended_Bus'
110110
]
111111

112-
def run_library_checks(validators, bundle_submodules, latest_pylint, kw_args):
112+
def run_library_checks(validators, bundle_submodules, latest_pylint, kw_args, error_depth):
113113
"""runs the various library checking functions"""
114114
pylint_info = pypi.get("/pypi/pylint/json")
115115
if pylint_info and pylint_info.ok:
@@ -491,29 +491,24 @@ def print_issue_overview(*insights):
491491
)
492492
output_handler(hacktober_changes)
493493

494-
if __name__ == "__main__":
494+
def main(verbosity=1, output_filename=None, validator=None, error_depth=5):
495495
validator_kwarg_list = {}
496496
startup_message = [
497497
"Running CircuitPython Library checks...",
498498
"Report Date: {}".format(datetime.datetime.now().strftime("%d %B %Y, %I:%M%p"))
499499
]
500-
cmd_line_args = cmd_line_parser.parse_args()
501-
502-
verbosity = cmd_line_args.verbose
503500

504-
if cmd_line_args.output_file:
505-
output_filename = cmd_line_args.output_file
501+
if output_filename:
506502
startup_message.append(" - Report output will be saved to: {}".format(output_filename))
507503

508504
validators = []
509505
validator_names = []
510-
if cmd_line_args.validator:
511-
error_depth = cmd_line_args.error_depth
506+
if validator:
512507
startup_message.append(" - Depth for listing libraries with errors: {}".format(error_depth))
513508

514-
if cmd_line_args.validator != "all":
509+
if validator != "all":
515510
validators = []
516-
for func in cmd_line_args.validator.split(","):
511+
for func in validator.split(","):
517512
func_name = func.strip()
518513
try:
519514
if not func_name.startswith("validate"):
@@ -553,7 +548,7 @@ def print_issue_overview(*insights):
553548
output_handler()
554549
#print(validators)
555550
run_library_checks(validators, bundle_submodules, latest_pylint,
556-
validator_kwarg_list)
551+
validator_kwarg_list, error_depth)
557552
except:
558553
if output_filename is not None:
559554
exc_type, exc_val, exc_tb = sys.exc_info()
@@ -572,3 +567,12 @@ def print_issue_overview(*insights):
572567
with open(output_filename, 'w') as f:
573568
for line in file_data:
574569
f.write(str(line) + "\n")
570+
571+
if __name__ == "__main__":
572+
cli_args = cmd_line_parser.parse_args()
573+
main(
574+
verbosity=cli_args.verbose,
575+
output_filename=cli_args.output_file,
576+
validator=cli_args.validator,
577+
error_depth=cli_args.error_depth
578+
)

pytest.ini

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
addopts = -v --tb=short --show-capture=no
3+
testpaths = tests/unit/ tests/integration/

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ chardet==3.0.4
33
idna==2.6
44
packaging==20.3
55
pylint
6+
pytest
67
pyyaml==5.4.1
78
redis==2.10.6
89
requests==2.20.0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from adabot import arduino_libraries
4+
from adabot import github_requests
5+
6+
def test_adafruit_libraries(monkeypatch):
7+
8+
def get_list_repos():
9+
repos = []
10+
repos.append(github_requests.get("/repos/adafruit/Adafruit_NeoPixel").json())
11+
return repos
12+
13+
monkeypatch.setattr(arduino_libraries, "list_repos", get_list_repos)
14+
15+
print(arduino_libraries.list_repos())
16+
17+
arduino_libraries.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from adabot.lib import common_funcs
4+
from adabot import github_requests
5+
from adabot import circuitpython_libraries
6+
7+
def test_circuitpython_libraires(monkeypatch):
8+
9+
def mock_list_repos(*args, **kwargs):
10+
repos = []
11+
repos.append(github_requests.get("/repos/adafruit/Adafruit_CircuitPython_TestRepo").json())
12+
return repos
13+
14+
monkeypatch.setattr(common_funcs, "list_repos", mock_list_repos)
15+
16+
circuitpython_libraries.main(validator="all")

tests/test_circuitpython_libraries.py

-124
This file was deleted.

tests/unit/test_blinka_funcs.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
from adabot.lib import blinka_funcs
4+
5+
def test_board_count():
6+
assert blinka_funcs.board_count() >= 0

tests/unit/test_common_funcs.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
from adabot.lib import common_funcs
3+
4+
def test_list_repos():
5+
repos = common_funcs.list_repos()
6+
7+
assert isinstance(repos, list)
8+
9+
def test_repo_is_on_pypi_true():
10+
assert common_funcs.repo_is_on_pypi({"name": "pytest"})

tests/unit/test_github_requests.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
3+
from adabot import github_requests
4+
5+
def test_fix_url():
6+
url = github_requests._fix_url("/meta")
7+
assert url == "https://api.github.com/meta"
8+
9+
def test_fix_kwargs():
10+
dummy_kwargs = github_requests._fix_kwargs({})
11+
12+
assert "headers" in dummy_kwargs
13+
assert "Accept" in dummy_kwargs["headers"]

tests/unit/test_pypi_requests.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
3+
from adabot import pypi_requests
4+
5+
def test_fix_url():
6+
url = pypi_requests._fix_url("/test")
7+
assert url == "https://pypi.org/test"

0 commit comments

Comments
 (0)