From 13399d0718a14941111c18769dafa65faff677d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kry=C5=A1tof=20Korb?= Date: Thu, 4 Jan 2024 00:33:23 +0100 Subject: [PATCH 1/3] Enhance reading from .python-version --- __tests__/utils.test.ts | 26 ++++++++++++++++++++++++-- src/setup-python.ts | 4 ++-- src/utils.ts | 29 ++++++++++++++++++++++------- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 6c0f0e13..f724195b 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -10,7 +10,7 @@ import { validatePythonVersionFormatForPyPy, isCacheFeatureAvailable, getVersionInputFromFile, - getVersionInputFromPlainFile, + getVersionsInputFromPlainFile, getVersionInputFromTomlFile, getNextPageUrl, isGhes, @@ -95,7 +95,7 @@ const tempDir = path.join( ); describe('Version from file test', () => { - it.each([getVersionInputFromPlainFile, getVersionInputFromFile])( + it.each([getVersionsInputFromPlainFile, getVersionInputFromFile])( 'Version from plain file test', async _fn => { await io.mkdirP(tempDir); @@ -106,6 +106,28 @@ describe('Version from file test', () => { 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])( 'Version from standard pyproject.toml test', async _fn => { diff --git a/src/setup-python.ts b/src/setup-python.ts index ab5931b8..5d585d73 100644 --- a/src/setup-python.ts +++ b/src/setup-python.ts @@ -11,7 +11,7 @@ import { logWarning, IS_MAC, getVersionInputFromFile, - getVersionInputFromPlainFile + getVersionsInputFromPlainFile } from './utils'; function isPyPyVersion(versionSpec: string) { @@ -35,7 +35,7 @@ async function cacheDependencies(cache: string, pythonVersion: string) { function resolveVersionInputFromDefaultFile(): string[] { const couples: [string, (versionFile: string) => string[]][] = [ - ['.python-version', getVersionInputFromPlainFile] + ['.python-version', getVersionsInputFromPlainFile] ]; for (const [versionFile, _fn] of couples) { logWarning( diff --git a/src/utils.ts b/src/utils.ts index 6274895e..17460986 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -269,13 +269,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[] { - core.debug(`Trying to resolve version form ${versionFile}`); - const version = fs.readFileSync(versionFile, 'utf8').trim(); - core.info(`Resolved ${versionFile} as ${version}`); - return [version]; +export function getVersionsInputFromPlainFile(versionFile: string): string[] { + core.debug(`Trying to resolve versions form ${versionFile}`); + const content = fs.readFileSync(versionFile, 'utf8').trim(); + const lines = content.split(/\r\n|\r|\n/); + 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; } /** @@ -319,7 +334,7 @@ export function getVersionInputFromFile(versionFile: string): string[] { } else if (versionFile.match('.tool-versions')) { return getVersionInputFromToolVersions(versionFile); } else { - return getVersionInputFromPlainFile(versionFile); + return getVersionsInputFromPlainFile(versionFile); } } From e73db4617cc9be9f51d5673a35d09c00a7f96604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kry=C5=A1tof=20Korb?= Date: Thu, 4 Jan 2024 00:45:19 +0100 Subject: [PATCH 2/3] Fix typos --- src/utils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 17460986..f39006d9 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -228,7 +228,7 @@ function extractValue(obj: any, keys: string[]): string | undefined { * If none is present, returns an empty list. */ 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'); // Normalize the line endings in the pyprojectFile @@ -276,7 +276,7 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] { * - Trims whitespace. */ export function getVersionsInputFromPlainFile(versionFile: string): string[] { - core.debug(`Trying to resolve versions form ${versionFile}`); + core.debug(`Trying to resolve versions from ${versionFile}`); const content = fs.readFileSync(versionFile, 'utf8').trim(); const lines = content.split(/\r\n|\r|\n/); const versions = lines From 795bdd5e6c8b40c29293dcea30d7ffbf6fe620fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kry=C5=A1tof=20Korb?= Date: Wed, 9 Apr 2025 11:01:32 +0200 Subject: [PATCH 3/3] Fix lint --- __tests__/utils.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index f724195b..052bce7a 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -123,7 +123,8 @@ describe('Version from file test', () => { 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'; + 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']); }