Skip to content

Commit abff239

Browse files
committed
Add support for mise version array
1 parent 9e32ee0 commit abff239

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

__tests__/utils.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,19 @@ describe('Version from file test', () => {
140140
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
141141
}
142142
);
143+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
144+
'Version from mise .mise.toml array test',
145+
async _fn => {
146+
await io.mkdirP(tempDir);
147+
const pythonVersionFileName = '.mise.toml';
148+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
149+
const pythonVersion = '3.7.0';
150+
const otherPythonVersion = '3.6.0';
151+
const pythonVersionFileContent = `[tools]\npython = ["${pythonVersion}", "${otherPythonVersion}"]`;
152+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
153+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
154+
}
155+
);
143156
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
144157
'Version from mise verbose .mise.toml test',
145158
async _fn => {
@@ -152,6 +165,19 @@ describe('Version from file test', () => {
152165
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
153166
}
154167
);
168+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
169+
'Version from mise verbose .mise.toml array test',
170+
async _fn => {
171+
await io.mkdirP(tempDir);
172+
const pythonVersionFileName = '.mise.toml';
173+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
174+
const pythonVersion = '3.7.0';
175+
const otherPythonVersion = '3.6.0';
176+
const pythonVersionFileContent = `[tools]\npython = [{ version="${pythonVersion}", virtualenv=".venv" }, { version="${otherPythonVersion}", virtualenv=".venv2" }]`;
177+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
178+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
179+
}
180+
);
155181
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
156182
'Version undefined',
157183
async _fn => {

src/utils.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,28 @@ function isString(value: unknown): value is string {
205205
* If the value is present, it is returned. Otherwise undefined is returned.
206206
*/
207207
function extractValue(obj: any, keys: string[]): string | undefined {
208+
if (obj === null || obj === undefined) {
209+
return;
210+
}
211+
208212
if (keys.length > 0) {
213+
if (Array.isArray(obj)) {
214+
const first = obj[0];
215+
if (isString(first)) {
216+
return first;
217+
}
218+
return extractValue(first, keys);
219+
}
220+
209221
const value = obj[keys[0]];
222+
210223
if (keys.length > 1 && value !== undefined) {
211224
return extractValue(value, keys.slice(1));
212-
} else if (isString(value)) {
225+
}
226+
227+
if (isString(value)) {
213228
return value;
214-
} else {
215-
return;
216229
}
217-
} else {
218-
return;
219230
}
220231
}
221232

0 commit comments

Comments
 (0)