Skip to content

Commit 0d5da6a

Browse files
authored
Read python version from pyproject.toml (fix actions#542) (actions#669)
1 parent 3f824b7 commit 0d5da6a

File tree

10 files changed

+2669
-53
lines changed

10 files changed

+2669
-53
lines changed

.github/workflows/e2e-tests.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,24 @@ jobs:
7979
run: python __tests__/verify-python.py 3.10
8080
- name: Run python-path sample 3.10
8181
run: pipx run --python '${{ steps.cp310.outputs.python-path }}' nox --version
82+
83+
- name: Run with setup-python ==3.8
84+
uses: ./
85+
with:
86+
python-version: '==3.8'
87+
- name: Verify ==3.8
88+
run: python __tests__/verify-python.py 3.8
89+
90+
- name: Run with setup-python <3.11
91+
uses: ./
92+
with:
93+
python-version: '<3.11'
94+
- name: Verify <3.11
95+
run: python __tests__/verify-python.py 3.10
96+
97+
- name: Run with setup-python >3.8
98+
uses: ./
99+
with:
100+
python-version: '>3.8'
101+
- name: Verify >3.8
102+
run: python __tests__/verify-python.py 3.11

.github/workflows/test-python.yml

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,152 @@ jobs:
8686
id: setup-python
8787
uses: ./
8888
with:
89-
python-version-file: '.python-version'
89+
python-version-file: .python-version
90+
91+
- name: Check python-path
92+
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
93+
shell: bash
94+
95+
- name: Validate version
96+
run: |
97+
$pythonVersion = (python --version)
98+
if ("Python ${{ matrix.python }}" -ne "$pythonVersion"){
99+
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}"
100+
exit 1
101+
}
102+
$pythonVersion
103+
shell: pwsh
104+
105+
- name: Run simple code
106+
run: python -c 'import math; print(math.factorial(5))'
107+
108+
setup-versions-from-file-without-parameter:
109+
name: Setup ${{ matrix.python }} ${{ matrix.os }} version file without parameter
110+
runs-on: ${{ matrix.os }}
111+
strategy:
112+
fail-fast: false
113+
matrix:
114+
os: [macos-latest, windows-latest, ubuntu-20.04, ubuntu-22.04]
115+
python: [3.5.4, 3.6.7, 3.7.5, 3.8.15, 3.9.13]
116+
exclude:
117+
- os: ubuntu-22.04
118+
python: 3.5.4
119+
- os: ubuntu-22.04
120+
python: 3.6.7
121+
- os: ubuntu-22.04
122+
python: 3.7.5
123+
- os: windows-latest
124+
python: 3.8.15
125+
steps:
126+
- name: Checkout
127+
uses: actions/checkout@v3
128+
129+
- name: build-version-file ${{ matrix.python }}
130+
run: echo ${{ matrix.python }} > .python-version
131+
132+
- name: setup-python ${{ matrix.python }}
133+
id: setup-python
134+
uses: ./
135+
136+
- name: Check python-path
137+
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
138+
shell: bash
139+
140+
- name: Validate version
141+
run: |
142+
$pythonVersion = (python --version)
143+
if ("Python ${{ matrix.python }}" -ne "$pythonVersion"){
144+
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}"
145+
exit 1
146+
}
147+
$pythonVersion
148+
shell: pwsh
149+
150+
- name: Run simple code
151+
run: python -c 'import math; print(math.factorial(5))'
152+
153+
setup-versions-from-standard-pyproject-file:
154+
name: Setup ${{ matrix.python }} ${{ matrix.os }} standard pyproject file
155+
runs-on: ${{ matrix.os }}
156+
strategy:
157+
fail-fast: false
158+
matrix:
159+
os: [macos-latest, windows-latest, ubuntu-20.04, ubuntu-22.04]
160+
python: [3.5.4, 3.6.7, 3.7.5, 3.8.15, 3.9.13]
161+
exclude:
162+
- os: ubuntu-22.04
163+
python: 3.5.4
164+
- os: ubuntu-22.04
165+
python: 3.6.7
166+
- os: ubuntu-22.04
167+
python: 3.7.5
168+
- os: windows-latest
169+
python: 3.8.15
170+
steps:
171+
- name: Checkout
172+
uses: actions/checkout@v3
173+
174+
- name: build-version-file ${{ matrix.python }}
175+
run: |
176+
echo '[project]
177+
requires-python = "${{ matrix.python }}"
178+
' > pyproject.toml
179+
180+
- name: setup-python ${{ matrix.python }}
181+
id: setup-python
182+
uses: ./
183+
with:
184+
python-version-file: pyproject.toml
185+
186+
- name: Check python-path
187+
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'
188+
shell: bash
189+
190+
- name: Validate version
191+
run: |
192+
$pythonVersion = (python --version)
193+
if ("Python ${{ matrix.python }}" -ne "$pythonVersion"){
194+
Write-Host "The current version is $pythonVersion; expected version is ${{ matrix.python }}"
195+
exit 1
196+
}
197+
$pythonVersion
198+
shell: pwsh
199+
200+
- name: Run simple code
201+
run: python -c 'import math; print(math.factorial(5))'
202+
203+
setup-versions-from-poetry-pyproject-file:
204+
name: Setup ${{ matrix.python }} ${{ matrix.os }} poetry pyproject file
205+
runs-on: ${{ matrix.os }}
206+
strategy:
207+
fail-fast: false
208+
matrix:
209+
os: [macos-latest, windows-latest, ubuntu-20.04, ubuntu-22.04]
210+
python: [3.5.4, 3.6.7, 3.7.5, 3.8.15, 3.9.13]
211+
exclude:
212+
- os: ubuntu-22.04
213+
python: 3.5.4
214+
- os: ubuntu-22.04
215+
python: 3.6.7
216+
- os: ubuntu-22.04
217+
python: 3.7.5
218+
- os: windows-latest
219+
python: 3.8.15
220+
steps:
221+
- name: Checkout
222+
uses: actions/checkout@v3
223+
224+
- name: build-version-file ${{ matrix.python }}
225+
run: |
226+
echo '[tool.poetry.dependencies]
227+
python = "${{ matrix.python }}"
228+
' > pyproject.toml
229+
230+
- name: setup-python ${{ matrix.python }}
231+
id: setup-python
232+
uses: ./
233+
with:
234+
python-version-file: pyproject.toml
90235

