-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
101 lines (94 loc) · 3.57 KB
/
action.yml
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
name: pytest --last-failed
description: "Run pytest with --last-failed if the previous run had failed tests"
author: '@sjvrijn'
branding:
icon: 'repeat'
color: 'blue'
inputs:
pytest-args:
description: >
Additional arguments to be passed on to pytest
required: false
default: ''
run-all-on-success:
description: >
Whether to perform a full run of the test-suite once any previously failed
tests pass again. This may catch any unintended side effects of fixing the
previously failed tests, at the cost of running the entire suite again.
required: false
default: false
runs:
using: "composite"
steps:
# Each job created by a matrix should get its own cache
# If you know a better way to has the `matrix` object, please submit a PR!!!
- name: Create hash for current matrix job
run: |
MY_STRING=$(cat << EOF
${{ toJson(matrix) }}
EOF
)
MY_STRING="${MY_STRING//'%'/'%25'}"
MY_STRING="${MY_STRING//$'\n'/'%0A'}"
MY_STRING="${MY_STRING//$'\r'/'%0D'}"
echo "$MY_STRING" > matrix.json
echo "MATRIX_HASH=$(md5sum matrix.json | cut -d' ' -f1)" >> $GITHUB_ENV
shell: bash
# Initial restore of cache, if it exists
- uses: actions/cache/restore@v4
id: cache
with:
path: .pytest_cache
key: pytest-cache-${{github.event.number}}-${{env.MATRIX_HASH}}
restore-keys:
pytest-cache-${{github.event.number}}-${{env.MATRIX_HASH}}-
# Determine if pytests's lastfailed cache contains any entries.
#
# Specifically, we assume the output of the command
# $ pytest --collect-only -qqq --no-header --no-summary --cache-show cache/lastfailed
# is in the following format:
#
# > cachedir: <CACHE_DIR>
# > ----------------------------------------- cache values for 'cache/lastfailed' ------------------------------------------
# > cache/lastfailed contains:
# > {...}
#
# If no tests have previously failed, an empty dictionary will be displayed
# as shown above. This line will then consist of (at least) 5 bytes: two
# spaces, two curly braces and an endline.
#
# The command below takes the fourth line of this output and counts the
# number of bytes. If it consists of more than 5 bytes, at least one failed
# test is being reported.
- name: Check cache length
run: |
echo "CACHE_OUTPUT_LENGTH=$( pytest --collect-only -qqq --no-header --no-summary --cache-show cache/lastfailed | sed -n '4 p' | wc -c )" >> "$GITHUB_ENV"
shell: bash
# If some tests previously failed, i.e., a non-empty cache exists, run pytest --last-failed
- name: Run only --last-failed
id: last_failed
if: |
always() &&
env.CACHE_OUTPUT_LENGTH > 5
run: |
pytest --last-failed ${{ inputs.pytest-args }}
shell: bash
# If 'pytest --last-failed' was skipped (i.e. no entries in cache), or succeeded
# (i.e. last-failed tests pass again), run all tests with a clean cache
- name: Run all tests
id: all_tests
if: |
always() &&
( steps.last_failed.outcome == 'skipped'
|| ( steps.last_failed.outcome == 'success' && inputs.run-all-on-success )
)
run: |
pytest --cache-clear ${{ inputs.pytest-args }}
shell: bash
# Save pytest cache for future runs
- name: Update pytest cache
uses: actions/cache/save@v4
if: always()
with:
path: .pytest_cache
key: pytest-cache-${{github.event.number}}-${{env.MATRIX_HASH}}-${{github.run_id}}