Skip to content

Commit bd62299

Browse files
authored
Merge pull request #753 from eskerda/check-changes
check changes
2 parents dfbe327 + bb7c88c commit bd62299

8 files changed

+173
-56
lines changed

.github/workflows/test.yml

+63-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ on:
1515
branches: [ "master" ]
1616

1717
jobs:
18-
test:
18+
unit:
19+
name: Unit tests
1920
runs-on: ubuntu-latest
2021
strategy:
21-
fail-fast: false
22+
fail-fast: true
2223
matrix:
2324
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
2425
steps:
@@ -33,11 +34,68 @@ jobs:
3334
run: make lint
3435
- name: test
3536
run: make test
36-
- name: test update
37+
instance-changes:
38+
name: Instance tests (only changes)
39+
runs-on: ubuntu-latest
40+
needs: [unit]
41+
strategy:
42+
fail-fast: false
43+
steps:
44+
- uses: actions/checkout@v4
45+
with:
46+
fetch-depth: 0
47+
- name: Set up Python ${{ matrix.python-version }}
48+
uses: actions/setup-python@v5
49+
with:
50+
python-version: '3.8'
51+
- name: Install dependencies
52+
run: make install
53+
- name: Test instance changes on this branch
54+
run: make test-changes T_FLAGS+='-n 10 --json-report --json-report-file=report/report.json'
55+
# Only run summary once
56+
- name: all summary
57+
if: (success() || failure())
3758
run: |
38-
make report T_FLAGS+='-n 10' || true
59+
make github-summary >> $GITHUB_STEP_SUMMARY
60+
instance-tests:
61+
name: Instance tests (all)
62+
runs-on: ubuntu-latest
63+
needs: [unit]
64+
strategy:
65+
fail-fast: false
66+
steps:
67+
- uses: actions/checkout@v4
68+
- name: Set up Python ${{ matrix.python-version }}
69+
uses: actions/setup-python@v5
70+
with:
71+
python-version: '3.8'
72+
- name: Install dependencies
73+
run: make install
74+
- name: Test all instances
75+
run: |
76+
make report! T_FLAGS+='-n 10' || true
3977
# Only run summary once
4078
- name: summary
41-
if: (success() || failure()) && matrix.python-version == '3.8'
79+
if: (success() || failure())
4280
run: |
4381
make github-summary >> $GITHUB_STEP_SUMMARY
82+
83+
# This is more like a watchdog to know if the library still works with
84+
# python 2 or not. Not that it really matters
85+
python2:
86+
name: Unit tests (2.7)
87+
runs-on: ubuntu-latest
88+
steps:
89+
- uses: actions/checkout@v3
90+
- name: Set up Python 2.7
91+
uses: MatteoH2O1999/setup-python@v4
92+
with:
93+
python-version: '2.7'
94+
allow-build: info
95+
cache-build: true
96+
- name: Install dependencies
97+
run: make install
98+
- name: lint
99+
run: make lint
100+
- name: test
101+
run: make test

.github/workflows/test_python2.yml

-41
This file was deleted.

Makefile

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
T_FLAGS =
2+
R_FILE ?= report.json
23

34
.PHONY: install
45
install:
@@ -7,38 +8,45 @@ install:
78

89
.PHONY: test
910
test: install
10-
pytest tests -m 'not update' $(T_FLAGS)
11+
pytest tests -m 'not update and not changes' $(T_FLAGS)
1112

1213
.PHONY: test-update
1314
test-update: install
1415
pytest tests -m update $(T_FLAGS)
1516

17+
.PHONY: test-changes
18+
test-changes: install
19+
pytest tests -m changes $(T_FLAGS)
20+
1621
.PHONY: lint
1722
lint:
1823
flake8 pybikes tests --count --select=E9,F63,F7,F82 --show-source --statistics
1924
flake8 pybikes tests --count --exit-zero --max-complexity=10 --max-line-length=88 --statistics
2025

21-
report/report.json:
22-
pytest tests -m update --json-report --json-report-file=report/report.json $(T_FLAGS)
26+
report/$(R_FILE):
27+
pytest tests -m update $(T_FLAGS) --json-report --json-report-file=report/$(R_FILE)
2328

2429
.PHONY: report
25-
report: report/report.json
30+
report: report/$(R_FILE)
31+
32+
.PHONY: report!
33+
report!: clean report
2634

2735
.PHONY: summary
28-
summary: report/report.json
29-
@./utils/report.py report/report.json
36+
summary: report/$(R_FILE)
37+
@./utils/report.py report/$(R_FILE)
3038