91236
- name: Check python-path
92237
run: ./__tests__/check-python-path.sh '${{ steps.setup-python.outputs.python-path }}'

.licenses/npm/@iarna/toml.dep.yml

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__tests__/utils.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import * as cache from '@actions/cache';
22
import * as core from '@actions/core';
3+
import * as io from '@actions/io';
4+
5+
import fs from 'fs';
6+
import path from 'path';
7+
38
import {
49
validateVersion,
510
validatePythonVersionFormatForPyPy,
6-
isCacheFeatureAvailable
11+
isCacheFeatureAvailable,
12+
getVersionInputFromFile,
13+
getVersionInputFromPlainFile,
14+
getVersionInputFromTomlFile
715
} from '../src/utils';
816

917
jest.mock('@actions/cache');
@@ -73,3 +81,58 @@ describe('isCacheFeatureAvailable', () => {
7381
expect(isCacheFeatureAvailable()).toBe(true);
7482
});
7583
});
84+
85+
const tempDir = path.join(
86+
__dirname,
87+
'runner',
88+
path.join(Math.random().toString(36).substring(7)),
89+
'temp'
90+
);
91+
92+
describe('Version from file test', () => {
93+
it.each([getVersionInputFromPlainFile, getVersionInputFromFile])(
94+
'Version from plain file test',
95+
async _fn => {
96+
await io.mkdirP(tempDir);
97+
const pythonVersionFileName = 'python-version.file';
98+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
99+
const pythonVersionFileContent = '3.7';
100+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
101+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersionFileContent]);
102+
}
103+
);
104+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
105+
'Version from standard pyproject.toml test',
106+
async _fn => {
107+
await io.mkdirP(tempDir);
108+
const pythonVersionFileName = 'pyproject.toml';
109+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
110+
const pythonVersion = '>=3.7';
111+
const pythonVersionFileContent = `[project]\nrequires-python = "${pythonVersion}"`;
112+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
113+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
114+
}
115+
);
116+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
117+
'Version from poetry pyproject.toml test',
118+
async _fn => {
119+
await io.mkdirP(tempDir);
120+
const pythonVersionFileName = 'pyproject.toml';
121+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
122+
const pythonVersion = '>=3.7';
123+
const pythonVersionFileContent = `[tool.poetry.dependencies]\npython = "${pythonVersion}"`;
124+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
125+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
126+
}
127+
);
128+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
129+
'Version undefined',
130+
async _fn => {
131+
await io.mkdirP(tempDir);
132+
const pythonVersionFileName = 'pyproject.toml';
133+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
134+
fs.writeFileSync(pythonVersionFilePath, ``);
135+
expect(_fn(pythonVersionFilePath)).toEqual([]);
136+
}
137+
);
138+
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy