Repository navigation
179 lines (165 loc) · 6.63 KB
/
Copy pathpython_test.yml
File metadata and controls
179 lines (165 loc) · 6.63 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
name: Python test
# INTERNAL: shared core of python_test_pip.yml, python_test_conda.yml and python_test_pixi.yml, which call it by
# local path so it always runs from the same commit as the wrapper. Not part of the public interface: its inputs may
# change in any release. Callers should use the wrappers.
#
# Runs a Python project's tests across a matrix, installing it with pip, conda (micromamba) or pixi, optionally with
# externally built tools from artifacts on PATH and a Codecov upload.
on:
workflow_call:
inputs:
ref:
description: "Branch, tag or commit to test; defaults to the commit that triggered the caller"
required: false
type: string
default: ""
installer:
description: "How to install the project: pip, conda or pixi"
required: true
type: string
targets:
description: "JSON list of matrix targets: Python versions (pip, conda; \"\" = the environment file's) or pixi environments (pixi)"
required: true
type: string
runs_on:
description: "JSON list of runner images"
required: false
type: string
default: '["ubuntu-latest"]'
pip_install:
description: "Arguments to `pip install` (pip)"
required: false
type: string
default: "-e .[dev]"
conda_environment_file:
description: "Environment file that installs the project and its test dependencies (conda)"
required: false
type: string
default: environment.yml
tools_artifact_pattern:
description: "Pattern of artifacts from the calling run whose files are added to PATH (e.g. tool-*)"
required: false
type: string
default: ""
test_command:
description: "Bash script that runs the tests inside the environment; may span several lines (set -euo pipefail)"
required: false
type: string
default: pytest
secrets:
codecov_token:
description: "Codecov upload token; when set, coverage.xml is uploaded (test_command must write it, e.g. pytest --cov --cov-report=xml)"
required: false
jobs:
test:
name: Test (${{ inputs.installer }}${{ matrix.target != '' && format(', {0}', matrix.target) || '' }}, ${{ matrix.os }})
runs-on: ${{ matrix.os }}
permissions:
contents: read
strategy:
fail-fast: false
matrix:
os: ${{ fromJson(inputs.runs_on) }}
# Python versions for pip/conda, pixi environments for pixi
target: ${{ fromJson(inputs.targets) }}
defaults:
run:
shell: bash
env:
# Secrets cannot be tested in step conditions, so expose whether the token was passed
HAS_CODECOV_TOKEN: ${{ secrets.codecov_token != '' }}
steps:
- name: Check installer
env:
INSTALLER: ${{ inputs.installer }}
run: |
case "$INSTALLER" in
pip|conda|pixi) ;;
*) echo "::error::Invalid installer '${INSTALLER}'; expected pip, conda or pixi"; exit 1 ;;
esac
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.ref }}
# Full history without old file contents, so version tools such as versioningit can find the last tag
fetch-depth: 0
filter: tree:0
- name: Download external tools
if: inputs.tools_artifact_pattern != ''
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: ${{ inputs.tools_artifact_pattern }}
merge-multiple: true
path: ${{ runner.temp }}/bin
- name: Add external tools to PATH
if: inputs.tools_artifact_pattern != ''
run: |
chmod +x "$RUNNER_TEMP"/bin/*
echo "$RUNNER_TEMP/bin" >> "$GITHUB_PATH"
# pip
- name: Set up Python ${{ matrix.target }}
if: inputs.installer == 'pip'
uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: ${{ matrix.target }}
cache: ${{ hashFiles('pyproject.toml') != '' && 'pip' || '' }}
cache-dependency-path: pyproject.toml
- name: Install with pip
if: inputs.installer == 'pip'
env:
PIP_INSTALL: ${{ inputs.pip_install }}
run: |
python -m pip install --upgrade pip
# PIP_INSTALL is word-split on purpose so several arguments can be passed
# shellcheck disable=SC2086
python -m pip install $PIP_INSTALL
# conda
- name: Set up conda environment
if: inputs.installer == 'conda'
uses: mamba-org/setup-micromamba@f457c30a868e4760d3a6fcea5f25dc655b8edf39 # v3.2.1
with:
environment-file: ${{ inputs.conda_environment_file }}
# Fixed name so later steps can run commands in it, whatever the file calls it
environment-name: test
create-args: ${{ matrix.target != '' && format('python={0}', matrix.target) || '' }}
cache-downloads: true
# pixi
- name: Set up pixi environment
if: inputs.installer == 'pixi'
uses: prefix-dev/setup-pixi@ad1da6449d9e506fd127fd67ab2d054a769f6a94 # v0.11.2
with:
environments: ${{ matrix.target }}
locked: true
cache: true
# in-env CMD runs CMD with bash inside whichever environment was set up. The command goes through a script
# file so no tool re-parses its quoting.
- name: Prepare environment runner
env:
INSTALLER: ${{ inputs.installer }}
TARGET: ${{ matrix.target }}
run: |
case "$INSTALLER" in
pip) prefix="" ;;
conda) prefix="micromamba run -n test" ;;
pixi) prefix="pixi run -e $(printf '%q' "$TARGET")" ;;
esac
cat > "$RUNNER_TEMP/in-env" <<SCRIPT
#!/usr/bin/env bash
set -euo pipefail
script=\$(mktemp)
printf '%s\n' "\$1" > "\$script"
exec ${prefix} bash -euo pipefail "\$script"
SCRIPT
chmod +x "$RUNNER_TEMP/in-env"
cat "$RUNNER_TEMP/in-env"
- name: Run tests
env:
TEST_COMMAND: ${{ inputs.test_command }}
run: '"$RUNNER_TEMP/in-env" "$TEST_COMMAND"'
- name: Upload coverage to Codecov
if: env.HAS_CODECOV_TOKEN == 'true'
uses: codecov/codecov-action@303a32d7a59b442fa8d48b6a1cc6825c09c847a5 # v7.1.1
with:
files: ./coverage.xml
flags: ${{ inputs.installer }}${{ matrix.target != '' && format('-{0}', matrix.target) || '' }}
token: ${{ secrets.codecov_token }}