Skip to content

Commit

Permalink
Add support for mise version array
Browse files Browse the repository at this point in the history
  • Loading branch information
dbowring committed Sep 10, 2024
1 parent 9e32ee0 commit abff239
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
26 changes: 26 additions & 0 deletions __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand Down
21 changes: 16 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit abff239

Please sign in to comment.