Skip to content

Commit 08f73d8

Browse files
committed
Support reading python version from mise config
1 parent 9a7ac94 commit 08f73d8

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
@@ -126,6 +126,30 @@ describe('Version from file test', () => {
126126
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
127127
}
128128
);
129+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
130+
'Version from mise .mise.toml test',
131+
async _fn => {
132+
await io.mkdirP(tempDir);
133+
const pythonVersionFileName = '.mise.toml';
134+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
135+
const pythonVersion = '3.7.0';
136+
const pythonVersionFileContent = `[tools]\npython = "${pythonVersion}"`;
137+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
138+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
139+
}
140+
);
141+
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
142+
'Version from mise verbose .mise.toml test',
143+
async _fn => {
144+
await io.mkdirP(tempDir);
145+
const pythonVersionFileName = '.mise.toml';
146+
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
147+
const pythonVersion = '3.7.0';
148+
const pythonVersionFileContent = `[tools]\npython = { version="${pythonVersion}", virtualenv=".venv" }`;
149+
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
150+
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
151+
}
152+
);
129153
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
130154
'Version undefined',
131155
async _fn => {

dist/setup/index.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91829,6 +91829,9 @@ function getOSInfo() {
9182991829
});
9183091830
}
9183191831
exports.getOSInfo = getOSInfo;
91832+
function isString(value) {
91833+
return typeof value === 'string' || value instanceof String;
91834+
}
9183291835
/**
9183391836
* Extract a value from an object by following the keys path provided.
9183491837
* If the value is present, it is returned. Otherwise undefined is returned.
@@ -91839,9 +91842,12 @@ function extractValue(obj, keys) {
9183991842
if (keys.length > 1 && value !== undefined) {
9184091843
return extractValue(value, keys.slice(1));
9184191844
}
91842-
else {
91845+
else if (isString(value)) {
9184391846
return value;
9184491847
}
91848+
else {
91849+
return;
91850+
}
9184591851
}
9184691852
else {
9184791853
return;
@@ -91859,19 +91865,26 @@ function getVersionInputFromTomlFile(versionFile) {
9185991865
core.debug(`Trying to resolve version form ${versionFile}`);
9186091866
const pyprojectFile = fs_1.default.readFileSync(versionFile, 'utf8');
9186191867
const pyprojectConfig = toml.parse(pyprojectFile);
91862-
let keys = [];
91868+
let keyPaths = [];
9186391869
if ('project' in pyprojectConfig) {
9186491870
// standard project metadata (PEP 621)
91865-
keys = ['project', 'requires-python'];
91871+
keyPaths = [['project', 'requires-python']];
9186691872
}
9186791873
else {
91868-
// python poetry
91869-
keys = ['tool', 'poetry', 'dependencies', 'python'];
91874+
keyPaths = [
91875+
// python poetry
91876+
['tool', 'poetry', 'dependencies', 'python'],
91877+
// mise
91878+
['tools', 'python'],
91879+
['tools', 'python', 'version']
91880+
];
9187091881
}
9187191882
const versions = [];
91872-
const version = extractValue(pyprojectConfig, keys);
91873-
if (version !== undefined) {
91874-
versions.push(version);
91883+
for (const keys of keyPaths) {
91884+
const value = extractValue(pyprojectConfig, keys);
91885+
if (value !== undefined) {
91886+
versions.push(value);
91887+
}
9187591888
}
9187691889
core.info(`Extracted ${versions} from ${versionFile}`);
9187791890
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;
@@ -226,19 +232,29 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {
226232

227233
const pyprojectFile = fs.readFileSync(versionFile, 'utf8');
228234
const pyprojectConfig = toml.parse(pyprojectFile);
229-
let keys = [];
235+
236+
let keyPaths = [];
230237

231238
if ('project' in pyprojectConfig) {
232239
// standard project metadata (PEP 621)
233-
keys = ['project', 'requires-python'];
240+
keyPaths = [['project', 'requires-python']];
234241
} else {
235-
// python poetry
236-
keys = ['tool', 'poetry', 'dependencies', 'python'];
242+
keyPaths = [
243+
// python poetry
244+
['tool', 'poetry', 'dependencies', 'python'],
245+
// mise
246+
['tools', 'python'],
247+
['tools', 'python', 'version']
248+
];
237249
}
250+
238251
const versions = [];
239-
const version = extractValue(pyprojectConfig, keys);
240-
if (version !== undefined) {
241-
versions.push(version);
252+
253+
for (const keys of keyPaths) {
254+
const value = extractValue(pyprojectConfig, keys);
255+
if (value !== undefined) {
256+
versions.push(value);
257+
}
242258
}
243259

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

0 commit comments

Comments
 (0)