Skip to content

Commit 97860b5

Browse files
authored
setup-poetry: Add use-cache input and regression tests (#8)
* github: Add tests for setup-python and setup-poetry * github: Fix version parsing * github: Add another job to combine test results for status checks * setup-poetry: Add use-cache input and cache-hit output * github: Add test for setup-poetry caching * setup-poetry: Document new input and output * github: Add a use_cache=false test * github: Change conditional step names to reflect result of condition, not the check
1 parent f732705 commit 97860b5

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

.github/workflows/test_actions.yml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,122 @@ on:
55
workflow_dispatch:
66

77
jobs:
8+
test_setup_python:
9+
name: Test setup-python
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
matrix:
13+
os: [windows-latest, ubuntu-latest]
14+
python-version: [3.9, '3.10', 3.11, 3.12, 3.13]
15+
steps:
16+
- name: Check out repo
17+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
18+
- name: Set up Python
19+
uses: ./setup-python
20+
with:
21+
python-version: ${{ matrix.python-version }}
22+
- name: Check Python version
23+
run: |
24+
import sys
25+
version = sys.version_info[:2]
26+
expected_version = tuple(map(int, "${{ matrix.python-version }}".split(".")))
27+
if version != expected_version:
28+
print(f"::error title=Test Failure::The Python version does not match. Got {version}, expected {expected_version}.")
29+
sys.exit(1)
30+
shell: python
31+
32+
test_setup_poetry:
33+
name: Test setup-poetry
34+
runs-on: ${{ matrix.os }}
35+
strategy:
36+
matrix:
37+
os: [windows-latest, ubuntu-latest]
38+
python-version: [3.9, '3.10', 3.11, 3.12, 3.13]
39+
steps:
40+
- name: Check out repo
41+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
42+
- name: Set up Python
43+
uses: ./setup-python
44+
with:
45+
python-version: ${{ matrix.python-version }}
46+
- name: Set up Poetry
47+
uses: ./setup-poetry
48+
- name: Create project
49+
run: poetry new test-project
50+
- name: Check that the project was created
51+
run: |
52+
if [ ! -f test-project/pyproject.toml ]; then
53+
echo "::error title=Test Failure::The project file does not exist."
54+
exit 1
55+
fi
56+
shell: bash
57+
58+
test_setup_poetry_cache_hit:
59+
name: Test setup-poetry (cache hit)
60+
runs-on: ${{ matrix.os }}
61+
needs: test_setup_poetry
62+
strategy:
63+
matrix:
64+
os: [windows-latest, ubuntu-latest]
65+
python-version: [3.9, '3.10', 3.11, 3.12, 3.13]
66+
steps:
67+
- name: Check out repo
68+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
69+
- name: Set up Python
70+
uses: ./setup-python
71+
with:
72+
python-version: ${{ matrix.python-version }}
73+
- name: Set up Poetry
74+
id: setup-poetry
75+
uses: ./setup-poetry
76+
- name: Error if cache miss
77+
if: steps.setup-poetry.outputs.cache-hit != 'true'
78+
run: echo "::error title=Test Failure::Expected cache hit."; exit 1
79+
shell: bash
80+
- name: Create project
81+
run: poetry new test-project
82+
- name: Check that the project was created
83+
run: |
84+
if [ ! -f test-project/pyproject.toml ]; then
85+
echo "::error title=Test Failure::The project file does not exist."
86+
exit 1
87+
fi
88+
shell: bash
89+
90+
test_setup_poetry_no_cache:
91+
name: Test setup-poetry (no cache)
92+
runs-on: ${{ matrix.os }}
93+
needs: test_setup_poetry
94+
strategy:
95+
matrix:
96+
os: [windows-latest, ubuntu-latest]
97+
python-version: [3.9, '3.10', 3.11, 3.12, 3.13]
98+
steps:
99+
- name: Check out repo
100+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
101+
- name: Set up Python
102+
uses: ./setup-python
103+
with:
104+
python-version: ${{ matrix.python-version }}
105+
- name: Set up Poetry
106+
id: setup-poetry
107+
uses: ./setup-poetry
108+
with:
109+
use-cache: false
110+
- name: Error if cache hit
111+
if: steps.setup-poetry.outputs.cache-hit == 'true'
112+
run: echo "::error title=Test Failure::Expected cache miss."; exit 1
113+
shell: bash
114+
- name: Create project
115+
run: poetry new test-project
116+
- name: Check that the project was created
117+
run: |
118+
if [ ! -f test-project/pyproject.toml ]; then
119+
echo "::error title=Test Failure::The project file does not exist."
120+
exit 1
121+
fi
122+
shell: bash
123+
8124
test_check_project_version:
9125
name: Test check-project-version
10126
runs-on: ubuntu-latest
@@ -38,6 +154,7 @@ jobs:
38154
run: |
39155
echo "::error title=Test Failure::The previous step did not fail as expected."
40156
exit 1
157+
41158
test_update_project_version:
42159
name: Test update-project-version
43160
runs-on: ubuntu-latest
@@ -84,3 +201,22 @@ jobs:
84201
with:
85202
project-directory: test-project
86203
expected-version: 1.0.2.dev1
204+
205+
# This job is intended to combine the test results so we don't have to list
206+
# each matrix combination in the required status check settings. There are a
207+
# lot of corner cases that make this harder than it should be; see See
208+
# https://github.com/orgs/community/discussions/26822 for more info.
209+
test_results:
210+
name: Test Results
211+
runs-on: ubuntu-latest
212+
needs: [
213+
test_setup_python,
214+
test_setup_poetry,
215+
test_setup_poetry_cache_hit,
216+
test_setup_poetry_no_cache,
217+
test_check_project_version,
218+
test_update_project_version
219+
]
220+
if: ${{ !cancelled() }}
221+
steps:
222+
- run: exit 0

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
- [Usage](#usage-1)
1818
- [Inputs](#inputs-1)
1919
- [`poetry-version`](#poetry-version)
20+
- [`use-cache`](#use-cache)
21+
- [Outputs](#outputs-1)
22+
- [`cache-hit`](#cache-hit)
2023
- [`ni/python-actions/check-project-version`](#nipython-actionscheck-project-version)
2124
- [Usage](#usage-2)
2225
- [Inputs](#inputs-2)
@@ -125,6 +128,17 @@ steps:
125128
- run: poetry install -v
126129
```
127130

131+
#### `use-cache`
132+
133+
If you run into caching problems, you can disable caching by specifying `use-cache: false`.
134+
135+
### Outputs
136+
137+
#### `cache-hit`
138+
139+
You can use `cache-hit` to check whether Poetry was loaded from cache. This is mainly intended for
140+
testing the action.
141+
128142
## `ni/python-actions/check-project-version`
129143

130144
The `check-project-version` action uses Poetry to get the version of a Python project and checks

setup-poetry/action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ description: Install Poetry, add it to the PATH, and cache it to speed up workfl
33
inputs:
44
poetry-version:
55
default: 1.8.2
6+
use-cache:
7+
description: >
8+
A Boolean specifying whether to use the cache. Set this to false to work
9+
around caching problems.
10+
default: true
11+
outputs:
12+
cache-hit:
13+
description: >
14+
A Boolean indicating whether Poetry was loaded from the cache. This is
15+
mainly intended for testing the action.
16+
value: ${{ steps.cache-poetry.outputs.cache-hit }}
617
runs:
718
using: composite
819
steps:
@@ -38,6 +49,7 @@ runs:
3849
echo "poetry-home=$POETRY_HOME" >> "$GITHUB_OUTPUT"
3950
shell: bash
4051
- name: Cache poetry
52+
if: ${{ inputs.use-cache == 'true' }}
4153
id: cache-poetry
4254
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
4355
with:

0 commit comments

Comments
 (0)