From 9e32ee01fa170b64b0d1254ee8221ad10470d447 Mon Sep 17 00:00:00 2001 From: Daniel Bowring Date: Tue, 26 Mar 2024 11:49:00 +1100 Subject: [PATCH] Support reading python version from mise config --- __tests__/utils.test.ts | 24 ++++++++++++++++++++++++ dist/setup/index.js | 29 +++++++++++++++++++++-------- src/utils.ts | 32 ++++++++++++++++++++++++-------- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index c2c6bca..77e0564 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -128,6 +128,30 @@ describe('Version from file test', () => { expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); } ); + it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( + 'Version from mise .mise.toml test', + async _fn => { + await io.mkdirP(tempDir); + const pythonVersionFileName = '.mise.toml'; + const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName); + const pythonVersion = '3.7.0'; + const pythonVersionFileContent = `[tools]\npython = "${pythonVersion}"`; + fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent); + expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); + } + ); + it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( + 'Version from mise verbose .mise.toml test', + async _fn => { + await io.mkdirP(tempDir); + const pythonVersionFileName = '.mise.toml'; + const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName); + const pythonVersion = '3.7.0'; + const pythonVersionFileContent = `[tools]\npython = { version="${pythonVersion}", virtualenv=".venv" }`; + fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent); + expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); + } + ); it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( 'Version undefined', async _fn => { diff --git a/dist/setup/index.js b/dist/setup/index.js index f1e3e29..b250a25 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -92090,6 +92090,9 @@ function getOSInfo() { }); } exports.getOSInfo = getOSInfo; +function isString(value) { + return typeof value === 'string' || value instanceof String; +} /** * Extract a value from an object by following the keys path provided. * If the value is present, it is returned. Otherwise undefined is returned. @@ -92100,9 +92103,12 @@ function extractValue(obj, keys) { if (keys.length > 1 && value !== undefined) { return extractValue(value, keys.slice(1)); } - else { + else if (isString(value)) { return value; } + else { + return; + } } else { return; @@ -92122,19 +92128,26 @@ function getVersionInputFromTomlFile(versionFile) { // Normalize the line endings in the pyprojectFile pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n'); const pyprojectConfig = toml.parse(pyprojectFile); - let keys = []; + let keyPaths = []; if ('project' in pyprojectConfig) { // standard project metadata (PEP 621) - keys = ['project', 'requires-python']; + keyPaths = [['project', 'requires-python']]; } else { - // python poetry - keys = ['tool', 'poetry', 'dependencies', 'python']; + keyPaths = [ + // python poetry + ['tool', 'poetry', 'dependencies', 'python'], + // mise + ['tools', 'python'], + ['tools', 'python', 'version'] + ]; } const versions = []; - const version = extractValue(pyprojectConfig, keys); - if (version !== undefined) { - versions.push(version); + for (const keys of keyPaths) { + const value = extractValue(pyprojectConfig, keys); + if (value !== undefined) { + versions.push(value); + } } core.info(`Extracted ${versions} from ${versionFile}`); const rawVersions = Array.from(versions, version => version.split(',').join(' ')); diff --git a/src/utils.ts b/src/utils.ts index f3aa1ef..16136e4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -196,6 +196,10 @@ export async function getOSInfo() { } } +function isString(value: unknown): value is string { + return typeof value === 'string' || value instanceof String; +} + /** * Extract a value from an object by following the keys path provided. * If the value is present, it is returned. Otherwise undefined is returned. @@ -205,8 +209,10 @@ function extractValue(obj: any, keys: string[]): string | undefined { const value = obj[keys[0]]; if (keys.length > 1 && value !== undefined) { return extractValue(value, keys.slice(1)); - } else { + } else if (isString(value)) { return value; + } else { + return; } } else { return; @@ -229,19 +235,29 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] { pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n'); const pyprojectConfig = toml.parse(pyprojectFile); - let keys = []; + + let keyPaths = []; if ('project' in pyprojectConfig) { // standard project metadata (PEP 621) - keys = ['project', 'requires-python']; + keyPaths = [['project', 'requires-python']]; } else { - // python poetry - keys = ['tool', 'poetry', 'dependencies', 'python']; + keyPaths = [ + // python poetry + ['tool', 'poetry', 'dependencies', 'python'], + // mise + ['tools', 'python'], + ['tools', 'python', 'version'] + ]; } + const versions = []; - const version = extractValue(pyprojectConfig, keys); - if (version !== undefined) { - versions.push(version); + + for (const keys of keyPaths) { + const value = extractValue(pyprojectConfig, keys); + if (value !== undefined) { + versions.push(value); + } } core.info(`Extracted ${versions} from ${versionFile}`);