From 75117e63a19547f0eb7657dd8188deb17b0b015d Mon Sep 17 00:00:00 2001 From: eskerda Date: Tue, 17 Sep 2024 15:06:00 +0200 Subject: [PATCH] add a workflow to test changes --- .github/workflows/test_changes.yml | 41 +++++++++++++++++++++ tests/conftest.py | 3 ++ tests/test_changes.py | 57 +++++++++++++++++------------- 3 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 .github/workflows/test_changes.yml diff --git a/.github/workflows/test_changes.yml b/.github/workflows/test_changes.yml new file mode 100644 index 000000000..d2316ddbc --- /dev/null +++ b/.github/workflows/test_changes.yml @@ -0,0 +1,41 @@ +name: pybikes + +env: + PYBIKES_CYCLOCITY: ${{ secrets.PYBIKES_CYCLOCITY }} + PYBIKES_BYSYKKEL: ${{ secrets.PYBIKES_BYSYKKEL }} + PYBIKES_WEELO_CLIENT_ID: ${{ secrets.PYBIKES_WEELO_CLIENT_ID }} + PYBIKES_WEELO_CLIENT_SECRET: ${{ secrets.PYBIKES_WEELO_CLIENT_SECRET }} + PYBIKES_DEUTSCHEBAHN_CLIENT_ID: ${{ secrets.PYBIKES_DEUTSCHEBAHN_CLIENT_ID }} + PYBIKES_DEUTSCHEBAHN_CLIENT_SECRET: ${{ secrets.PYBIKES_DEUTSCHEBAHN_CLIENT_SECRET }} + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +jobs: + test: + run-name: Test instance changes on this branch + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: make install + - name: Test instance changes on this branch + run: make test-changes T_FLAGS+='-n 10 --json-report --json-report-file=report/report.json' + # Only run summary once + - name: summary + if: (success() || failure()) && matrix.python-version == '3.8' + run: | + make github-summary >> $GITHUB_STEP_SUMMARY diff --git a/tests/conftest.py b/tests/conftest.py index 2616823a8..a2db9a94c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,3 +2,6 @@ def pytest_configure(config): config.addinivalue_line( "markers", "update: mark a test that uses network and might fail" ) + config.addinivalue_line( + "markers", "changes: mark a test as a gen test about changes" + ) diff --git a/tests/test_changes.py b/tests/test_changes.py index bfaeb39fe..cedfc695a 100644 --- a/tests/test_changes.py +++ b/tests/test_changes.py @@ -10,6 +10,7 @@ import git import inspect +from warnings import warn from importlib.util import spec_from_file_location, module_from_spec from pytest import mark @@ -34,35 +35,43 @@ def is_system(obj): return True -g = git.cmd.Git(os.getcwd()) -changed_files = g.diff('--name-only', 'master').splitlines() -clss = set() +def generate_tests_from_changes(): + g = git.cmd.Git(os.getcwd()) + changed_files = g.diff('--name-only', 'origin/master').splitlines() + clss = set() -for file in changed_files: - if re.match(r'pybikes/data/.*\.json', file): - # Extract classes from json file - match = re.search(r'pybikes/data/(.*)\.json', file) - if not match: - continue - schema = match.group(1) - [clss.add(cls) for cls, _ in get_instances(schema)] - elif re.match(r'pybikes/.*\.py', file): - # Extract bike share classes from file - spec = spec_from_file_location('some.mod', file) - mod = module_from_spec(spec) - spec.loader.exec_module(mod) - systems = filter(lambda m: is_system(m[1]), inspect.getmembers(mod)) - [clss.add(cls) for cls, _ in systems] + for file in changed_files: + if re.match(r'pybikes/data/.*\.json', file): + # Extract classes from json file + match = re.search(r'pybikes/data/(.*)\.json', file) + if not match: + continue + schema = match.group(1) + [clss.add(cls) for cls, _ in get_instances(schema)] + elif re.match(r'pybikes/.*\.py', file): + # Extract bike share classes from file + spec = spec_from_file_location('some.mod', file) + mod = module_from_spec(spec) + spec.loader.exec_module(mod) + systems = filter(lambda m: is_system(m[1]), inspect.getmembers(mod)) + [clss.add(cls) for cls, _ in systems] + for cls in sorted(clss): + test_cls = get_test_cls(cls) + test_cls = mark.changes(test_cls) + # decorate with pytest mark + globals()[test_cls.__name__] = test_cls + + +# non-optimal not-required +try: + generate_tests_from_changes() +except Exception as e: + warn("Failed generating tests from branch changes: " + str(e)) + # Force pytest to succeed when no tests are marked with 'changes' @mark.changes def test_dummy(): assert True - -for cls in sorted(clss): - test_cls = get_test_cls(cls) - test_cls = mark.changes(test_cls) - # decorate with pytest mark - globals()[test_cls.__name__] = test_cls