Compare commits

...

3 Commits

Author SHA1 Message Date
Kryštof Korb 0b21d0f07f
Merge 04e00cd600 into 19dfb7b659 2024-10-07 12:30:24 -04:00
Kryštof Korb 04e00cd600 Fix typos 2024-01-04 00:45:19 +01:00
Kryštof Korb bd1ce022fe Enhance reading from .python-version 2024-01-04 00:33:23 +01:00
3 changed files with 49 additions and 12 deletions

View File

@ -10,7 +10,7 @@ import {
validatePythonVersionFormatForPyPy, validatePythonVersionFormatForPyPy,
isCacheFeatureAvailable, isCacheFeatureAvailable,
getVersionInputFromFile, getVersionInputFromFile,
getVersionInputFromPlainFile, getVersionsInputFromPlainFile,
getVersionInputFromTomlFile, getVersionInputFromTomlFile,
getNextPageUrl, getNextPageUrl,
IS_WINDOWS, IS_WINDOWS,
@ -93,7 +93,7 @@ const tempDir = path.join(
); );
describe('Version from file test', () => { describe('Version from file test', () => {
it.each([getVersionInputFromPlainFile, getVersionInputFromFile])( it.each([getVersionsInputFromPlainFile, getVersionInputFromFile])(
'Version from plain file test', 'Version from plain file test',
async _fn => { async _fn => {
await io.mkdirP(tempDir); await io.mkdirP(tempDir);
@ -104,6 +104,28 @@ describe('Version from file test', () => {
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersionFileContent]); expect(_fn(pythonVersionFilePath)).toEqual([pythonVersionFileContent]);
} }
); );
it.each([getVersionsInputFromPlainFile, getVersionInputFromFile])(
'Versions from multiline plain file test',
async _fn => {
await io.mkdirP(tempDir);
const pythonVersionFileName = 'python-version.file';
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
const pythonVersionFileContent = '3.8\r\n3.7';
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
expect(_fn(pythonVersionFilePath)).toEqual(['3.8', '3.7']);
}
);
it.each([getVersionsInputFromPlainFile, getVersionInputFromFile])(
'Version from complex plain file test',
async _fn => {
await io.mkdirP(tempDir);
const pythonVersionFileName = 'python-version.file';
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
const pythonVersionFileContent = '3.10/envs/virtualenv\r# 3.9\n3.8\r\n3.7\r\n 3.6 \r\n';
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
expect(_fn(pythonVersionFilePath)).toEqual(['3.10', '3.8', '3.7', '3.6']);
}
);
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
'Version from standard pyproject.toml test', 'Version from standard pyproject.toml test',
async _fn => { async _fn => {

View File

@ -11,7 +11,7 @@ import {
logWarning, logWarning,
IS_MAC, IS_MAC,
getVersionInputFromFile, getVersionInputFromFile,
getVersionInputFromPlainFile getVersionsInputFromPlainFile
} from './utils'; } from './utils';
function isPyPyVersion(versionSpec: string) { function isPyPyVersion(versionSpec: string) {
@ -35,7 +35,7 @@ async function cacheDependencies(cache: string, pythonVersion: string) {
function resolveVersionInputFromDefaultFile(): string[] { function resolveVersionInputFromDefaultFile(): string[] {
const couples: [string, (versionFile: string) => string[]][] = [ const couples: [string, (versionFile: string) => string[]][] = [
['.python-version', getVersionInputFromPlainFile] ['.python-version', getVersionsInputFromPlainFile]
]; ];
for (const [versionFile, _fn] of couples) { for (const [versionFile, _fn] of couples) {
logWarning( logWarning(

View File

@ -222,7 +222,7 @@ function extractValue(obj: any, keys: string[]): string | undefined {
* If none is present, returns an empty list. * If none is present, returns an empty list.
*/ */
export function getVersionInputFromTomlFile(versionFile: string): string[] { export function getVersionInputFromTomlFile(versionFile: string): string[] {
core.debug(`Trying to resolve version form ${versionFile}`); core.debug(`Trying to resolve version from ${versionFile}`);
let pyprojectFile = fs.readFileSync(versionFile, 'utf8'); let pyprojectFile = fs.readFileSync(versionFile, 'utf8');
// Normalize the line endings in the pyprojectFile // Normalize the line endings in the pyprojectFile
@ -263,13 +263,28 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {
} }
/** /**
* Python version extracted from a plain text file. * Python versions extracted from a plain text file.
* - Resolves multiple versions from multiple lines.
* - Handles pyenv-virtualenv pointers (e.g. `3.10/envs/virtualenv`).
* - Ignores empty lines and lines starting with `#`
* - Trims whitespace.
*/ */
export function getVersionInputFromPlainFile(versionFile: string): string[] { export function getVersionsInputFromPlainFile(versionFile: string): string[] {
core.debug(`Trying to resolve version form ${versionFile}`); core.debug(`Trying to resolve versions from ${versionFile}`);
const version = fs.readFileSync(versionFile, 'utf8').trim(); const content = fs.readFileSync(versionFile, 'utf8').trim();
core.info(`Resolved ${versionFile} as ${version}`); const lines = content.split(/\r\n|\r|\n/);
return [version]; const versions = lines
.map(line => {
if (line.startsWith('#') || line.trim() === '') {
return undefined;
}
let version: string = line.trim();
version = version.split('/')[0];
return version;
})
.filter(version => version !== undefined) as string[];
core.info(`Resolved ${versionFile} as ${versions.join(', ')}`);
return versions;
} }
/** /**
@ -279,7 +294,7 @@ export function getVersionInputFromFile(versionFile: string): string[] {
if (versionFile.endsWith('.toml')) { if (versionFile.endsWith('.toml')) {
return getVersionInputFromTomlFile(versionFile); return getVersionInputFromTomlFile(versionFile);
} else { } else {
return getVersionInputFromPlainFile(versionFile); return getVersionsInputFromPlainFile(versionFile);
} }
} }