3139
.PHONY: map
32-
map: report/report.json
33-
@./utils/report.py report/report.json --template utils/map.tpl.html > report/map.html
40+
map: report/$(R_FILE)
41+
@./utils/report.py report/$(R_FILE) --template utils/map.tpl.html > report/map.html
3442
@open report/map.html || xdg-open report/map.html
3543

3644
.PHONY: map!
3745
map!: clean map
3846

3947
.PHONY: github-summary
40-
github-summary: report/report.json
41-
@./utils/report.py report/report.json --template utils/github-summary.tpl.md
48+
github-summary: report/$(R_FILE)
49+
@./utils/report.py report/$(R_FILE) --template utils/github-summary.tpl.md
4250

4351
.PHONY: clean
4452
clean: clean-report

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
pytest
55
pytest-xdist
66
mock; python_version < '3.3'
7+
gitpython
78

89
# Lint
910
flake8

tests/conftest.py

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ def pytest_configure(config):
22
config.addinivalue_line(
33
"markers", "update: mark a test that uses network and might fail"
44
)
5+
config.addinivalue_line(
6+
"markers", "changes: mark a test as a gen test about changes"
7+
)

tests/test_changes.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
""" Very hacky script that runs tests on bike share systems that have
2+
changed on a specific branch. Checks for changes on data files and
3+
classes containing bike share systems.
4+
5+
This is meant for red explicit fails on changes in CI
6+
"""
7+
8+
import re
9+
import os
10+
import git
11+
import inspect
12+
13+
from pytest import mark
14+
from warnings import warn
15+
16+
from pybikes import BikeShareSystem
17+
from pybikes.data import get_instances
18+
from pybikes.compat import resources
19+
20+
from tests.test_instances import get_test_cls
21+
22+
23+
def is_system(mod, obj):
24+
if not inspect.isclass(obj):
25+
return False
26+
27+
# Only declared in module
28+
if obj.__module__ != mod.__name__:
29+
return False
30+
31+
if not issubclass(obj, BikeShareSystem):
32+
return False
33+
34+
if obj == BikeShareSystem:
35+
return False
36+
37+
return True
38+
39+
40+
def generate_tests_from_changes(branch):
41+
42+
# this will fail in python < 3.8
43+
from importlib.util import spec_from_file_location, module_from_spec
44+
45+
# this might fail if branch not in git dir
46+
g = git.cmd.Git(os.getcwd())
47+
changed_files = g.diff('--name-only', branch).splitlines()
48+
clss = set()
49+
50+
for file in changed_files:
51+
if re.match(r'pybikes/data/.*\.json', file):
52+
# Extract classes from json file
53+
match = re.search(r'pybikes/data/(.*)\.json', file)
54+
schema = match.group(1)
55+
[clss.add(cls) for cls, _ in get_instances(schema)]
56+
elif re.match(r'pybikes/.*\.py', file):
57+
# Extract bike share classes from file
58+
spec = spec_from_file_location('some.mod', file)
59+
mod = module_from_spec(spec)
60+
spec.loader.exec_module(mod)
61+
systems = filter(lambda m: is_system(mod, m[1]), inspect.getmembers(mod))
62+
[clss.add(cls) for cls, _ in systems]
63+
64+
for cls in sorted(clss):
65+
test_cls = get_test_cls(cls)
66+
# decorate with pytest mark 'changes'
67+
test_cls = mark.changes(test_cls)
68+
globals()[test_cls.__name__] = test_cls
69+
70+
71+
# non-optimal not-required
72+
try:
73+
generate_tests_from_changes('origin/master')
74+
except Exception as e:
75+
warn("Failed generating tests from branch changes: " + str(e))
76+
77+
# Force pytest to succeed when no tests are marked with 'changes'
78+
@mark.changes
79+
def test_dummy():
80+
assert True

utils/github-summary.tpl.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# systems status report (Python {{ version }})
22

3+
{% if systems %}
34
| overall success rate | {{ int((health.passed / health.total) * 100) }}% |
45
|-|-|
56
| passed | {{ health.passed }} |
@@ -55,3 +56,6 @@
5556
{% endfor %}
5657
{% endfor %}
5758
{% endfor %}
59+
{% else %}
60+
No systems
61+
{% endif %}

utils/report.tpl.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# systems status report (Python {{ version }})
2+
{% if systems %}
23

34
| overall success rate | {{ int((health.passed / health.total) * 100) }}% |
45
|-|-|
@@ -54,3 +55,6 @@
5455
{% endfor %}
5556
{% endfor %}
5657
{% endfor %}
58+
{% else %}
59+
No systems
60+
{% endif %}

0 commit comments

Comments
 (0)