From 3a78503769da6d3a11f9b295c4bb1df73c86de62 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 | 12 ++++++++++++ dist/setup/index.js | 20 +++++++++++++------- src/utils.ts | 23 ++++++++++++++++------- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index f5e364788..ff629950b 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -126,6 +126,18 @@ 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 undefined', async _fn => { diff --git a/dist/setup/index.js b/dist/setup/index.js index f3040a80a..2d4dc902e 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -91859,19 +91859,25 @@ function getVersionInputFromTomlFile(versionFile) { core.debug(`Trying to resolve version form ${versionFile}`); const pyprojectFile = fs_1.default.readFileSync(versionFile, 'utf8'); 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'] + ]; } 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 644b4af5f..7c5c63458 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -226,19 +226,28 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] { const pyprojectFile = fs.readFileSync(versionFile, 'utf8'); 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'] + ]; } + 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}`);