Skip to content

Commit 9655158

Browse files
committed
test languages only when they are changed
1 parent 1054afd commit 9655158

File tree

4 files changed

+163
-33
lines changed

4 files changed

+163
-33
lines changed

.github/actions/pre-test/action.yml

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,5 @@ inputs:
55
runs:
66
using: composite
77
steps:
8-
- name: setup (windows)
9-
shell: bash
10-
if: runner.os == 'Windows'
11-
run: |
12-
set -x
13-
14-
echo 'TEMP=C:\TEMP' >> "$GITHUB_ENV"
15-
16-
echo "$CONDA\Scripts" >> "$GITHUB_PATH"
17-
18-
echo 'C:\Strawberry\perl\bin' >> "$GITHUB_PATH"
19-
echo 'C:\Strawberry\perl\site\bin' >> "$GITHUB_PATH"
20-
echo 'C:\Strawberry\c\bin' >> "$GITHUB_PATH"
21-
22-
testing/get-coursier.sh
23-
testing/get-dart.sh
24-
- name: setup (linux)
25-
shell: bash
26-
if: runner.os == 'Linux'
27-
run: |
28-
set -x
29-
30-
sudo apt-get update
31-
sudo apt-get install -y --no-install-recommends \
32-
lua5.3 \
33-
liblua5.3-dev \
34-
luarocks
35-
36-
testing/get-coursier.sh
37-
testing/get-dart.sh
38-
testing/get-swift.sh
398
- uses: asottile/workflows/.github/actions/latest-git@v1.4.0
409
if: inputs.env == 'py38' && runner.os == 'Linux'

.github/workflows/languages.yaml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: languages
2+
3+
on:
4+
push:
5+
branches: [main, test-me-*]
6+
tags:
7+
pull_request:
8+
9+
concurrency:
10+
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
11+
cancel-in-progress: true
12+
13+
jobs:
14+
vars:
15+
runs-on: ubuntu-latest
16+
outputs:
17+
languages: ${{ steps.vars.outputs.languages }}
18+
steps:
19+
- uses: actions/checkout@v3
20+
with:
21+
fetch-depth: 0
22+
- uses: actions/setup-python@v4
23+
with:
24+
python-version: 3.8
25+
- name: install deps
26+
run: python -mpip install -e . -r requirements-dev.txt
27+
- name: vars
28+
run: testing/languages ${{ github.event_name == 'push' && '--all' || '' }}
29+
id: vars
30+
language:
31+
needs: [vars]
32+
runs-on: ${{ matrix.os }}
33+
if: needs.vars.outputs.languages != '[]'
34+
strategy:
35+
fail-fast: false
36+
matrix:
37+
include: ${{ fromJSON(needs.vars.outputs.languages) }}
38+
steps:
39+
- uses: asottile/workflows/.github/actions/fast-checkout@v1.4.0
40+
- uses: actions/setup-python@v4
41+
with:
42+
python-version: 3.8
43+
44+
- run: echo "$CONDA\Scripts" >> "$GITHUB_PATH"
45+
shell: bash
46+
if: matrix.os == 'windows-latest' && matrix.language == 'conda'
47+
- run: testing/get-coursier.sh
48+
shell: bash
49+
if: matrix.language == 'coursier'
50+
- run: testing/get-dart.sh
51+
shell: bash
52+
if: matrix.language == 'dart'
53+
- run: |
54+
sudo apt-get update
55+
sudo apt-get install -y --no-install-recommends \
56+
lua5.3 \
57+
liblua5.3-dev \
58+
luarocks
59+
if: matrix.os == 'ubuntu-latest' && matrix.language == 'lua'
60+
- run: |
61+
echo 'C:\Strawberry\perl\bin' >> "$GITHUB_PATH"
62+
echo 'C:\Strawberry\perl\site\bin' >> "$GITHUB_PATH"
63+
echo 'C:\Strawberry\c\bin' >> "$GITHUB_PATH"
64+
shell: bash
65+
if: matrix.os == 'windows-latest' && matrix.language == 'perl'
66+
- run: testing/get-swift.sh
67+
if: matrix.os == 'ubuntu-latest' && matrix.language == 'swift'
68+
69+
- name: install deps
70+
run: python -mpip install -e . -r requirements-dev.txt
71+
- name: run tests
72+
run: coverage run -m pytest tests/languages/${{ matrix.language }}_test.py
73+
- name: check coverage
74+
run: coverage report --include pre_commit/languages/${{ matrix.language }}.py,tests/languages/${{ matrix.language }}_test.py
75+
collector:
76+
needs: [language]
77+
if: always()
78+
runs-on: ubuntu-latest
79+
steps:
80+
- name: check for failures
81+
if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
82+
run: echo job failed && exit 1

testing/languages

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python3
2+
from __future__ import annotations
3+
4+
import argparse
5+
import concurrent.futures
6+
import json
7+
import os.path
8+
import subprocess
9+
import sys
10+
11+
EXCLUDED = frozenset((
12+
('windows-latest', 'docker'),
13+
('windows-latest', 'docker_image'),
14+
('windows-latest', 'lua'),
15+
('windows-latest', 'swift'),
16+
))
17+
18+
19+
def _lang_files(lang: str) -> frozenset[str]:
20+
prog = f'''\
21+
import json
22+
import os.path
23+
import sys
24+
25+
import pre_commit.languages.{lang}
26+
import tests.languages.{lang}_test
27+
28+
modules = sorted(
29+
os.path.relpath(v.__file__)
30+
for k, v in sys.modules.items()
31+
if k.startswith(('pre_commit.', 'tests.', 'testing.'))
32+
)
33+
print(json.dumps(modules))
34+
'''
35+
out = json.loads(subprocess.check_output((sys.executable, '-c', prog)))
36+
return frozenset(out)
37+
38+
39+
def main() -> int:
40+
parser = argparse.ArgumentParser()
41+
parser.add_argument('--all', action='store_true')
42+
args = parser.parse_args()
43+
44+
langs = [
45+
os.path.splitext(fname)[0]
46+
for fname in sorted(os.listdir('pre_commit/languages'))
47+
if fname.endswith('.py') and fname != '__init__.py'
48+
]
49+
50+
if not args.all:
51+
with concurrent.futures.ThreadPoolExecutor(os.cpu_count()) as exe:
52+
by_lang = {
53+
lang: files
54+
for lang, files in zip(langs, exe.map(_lang_files, langs))
55+
}
56+
57+
diff_cmd = ('git', 'diff', '--name-only', 'origin/main...HEAD')
58+
files = set(subprocess.check_output(diff_cmd).decode().splitlines())
59+
60+
langs = [
61+
lang
62+
for lang, lang_files in by_lang.items()
63+
if lang_files & files
64+
]
65+
66+
matched = [
67+
{'os': os, 'language': lang}
68+
for os in ('windows-latest', 'ubuntu-latest')
69+
for lang in langs
70+
if (os, lang) not in EXCLUDED
71+
]
72+
73+
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
74+
f.write(f'languages={json.dumps(matched)}\n')
75+
return 0
76+
77+
78+
if __name__ == '__main__':
79+
raise SystemExit(main())

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ deps = -rrequirements-dev.txt
66
passenv = *
77
commands =
88
coverage erase
9-
coverage run -m pytest {posargs:tests}
10-
coverage report
9+
coverage run -m pytest {posargs:tests} --ignore=tests/languages
10+
coverage report --omit=pre_commit/languages/*,tests/languages/*
1111

1212
[testenv:pre-commit]
1313
skip_install = true

0 commit comments

Comments
 (0)