16
16
validate-config :
17
17
name : " Validate config"
18
18
outputs :
19
- config : " ${{ steps.config-exporter .outputs.config }}"
19
+ config : " ${{ steps.config-transformer .outputs.config }}"
20
20
runs-on : " ubuntu-latest"
21
21
steps :
22
22
- name : " Setup Python"
@@ -26,38 +26,12 @@ jobs:
26
26
27
27
- name : " Export config"
28
28
id : " config-exporter"
29
- shell : " python"
30
- # Loading the workflow input from an environment variable
31
- # avoids injection attacks and allows for more robust validation.
29
+ shell : " bash"
30
+ # Loading the input from an environment variable avoids injection attacks.
32
31
env :
33
32
inputs_config : " ${{ inputs.config }}"
34
33
run : |
35
- # START: tox-config-writer.py
36
- import json
37
- import os
38
- import sys
39
-
40
- try:
41
- config_string = os.environ["inputs_config"] # KeyError
42
- config = json.loads(config_string) # json.JSONDecodeError
43
- if not isinstance(config, dict):
44
- raise TypeError
45
- except KeyError:
46
- print("The 'inputs_config' key wasn't found in the environment")
47
- except json.JSONDecodeError:
48
- print("The config input couldn't be decoded as valid JSON")
49
- except TypeError:
50
- print("The config isn't a JSON object")
51
- else:
52
- output = json.dumps(config, sort_keys=True, separators=(",", ":"))
53
- with open("config.json", "w") as file:
54
- file.write(output)
55
- with open(os.environ["GITHUB_OUTPUT"], "a") as file:
56
- file.write(f"config={output}")
57
- sys.exit(0)
58
-
59
- sys.exit(1)
60
- # END: tox-config-writer.py
34
+ echo "$inputs_config" > config.json
61
35
62
36
# If a previous workflow run successfully validated an identical config object,
63
37
# a cache hit is sufficient to demonstrate that no further validation is required.
@@ -212,6 +186,36 @@ jobs:
212
186
pip install --user check-jsonschema
213
187
check-jsonschema --schemafile schema.json config.json
214
188
189
+ - name : " Transform config"
190
+ id : " config-transformer"
191
+ shell : " python"
192
+ run : |
193
+ # START: tox-config-transformer.py
194
+ import json
195
+ import os
196
+
197
+ with open("config.json") as file:
198
+ config = json.load(file)
199
+
200
+ # Transform the tox environments for convenience.
201
+ # "pre-environments" and "post-environments" will be injected into "environments",
202
+ # together with a full list of CPython and PyPy interpreter versions.
203
+ # Since these keys are mutually-exclusive with "environments",
204
+ # no config data are lost in this transformation.
205
+ if {"pre-environments", "post-environments"} & config.get("tox", {}).keys():
206
+ environments = config["tox"].pop("pre-environments", [])
207
+ environments.extend(f"py{version}" for version in config.get("cpythons", []))
208
+ if "cpython-beta" in config:
209
+ environments.append(f"py{config['cpython-beta']}")
210
+ environments.extend(f"pypy{version}" for version in config.get("pypys", []))
211
+ environments.extend(config["tox"].pop("post-environments", []))
212
+ config["tox"]["environments"] = environments
213
+
214
+ output = json.dumps(config, sort_keys=True, separators=(",", ":"))
215
+ with open(os.environ["GITHUB_OUTPUT"], "a") as file:
216
+ file.write(f"config={output}")
217
+ # END: tox-config-transformer.py
218
+
215
219
tox :
216
220
needs :
217
221
- " validate-config"
@@ -229,11 +233,12 @@ jobs:
229
233
run : |
230
234
# shellcheck disable=SC2086
231
235
for pattern in $FILE_PATTERNS; do
232
- if ! md5sum $pattern >> '.hash-files.md5 '; then
236
+ if ! ${{ runner.os == 'macOS' && 'shasum -a 1' || 'sha1sum' }} $pattern >> '.hash-files.sha '; then
233
237
echo "The cache.hash-files pattern '$pattern' matched nothing"
234
238
exit 1
235
239
fi
236
240
done
241
+ cat .hash-files.sha
237
242
238
243
- name : " Setup Pythons"
239
244
uses : " actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d" # v5.1.0
@@ -275,7 +280,7 @@ jobs:
275
280
.tox/
276
281
.venv/
277
282
${{ fromJSON(inputs.config).cache.paths && join(fromJSON(inputs.config).cache.paths, '\n') }}
278
- key : " ${{ fromJSON(inputs.config).cache.name || 'tox' }}-os=${{ fromJSON(inputs.config).runner }}-hash=${{ hashFiles('.python-identifiers', '.workflow-config.json', 'tox.ini', fromJSON(inputs.config).cache.key.hash-files && '.hash-files.md5 ') }}"
283
+ key : " ${{ fromJSON(inputs.config).cache.name || 'tox' }}-os=${{ fromJSON(inputs.config).runner }}-hash=${{ hashFiles('.python-identifiers', '.workflow-config.json', 'tox.ini', fromJSON(inputs.config).cache.key.hash-files && '.hash-files.sha ') }}"
279
284
280
285
- name : " Identify .venv path"
281
286
shell : " bash"
@@ -289,16 +294,6 @@ jobs:
289
294
${{ env.venv-path }}/python -m pip install --upgrade pip setuptools wheel
290
295
${{ env.venv-path }}/pip install tox
291
296
292
- - name : " Prepare the tox environment list to run (specific list)"
293
- if : " fromJSON(inputs.config).tox.environments"
294
- run : |
295
- echo "tox_environments=${{ join(fromJSON(inputs.config).tox.environments, ',') }}" >> "$GITHUB_ENV"
296
-
297
- - name : " Prepare the tox environment list to run (prefix/postfix)"
298
- if : " fromJSON(inputs.config).tox.pre-environments || fromJSON(inputs.config).tox.post-environments"
299
- run : |
300
- echo "tox_environments=${{ join(fromJSON(inputs.config).tox.environments, ',') }}" >> "$GITHUB_ENV"
301
-
302
297
- name : " Run the test suite"
303
298
run : |
304
- ${{ env.venv-path }}/tox run --colored yes ${{ env.tox_environments && format('-e "{0}"', env.tox_environments ) }}
299
+ ${{ env.venv-path }}/tox run --colored yes ${{ fromJSON(needs.validate-config.outputs.config).tox.environments && format('-e "{0}"', join(fromJSON(needs.validate-config.outputs.config).tox.environments, ',') ) }}
0 commit comments