1
0
mirror of https://github.com/actions/setup-python synced 2024-11-30 21:22:28 +08:00
This commit is contained in:
Daniel Bowring 2024-10-07 12:30:25 -04:00 committed by GitHub
commit e979013902
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 69 additions and 16 deletions

View File

@ -128,6 +128,30 @@ describe('Version from file test', () => {
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]); expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
} }
); );
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
'Version from mise .mise.toml test',
async _fn => {
await io.mkdirP(tempDir);
const pythonVersionFileName = '.mise.toml';
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
const pythonVersion = '3.7.0';
const pythonVersionFileContent = `[tools]\npython = "${pythonVersion}"`;
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
}
);
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
'Version from mise verbose .mise.toml test',
async _fn => {
await io.mkdirP(tempDir);
const pythonVersionFileName = '.mise.toml';
const pythonVersionFilePath = path.join(tempDir, pythonVersionFileName);
const pythonVersion = '3.7.0';
const pythonVersionFileContent = `[tools]\npython = { version="${pythonVersion}", virtualenv=".venv" }`;
fs.writeFileSync(pythonVersionFilePath, pythonVersionFileContent);
expect(_fn(pythonVersionFilePath)).toEqual([pythonVersion]);
}
);
it.each([getVersionInputFromTomlFile, getVersionInputFromFile])( it.each([getVersionInputFromTomlFile, getVersionInputFromFile])(
'Version undefined', 'Version undefined',
async _fn => { async _fn => {

29
dist/setup/index.js vendored
View File

@ -92090,6 +92090,9 @@ function getOSInfo() {
}); });
} }
exports.getOSInfo = getOSInfo; exports.getOSInfo = getOSInfo;
function isString(value) {
return typeof value === 'string' || value instanceof String;
}
/** /**
* Extract a value from an object by following the keys path provided. * Extract a value from an object by following the keys path provided.
* If the value is present, it is returned. Otherwise undefined is returned. * If the value is present, it is returned. Otherwise undefined is returned.
@ -92100,9 +92103,12 @@ function extractValue(obj, keys) {
if (keys.length > 1 && value !== undefined) { if (keys.length > 1 && value !== undefined) {
return extractValue(value, keys.slice(1)); return extractValue(value, keys.slice(1));
} }
else { else if (isString(value)) {
return value; return value;
} }
else {
return;
}
} }
else { else {
return; return;
@ -92122,19 +92128,26 @@ function getVersionInputFromTomlFile(versionFile) {
// Normalize the line endings in the pyprojectFile // Normalize the line endings in the pyprojectFile
pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n'); pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n');
const pyprojectConfig = toml.parse(pyprojectFile); const pyprojectConfig = toml.parse(pyprojectFile);
let keys = []; let keyPaths = [];
if ('project' in pyprojectConfig) { if ('project' in pyprojectConfig) {
// standard project metadata (PEP 621) // standard project metadata (PEP 621)
keys = ['project', 'requires-python']; keyPaths = [['project', 'requires-python']];
} }
else { else {
// python poetry keyPaths = [
keys = ['tool', 'poetry', 'dependencies', 'python']; // python poetry
['tool', 'poetry', 'dependencies', 'python'],
// mise
['tools', 'python'],
['tools', 'python', 'version']
];
} }
const versions = []; const versions = [];
const version = extractValue(pyprojectConfig, keys); for (const keys of keyPaths) {
if (version !== undefined) { const value = extractValue(pyprojectConfig, keys);
versions.push(version); if (value !== undefined) {
versions.push(value);
}
} }
core.info(`Extracted ${versions} from ${versionFile}`); core.info(`Extracted ${versions} from ${versionFile}`);
const rawVersions = Array.from(versions, version => version.split(',').join(' ')); const rawVersions = Array.from(versions, version => version.split(',').join(' '));

View File

@ -196,6 +196,10 @@ export async function getOSInfo() {
} }
} }
function isString(value: unknown): value is string {
return typeof value === 'string' || value instanceof String;
}
/** /**
* Extract a value from an object by following the keys path provided. * Extract a value from an object by following the keys path provided.
* If the value is present, it is returned. Otherwise undefined is returned. * If the value is present, it is returned. Otherwise undefined is returned.
@ -205,8 +209,10 @@ function extractValue(obj: any, keys: string[]): string | undefined {
const value = obj[keys[0]]; const value = obj[keys[0]];
if (keys.length > 1 && value !== undefined) { if (keys.length > 1 && value !== undefined) {
return extractValue(value, keys.slice(1)); return extractValue(value, keys.slice(1));
} else { } else if (isString(value)) {
return value; return value;
} else {
return;
} }
} else { } else {
return; return;
@ -229,19 +235,29 @@ export function getVersionInputFromTomlFile(versionFile: string): string[] {
pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n'); pyprojectFile = pyprojectFile.replace(/\r\n/g, '\n');
const pyprojectConfig = toml.parse(pyprojectFile); const pyprojectConfig = toml.parse(pyprojectFile);
let keys = [];
let keyPaths = [];
if ('project' in pyprojectConfig) { if ('project' in pyprojectConfig) {
// standard project metadata (PEP 621) // standard project metadata (PEP 621)
keys = ['project', 'requires-python']; keyPaths = [['project', 'requires-python']];
} else { } else {
// python poetry keyPaths = [
keys = ['tool', 'poetry', 'dependencies', 'python']; // python poetry
['tool', 'poetry', 'dependencies', 'python'],
// mise
['tools', 'python'],
['tools', 'python', 'version']
];
} }
const versions = []; const versions = [];
const version = extractValue(pyprojectConfig, keys);
if (version !== undefined) { for (const keys of keyPaths) {
versions.push(version); const value = extractValue(pyprojectConfig, keys);
if (value !== undefined) {
versions.push(value);
}
} }
core.info(`Extracted ${versions} from ${versionFile}`); core.info(`Extracted ${versions} from ${versionFile}`);