This commit is contained in:
Pablo Ifrán 2023-06-19 18:12:53 -05:00 committed by GitHub
commit 3fbd065cda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 11 deletions

View file

@ -1,6 +1,7 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import {
parsePythonVersionFile,
validateVersion,
validatePythonVersionFormatForPyPy,
isCacheFeatureAvailable
@ -9,6 +10,26 @@ import {
jest.mock('@actions/cache');
jest.mock('@actions/core');
describe('parsePythonVersionFile', () => {
it('handle the content of a .python-version file', () => {
expect(parsePythonVersionFile('3.6')).toEqual('3.6');
});
it('trims extra spaces at the end of the content', () => {
expect(parsePythonVersionFile('3.7 ')).toEqual('3.7');
});
it('parses correctly the content of .tool-version files', () => {
expect(parsePythonVersionFile('python 3.7')).toEqual('3.7');
});
it('parses correctly pypy version content of the .tool-version files', () => {
expect(parsePythonVersionFile('python pypy3.9-7.3.10')).toEqual(
'pypy3.9-7.3.10'
);
});
});
describe('validatePythonVersionFormatForPyPy', () => {
it.each([
['3.6', true],

View file

@ -239,7 +239,8 @@ jobs:
## Using the `python-version-file` input
`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.
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.
If both the python-version and the python-version-file inputs are provided then the python-version input is used.
>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.

View file

@ -65,8 +65,11 @@ export async function findPyPyVersion(
));
if (!installDir) {
({installDir, resolvedPythonVersion, resolvedPyPyVersion} =
await pypyInstall.installPyPy(
({
installDir,
resolvedPythonVersion,
resolvedPyPyVersion
} = await pypyInstall.installPyPy(
pypyVersionSpec.pypyVersion,
pypyVersionSpec.pythonVersion,
architecture,

View file

@ -5,7 +5,12 @@ import * as path from 'path';
import * as os from 'os';
import fs from 'fs';
import {getCacheDistributor} from './cache-distributions/cache-factory';
import {isCacheFeatureAvailable, logWarning, IS_MAC} from './utils';
import {
parsePythonVersionFile,
isCacheFeatureAvailable,
logWarning,
IS_MAC
} from './utils';
function isPyPyVersion(versionSpec: string) {
return versionSpec.startsWith('pypy');
@ -42,15 +47,21 @@ function resolveVersionInput() {
`The specified python version file at: ${versionFile} doesn't exist.`
);
}
const version = fs.readFileSync(versionFile, 'utf8');
const version = parsePythonVersionFile(
fs.readFileSync(versionFile, 'utf8')
);
core.info(`Resolved ${versionFile} as ${version}`);
return [version];
}
logWarning(
"Neither 'python-version' nor 'python-version-file' inputs were supplied. Attempting to find '.python-version' file."
);
versionFile = '.python-version';
if (fs.existsSync(versionFile)) {
const version = fs.readFileSync(versionFile, 'utf8');
core.info(`Resolved ${versionFile} as ${version}`);

View file

@ -28,6 +28,22 @@ export interface IPyPyManifestRelease {
files: IPyPyManifestAsset[];
}
export function parsePythonVersionFile(contents: string): string {
let pythonVersion: string | undefined;
// Try to find the version in tool-version file
const found = contents.match(/^(?:python\s+)?v?(?<version>[^\s]+)$/m);
pythonVersion = found?.groups?.version;
// In the case of an unknown format,
// return as is and evaluate the version separately.
if (!pythonVersion) {
pythonVersion = contents.trim();
}
return pythonVersion as string;
}
/** create Symlinks for downloaded PyPy
* It should be executed only for downloaded versions in runtime, because
* toolcache versions have this setup.