Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 892baae

Browse filesBrowse files
committed
Add support to .tool-version file format
Create a function to parse tool-version (asdf-vm) format, add the associated tests, and updated the documentation Ticket-ID: actions#571
1 parent bd6b4b6 commit 892baae
Copy full SHA for 892baae

File tree

Expand file treeCollapse file tree

4 files changed

+47
-6
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+47
-6
lines changed

‎__tests__/utils.test.ts

Copy file name to clipboardExpand all lines: __tests__/utils.test.ts
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as cache from '@actions/cache';
22
import * as core from '@actions/core';
33
import {
4+
parsePythonVersionFile,
45
validateVersion,
56
validatePythonVersionFormatForPyPy,
67
isCacheFeatureAvailable
@@ -9,6 +10,20 @@ import {
910
jest.mock('@actions/cache');
1011
jest.mock('@actions/core');
1112

13+
describe('parsePythonVersionFile', () => {
14+
it('handle the content of a .python-version file', () => {
15+
expect(parsePythonVersionFile('3.6')).toEqual('3.6')
16+
});
17+
18+
it('trims extra spaces at the end of the content', () => {
19+
expect(parsePythonVersionFile('3.7 ')).toEqual('3.7')
20+
});
21+
22+
it('parses correctly the content of .tool-version files', () => {
23+
expect(parsePythonVersionFile('python 3.7')).toEqual('3.7')
24+
});
25+
});
26+
1227
describe('validatePythonVersionFormatForPyPy', () => {
1328
it.each([
1429
['3.6', true],

‎docs/advanced-usage.md

Copy file name to clipboardExpand all lines: docs/advanced-usage.md
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,10 @@ jobs:
239239

240240
## Using the `python-version-file` input
241241

242-
`setup-python` action can read Python or PyPy version from a version file. `python-version-file` input is used for specifying the path to the version file. If the file that was supplied to `python-version-file` input doesn't exist, the action will fail with error.
242+
The python-version-file input accepts a path to a file containing the version of Python to be used by a project, for example .python-version, or .tool-versions.
243+
If both the python-version and the python-version-file inputs are provided then the python-version input is used.
243244

244-
>In case both `python-version` and `python-version-file` inputs are supplied, the `python-version-file` input will be ignored due to its lower priority.
245+
> The action will search for the python version file relative to the repository root.
245246

246247
```yaml
247248
steps:

‎src/setup-python.ts

Copy file name to clipboardExpand all lines: src/setup-python.ts
+13-4Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as path from 'path';
55
import * as os from 'os';
66
import fs from 'fs';
77
import {getCacheDistributor} from './cache-distributions/cache-factory';
8-
import {isCacheFeatureAvailable, logWarning, IS_MAC} from './utils';
8+
import {parsePythonVersionFile, isCacheFeatureAvailable, logWarning, IS_MAC} from './utils';
99

1010
function isPyPyVersion(versionSpec: string) {
1111
return versionSpec.startsWith('pypy');
@@ -37,20 +37,29 @@ function resolveVersionInput() {
3737
}
3838

3939
if (versionFile) {
40-
if (!fs.existsSync(versionFile)) {
40+
const versionFilePath = path.join(
41+
process.env.GITHUB_WORKSPACE!,
42+
versionFile
43+
);
44+
45+
if (!fs.existsSync(versionFilePath)) {
4146
throw new Error(
42-
`The specified python version file at: ${versionFile} doesn't exist.`
47+
`The specified python version file at: ${versionFilePath} doesn't exist.`
4348
);
4449
}
45-
const version = fs.readFileSync(versionFile, 'utf8');
50+
51+
const version = parsePythonVersionFile(fs.readFileSync(versionFilePath, 'utf8'));
4652
core.info(`Resolved ${versionFile} as ${version}`);
53+
4754
return [version];
4855
}
4956

5057
logWarning(
5158
"Neither 'python-version' nor 'python-version-file' inputs were supplied. Attempting to find '.python-version' file."
5259
);
60+
5361
versionFile = '.python-version';
62+
5463
if (fs.existsSync(versionFile)) {
5564
const version = fs.readFileSync(versionFile, 'utf8');
5665
core.info(`Resolved ${versionFile} as ${version}`);

‎src/utils.ts

Copy file name to clipboardExpand all lines: src/utils.ts
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ export interface IPyPyManifestRelease {
2828
files: IPyPyManifestAsset[];
2929
}
3030

31+
export function parsePythonVersionFile(contents: string): string {
32+
let pythonVersion: string | undefined;
33+
34+
// Try to find the version in tool-version file
35+
const found = contents.match(/^(?:python\s+)?v?(?<version>[^\s]+)$/m);
36+
pythonVersion = found?.groups?.version;
37+
38+
// In the case of an unknown format,
39+
// return as is and evaluate the version separately.
40+
if (!pythonVersion) {
41+
pythonVersion = contents.trim();
42+
}
43+
44+
return pythonVersion as string;
45+
}
46+
3147
/** create Symlinks for downloaded PyPy
3248
* It should be executed only for downloaded versions in runtime, because
3349
* toolcache versions have this setup.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.