Skip to content

Commit 719685e

Browse files
committed
feat: lookup github action values from nox
Establish a single source of truth for python versions and tests matrix, driven by nox. Make the local test and CI behave the same: the default behavior for tests(tox_version="latest") is to only run test_tox_to_nox.
1 parent 08813c3 commit 719685e

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

.github/workflows/ci.yml

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,63 @@ on:
88

99
env:
1010
FORCE_COLOR: "1"
11+
OS_LIST: '["ubuntu-20.04", "windows-latest", "macos-latest"]'
1112

1213
concurrency:
1314
group: ${{ github.workflow }}-${{ github.ref }}
1415
cancel-in-progress: true
1516

1617
jobs:
18+
19+
nox_data:
20+
name: Query nox
21+
runs-on: ubuntu-latest
22+
outputs:
23+
tests_matrix: ${{ steps.generate.outputs.tests_matrix }}
24+
lint_python_version: ${{ steps.generate.outputs.lint_python_version }}
25+
# our noxfile does not have an opinion about the python version for these:
26+
docs_python_version: "3.9"
27+
cover_python_version: "3.11"
28+
steps:
29+
- uses: actions/checkout@v4
30+
- name: Set up Python 3.11
31+
uses: actions/setup-python@v5
32+
with:
33+
python-version: "3.11"
34+
- name: Install Nox-under-test
35+
run: |
36+
python -m pip install --disable-pip-version-check .
37+
- name: Save nox sessions to json
38+
run: |
39+
nox -l --json >> nox_sessions.json
40+
- name: Generate output variables
41+
id: generate
42+
run: |
43+
jq 'map(select(.name == "tests")) | map({"python-version": .python, "tox-version": .call_spec.tox_version})' nox_sessions.json >> tests.json
44+
: # Unfortunately, Adding OSes to `strategy:matrix:os` and the python/tox versions to `strategy:matrix:include` does not
45+
: # combine as advertised so we have to make the combinations ourselves.
46+
: # https://github.com/orgs/community/discussions/67591#discussioncomment-7402927
47+
echo $OS_LIST | jq 'map({"os": .})' >> oses.json
48+
: # -s places the contents of each json file (each of which contains a list) into the first two elements of a list.
49+
: # 'combinations' loops creates a pair of objects for each combination in the two lists.
50+
: # 'add' fuses each of the pairs of objects into a single object.
51+
: # -c -j creates single line output.
52+
OUTPUT=$(jq -c -j -s '[ combinations | add ]' tests.json oses.json)
53+
echo "tests_matrix=$OUTPUT" >> "$GITHUB_OUTPUT"
54+
echo $OUTPUT
55+
56+
OUTPUT=$(jq -c -j 'map(select(.name == "lint")) | map(.python) | .[0]' nox_sessions.json)
57+
echo "lint_python_version=$OUTPUT" >> "$GITHUB_OUTPUT"
58+
echo $OUTPUT
59+
1760
build:
61+
needs: nox_data
62+
name: build (${{ matrix.os }}, py-${{ matrix.python-version }}, tox ${{ matrix.tox-version }})
1863
runs-on: ${{ matrix.os }}
1964
strategy:
2065
matrix:
21-
os: [ubuntu-20.04, windows-latest, macos-latest]
22-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
66+
include: ${{ fromJSON(needs.nox_data.outputs.tests_matrix) }}
67+
2368
steps:
2469
- uses: actions/checkout@v4
2570
- name: Set up Python ${{ matrix.python-version }}
@@ -36,26 +81,24 @@ jobs:
3681
- name: Install Nox-under-test
3782
run: |
3883
python -m pip install --disable-pip-version-check .
39-
- name: Run tests on ${{ matrix.os }} (tox <4)
40-
run: nox --non-interactive --error-on-missing-interpreter --session "tests(python='${{ matrix.python-version }}', tox_version='<4')" -- --full-trace
41-
- name: Run tox-to-nox tests on ${{ matrix.os }} (tox latest)
42-
run: nox --non-interactive --error-on-missing-interpreter --session "tests(python='${{ matrix.python-version }}', tox_version='latest')" -- tests/test_tox_to_nox.py --full-trace
43-
if: matrix.python-version != '3.7'
84+
- name: Run tests on ${{ matrix.os }} (tox ${{ matrix.tox-version }})
85+
run: |
86+
nox --non-interactive --error-on-missing-interpreter --session "tests(python='${{ matrix.python-version }}', tox_version='${{ matrix.tox-version }}')" -- --full-trace
4487
- name: Save coverage report
4588
uses: actions/upload-artifact@v4
4689
with:
4790
name: coverage-${{ github.job }}-${{ strategy.job-index }}
4891
path: .coverage.*
4992

5093
coverage:
51-
needs: build
94+
needs: [build, nox_data]
5295
runs-on: ubuntu-latest
5396
steps:
5497
- uses: actions/checkout@v4
55-
- name: Set up Python 3.11
98+
- name: Set up Python ${{ needs.nox_data.outputs.cover_python_version }}
5699
uses: actions/setup-python@v5
57100
with:
58-
python-version: "3.11"
101+
python-version: ${{ fromJSON(needs.nox_data.outputs.cover_python_version) }}
59102
- name: Install Nox-under-test
60103
run: |
61104
python -m pip install --disable-pip-version-check .
@@ -70,26 +113,28 @@ jobs:
70113
run: nox --non-interactive --session "cover"
71114

72115
lint:
116+
needs: nox_data
73117
runs-on: ubuntu-latest
74118
steps:
75119
- uses: actions/checkout@v4
76-
- name: Set up Python 3.9
120+
- name: Set up Python ${{ needs.nox_data.outputs.lint_python_version }}
77121
uses: actions/setup-python@v5
78122
with:
79-
python-version: 3.9
123+
python-version: ${{ fromJSON(needs.nox_data.outputs.lint_python_version) }}
80124
- name: Install Nox-under-test
81125
run: |
82126
python -m pip install --disable-pip-version-check .
83127
- name: Lint
84128
run: nox --non-interactive --error-on-missing-interpreter --session "lint"
85129
docs:
130+
needs: nox_data
86131
runs-on: ubuntu-latest
87132
steps:
88133
- uses: actions/checkout@v4
89-
- name: Set up Python 3.9
134+
- name: Set up Python ${{ needs.nox_data.outputs.docs_python_version }}
90135
uses: actions/setup-python@v5
91136
with:
92-
python-version: 3.9
137+
python-version: ${{ fromJSON(needs.nox_data.outputs.docs_python_version) }}
93138
- name: Install Nox-under-test
94139
run: |
95140
python -m pip install --disable-pip-version-check .

noxfile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,20 @@ def tests(session: nox.Session, tox_version: str) -> None:
5656
session.create_tmp() # Fixes permission errors on Windows
5757
session.install("-r", "requirements-test.txt")
5858
session.install("-e.[tox_to_nox]")
59+
args = session.posargs
5960
if tox_version != "latest":
6061
session.install(f"tox{tox_version}")
62+
elif not args:
63+
# this ensures consistent behavior locally and in CI
64+
args = ["tests/test_tox_to_nox.py"]
65+
6166
session.run(
6267
"pytest",
6368
"--cov",
6469
"--cov-config",
6570
"pyproject.toml",
6671
"--cov-report=",
67-
*session.posargs,
72+
*args,
6873
env={
6974
"COVERAGE_FILE": coverage_file,
7075
},

0 commit comments

Comments
 (0)