Repository navigation
136 lines (114 loc) · 5.72 KB
/
Copy pathpythonTests.yml
File metadata and controls
136 lines (114 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# path-sync copy -n hunterhogan
# === DO_NOT_EDIT: path-sync hunterhogan ===
name: Python Tests
on: [push, pull_request]
permissions:
contents: read
jobs:
get-project-info:
runs-on: ubuntu-latest
outputs:
tests-python-versions: ${{ steps.get-project-info.outputs.versions }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: '>=3.13'
cache: pip
cache-dependency-path: pyproject.toml
- run: pip install packaging
- id: get-project-info
run: |
python -c "
import itertools, json, os, packaging.specifiers, packaging.version, pathlib, tomllib, urllib.request
dictionaryProject = tomllib.loads(pathlib.Path('pyproject.toml').read_text())['project']
listPythonVersions = [str(max(group))
for _key, group in itertools.groupby(
packaging.specifiers.SpecifierSet(dictionaryProject['requires-python']).filter(
(packaging.version.Version(manifest['version'])
for manifest in json.loads(urllib.request.urlopen(
'https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json'
).read())), prereleases=False)
, lambda V: (V.major, V.minor))]
with open(os.environ['GITHUB_OUTPUT'], 'a') as writeStream:
writeStream.write(f'versions={json.dumps(listPythonVersions)}\n')
"
make-testbeds:
needs: get-project-info
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
tests-python-versions: ${{ fromJson(needs.get-project-info.outputs.tests-python-versions) }}
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: ${{ matrix.tests-python-versions }}
cache: pip
cache-dependency-path: pyproject.toml
- run: python -m pip install "pip>=25.1"
- run: pip install -e . --group testing
- run: pytest
test-wheel:
needs: get-project-info
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: ${{ fromJson(needs.get-project-info.outputs.tests-python-versions)[0] }}
cache: pip
cache-dependency-path: pyproject.toml
- run: pipx run build --wheel
- run: python -m zipfile -e dist/*.whl "$RUNNER_TEMP/wheel"
- run: pip install dist/*.whl
# The wheel must contain each tracked file of its packages, except files that match a pattern in
# `[tool.wheelTest] filesExcluded`. Without extras, each module must import in a new process, except
# that each module in `[tool.wheelTest.modulesOptional]` must fail because it lacks the import name beside it.
- name: Test the wheel
shell: python
run: |
from concurrent.futures import ThreadPoolExecutor
from fnmatch import fnmatch
from functools import partial
from itertools import filterfalse
from operator import attrgetter, itemgetter, methodcaller
import json, os, pathlib, subprocess, sys, tomllib
pathWheel = pathlib.Path(os.environ['RUNNER_TEMP'], 'wheel')
wheelTest = tomllib.loads(pathlib.Path('pyproject.toml').read_text(encoding='utf-8'))['tool'].get('wheelTest', {})
def isMissing(filename):
return (filename and '/.' not in filename and not (pathWheel / filename).exists()
and not any(map(partial(fnmatch, filename), wheelTest.get('filesExcluded', []))))
def isModule(pathFilename):
return 'tests' not in pathFilename.parts and pathFilename.stem != '__main__'
def getModule(pathFilename):
return '.'.join(pathFilename.relative_to(pathWheel).with_suffix('').parts).removesuffix('.__init__')
def importModule(module):
process = subprocess.run([sys.executable, '-P', '-X', 'utf8', '-c', f'import {module}'], stderr=subprocess.PIPE, encoding='utf-8')
return process.returncode and (process.stderr.strip().rpartition('\n')[2] or f'exit code {process.returncode}')
listPackages = [*map(attrgetter('name'), filterfalse(methodcaller('match', '*.dist-info'), pathWheel.iterdir()))]
listMissing = [*filter(isMissing, subprocess.run(['git', 'ls-files', '-z', '--', *listPackages]
, check=True, stdout=subprocess.PIPE, encoding='utf-8').stdout.split('\0'))]
listModules = [*map(getModule, filter(isModule, pathWheel.rglob('*.py')))]
with ThreadPoolExecutor(os.cpu_count()) as executor:
lookupError = dict(filter(itemgetter(1), zip(listModules, executor.map(importModule, listModules))))
modulesOptional = wheelTest.get('modulesOptional', {})
lookupErrorExpected = dict(zip(modulesOptional, map("ModuleNotFoundError: No module named '{}'".format, modulesOptional.values())))
print(len(listModules), 'modules in', *listPackages)
print('Tracked files that the wheel lacks:', json.dumps(listMissing, indent=2))
print('Modules that do not import:', json.dumps(lookupError, indent=2))
print('Expected:', json.dumps(lookupErrorExpected, indent=2))
sys.exit(bool(listMissing) or lookupError != lookupErrorExpected)
# === OK_EDIT: path-sync hunterhogan ===
# The contract is in `[tool.importlinter]` in pyproject.toml.
import-rules:
needs: get-project-info
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-python@v7
with:
python-version: ${{ fromJson(needs.get-project-info.outputs.tests-python-versions)[0] }}
- run: pip install import-linter
- run: lint-imports