From abff2393e5a376b7fc6ddbb655bf099b151fd41c Mon Sep 17 00:00:00 2001 From: Daniel Bowring Date: Tue, 10 Sep 2024 12:16:21 +1000 Subject: [PATCH] Add support for mise version array --- __tests__/utils.test.ts | 26 ++++++++++++++++++++++++++ src/utils.ts | 21 ++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 77e056496..ce975685f 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -140,6 +140,19 @@ describe('Version from file test', () => { expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); } ); + it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( + 'Version from mise .mise.toml array test', + async _fn => { + await io.mkdirP(tempDir); + const pythonVersionFileName = '.mise.toml'; + const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName); + const pythonVersion = '3.7.0'; + const otherPythonVersion = '3.6.0'; + const pythonVersionFileContent = `[tools]\npython = ["${pythonVersion}", "${otherPythonVersion}"]`; + fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent); + expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); + } + ); it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( 'Version from mise verbose .mise.toml test', async _fn => { @@ -152,6 +165,19 @@ describe('Version from file test', () => { expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); } ); + it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( + 'Version from mise verbose .mise.toml array test', + async _fn => { + await io.mkdirP(tempDir); + const pythonVersionFileName = '.mise.toml'; + const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName); + const pythonVersion = '3.7.0'; + const otherPythonVersion = '3.6.0'; + const pythonVersionFileContent = `[tools]\npython = [{ version="${pythonVersion}", virtualenv=".venv" }, { version="${otherPythonVersion}", virtualenv=".venv2" }]`; + fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent); + expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); + } + ); it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( 'Version undefined', async _fn => { diff --git a/src/utils.ts b/src/utils.ts index 16136e493..1bab779b3 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -205,17 +205,28 @@ function isString(value: unknown): value is string { * If the value is present, it is returned. Otherwise undefined is returned. */ function extractValue(obj: any, keys: string[]): string | undefined { + if (obj === null || obj === undefined) { + return; + } + if (keys.length > 0) { + if (Array.isArray(obj)) { + const first = obj[0]; + if (isString(first)) { + return first; + } + return extractValue(first, keys); + } + const value = obj[keys[0]]; + if (keys.length > 1 && value !== undefined) { return extractValue(value, keys.slice(1)); - } else if (isString(value)) { + } + + if (isString(value)) { return value; - } else { - return; } - } else { - return; } }