From 5ff5ede41c6e48129f881e297e682923b48d1963 Mon Sep 17 00:00:00 2001 From: Rishabh Jain <mailrishabh1403@gmail.com> Date: Sun, 17 Mar 2019 20:24:31 +0530 Subject: [PATCH] fix: snapshot test failures on some systems (#3397) - The function is calling python command which can be aliased to different python versions on different systems, the function depends on mock library which comes pre-installed on python v3.7+ - Add a python alias detection command which gets the correct python command which is aliased to python3.7+ Closes #3392 --- lib/manager/pip_setup/extract.js | 32 +++++++++++++++++++++++++- test/manager/pip_setup/extract.spec.js | 14 +++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/lib/manager/pip_setup/extract.js b/lib/manager/pip_setup/extract.js index cc205f241b..93ce1aa903 100644 --- a/lib/manager/pip_setup/extract.js +++ b/lib/manager/pip_setup/extract.js @@ -4,11 +4,41 @@ const { join } = require('upath'); const { isSkipComment } = require('../../util/ignore'); const { dependencyPattern } = require('../pip_requirements/extract'); +const pythonVersions = ['python', 'python3', 'python3.7']; +let pythonAlias = null; module.exports = { extractPackageFile, extractSetupFile, + parsePythonVersion, + getPythonAlias, + pythonVersions, }; +function parsePythonVersion(str) { + const arr = str.split(' ')[1].split('.'); + return [parseInt(arr[0], 10), parseInt(arr[1], 10)]; +} + +async function getPythonAlias() { + if (pythonAlias) { + return pythonAlias; + } + pythonAlias = pythonVersions[0]; // fallback to 'python' + for (const pythonVersion of pythonVersions) { + try { + const { stdout, stderr } = await exec(`${pythonVersion} --version`); + const version = parsePythonVersion(stdout || stderr); + // istanbul ignore if + if (version[0] >= 3 && version[1] >= 7) { + pythonAlias = pythonVersion; + } + } catch (err) { + logger.debug(`${pythonVersion} alias not found`); + } + } + return pythonAlias; +} + async function extractSetupFile(content, packageFile, config) { const cwd = config.localDir; // extract.py needs setup.py to be written to disk @@ -40,7 +70,7 @@ async function extractSetupFile(content, packageFile, config) { ); } else { logger.info('Running python via global command'); - cmd = 'python'; + cmd = await getPythonAlias(); } logger.debug({ cmd, args }, 'python command'); let stdout; diff --git a/test/manager/pip_setup/extract.spec.js b/test/manager/pip_setup/extract.spec.js index 82e7593f5c..4e8b8945f4 100644 --- a/test/manager/pip_setup/extract.spec.js +++ b/test/manager/pip_setup/extract.spec.js @@ -3,6 +3,9 @@ const tmp = require('tmp-promise'); const { relative } = require('path'); const { extractPackageFile, + parsePythonVersion, + getPythonAlias, + pythonVersions, // extractSetupFile, } = require('../../../lib/manager/pip_setup/extract'); @@ -39,6 +42,17 @@ describe('lib/manager/pip_setup/extract', () => { ).toBe(null); }); }); + + describe('parsePythonVersion', () => { + it('returns major and minor version numbers', () => { + expect(parsePythonVersion('Python 2.7.15rc1')).toEqual([2, 7]); + }); + }); + describe('getPythonAlias', () => { + it('returns the python alias to use', async () => { + expect(pythonVersions.includes(await getPythonAlias())).toBeTruthy(); + }); + }); /* describe('extractSetupFile()', () => { it('should return parsed setup() call', async () => { -- GitLab