Skip to content

Commit 9e32ee0

Browse files
committed
Support reading python version from mise config
1 parent 29a37be commit 9e32ee0

File tree

3 files changed

+69
-16
lines changed

3 files changed

+69
-16
lines changed

__tests__/utils.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ describe('Version from file test', () => {
128128
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
129129
}
130130
);
131+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
132+
'Version from mise .mise.toml test',
133+
async _fn => {
134+
await io.mkdirP(tempDir);
135+
const pythonVersionFileName = '.mise.toml';
136+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
137+
const pythonVersion = '3.7.0';
138+
const pythonVersionFileContent = `[tools]\npython = "${pythonVersion}"`;
139+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
140+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
141+
}
142+
);
143+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
144+
'Version from mise verbose .mise.toml 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 pythonVersionFileContent = `[tools]\npython = { version="${pythonVersion}", virtualenv=".venv" }`;
151+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
152+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
153+
}
154+
);
131155
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
132156
'Version undefined',
133157
async _fn => {

dist/setup/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92090,6 +92090,9 @@ function getOSInfo() {
9209092090
});
9209192091
}
9209292092
exports.getOSInfo = getOSInfo;
92093+
function isString(value) {
92094+
return typeof value === 'string' || value instanceof String;
92095+
}
9209392096
/**
9209492097
* Extract a value from an object by following the keys path provided.
9209592098
* If the value is present, it is returned. Otherwise undefined is returned.
@@ -92100,9 +92103,12 @@ function extractValue(obj, keys) {
9210092103
if (keys.length > 1 && value !== undefined) {
9210192104
return extractValue(value, keys.slice(1));
9210292105
}
92103-
else {
92106+
else if (isString(value)) {
9210492107
return value;
9210592108
}
92109+
else {
92110+
return;
92111+
}
9210692112
}
9210792113
else {
9210892114
return;
@@ -92122,19 +92128,26 @@ function getVersionInputFromTomlFile(versionFile) {
9212292128
// Normalize the line endings in the pyprojectFile
9212392129
pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n');
9212492130
const pyprojectConfig = toml.parse(pyprojectFile);
92125-
let keys = [];
92131+
let keyPaths = [];
9212692132
if ('project' in pyprojectConfig) {
9212792133
// standard project metadata (PEP 621)
92128-
keys = ['project', 'requires-python'];
92134+
keyPaths = [['project', 'requires-python']];
9212992135
}
9213092136
else {
92131-
// python poetry
92132-
keys = ['tool', 'poetry', 'dependencies', 'python'];
92137+
keyPaths = [
92138+
// python poetry
92139+
['tool', 'poetry', 'dependencies', 'python'],
92140+
// mise
92141+
['tools', 'python'],
92142+
['tools', 'python', 'version']
92143+
];
9213392144
}
9213492145
const versions = [];
92135-
const version = extractValue(pyprojectConfig, keys);
92136-
if (version !== undefined) {
92137-
versions.push(version);
92146+
for (const keys of keyPaths) {
92147+
const value = extractValue(pyprojectConfig, keys);
92148+
if (value !== undefined) {
92149+
versions.push(value);
92150+
}
9213892151
}
9213992152
core.info(`Extracted ${versions} from ${versionFile}`);
9214092153
const rawVersions = Array.from(versions, version => version.split(',').join(' '));

src/utils.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ export async function getOSInfo() {
196196
}
197197
}
198198

199+
function isString(value: unknown): value is string {
200+
return typeof value === 'string' || value instanceof String;
201+
}
202+
199203
/**
200204
* Extract a value from an object by following the keys path provided.
201205
* If the value is present, it is returned. Otherwise undefined is returned.
@@ -205,8 +209,10 @@ function extractValue(obj: any, keys: string[]): string | undefined {
205209
const value = obj[keys[0]];
206210
if (keys.length > 1 && value !== undefined) {
207211
return extractValue(value, keys.slice(1));
208-
} else {
212+
} else if (isString(value)) {
209213
return value;
214+
} else {
215+
return;
210216
}
211217
} else {
212218
return;
@@ -229,19 +235,29 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {
229235
pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n');
230236

231237
const pyprojectConfig = toml.parse(pyprojectFile);
232-
let keys = [];
238+
239+
let keyPaths = [];
233240

234241
if ('project' in pyprojectConfig) {
235242
// standard project metadata (PEP 621)
236-
keys = ['project', 'requires-python'];
243+
keyPaths = [['project', 'requires-python']];
237244
} else {
238-
// python poetry
239-
keys = ['tool', 'poetry', 'dependencies', 'python'];
245+
keyPaths = [
246+
// python poetry
247+
['tool', 'poetry', 'dependencies', 'python'],
248+
// mise
249+
['tools', 'python'],
250+
['tools', 'python', 'version']
251+
];
240252
}
253+
241254
const versions = [];
242-
const version = extractValue(pyprojectConfig, keys);
243-
if (version !== undefined) {
244-
versions.push(version);
255+
256+
for (const keys of keyPaths) {
257+
const value = extractValue(pyprojectConfig, keys);
258+
if (value !== undefined) {
259+
versions.push(value);
260+
}
245261
}
246262

247263
core.info(`Extracted ${versions} from ${versionFile}`);

0 commit comments

Comments
 (0)