From 8294536799aa008727af69133fcf52030c7fed89 Mon Sep 17 00:00:00 2001 From: Kohei Watanabe Date: Tue, 28 May 2024 09:36:32 +0900 Subject: [PATCH] feat: integrated utils.condensePath into utils.which fixed #151 Removed `utils.condensePath` and updated all references to use the enhanced `utils.which`, which now replaces the home directory with ~. Updated and added tests to ensure functionality. --- __tests__/utils.test.js | 28 ++++++++++++++++++++++++++++ src/helpers/binaries.js | 20 +++++++++----------- src/helpers/ides.js | 8 ++++---- src/helpers/languages.js | 12 ++++++------ src/helpers/managers.js | 12 ++++++------ src/helpers/servers.js | 4 ++-- src/helpers/utilities.js | 16 ++++++++-------- src/utils.js | 14 +++++++++----- 8 files changed, 72 insertions(+), 42 deletions(-) diff --git a/__tests__/utils.test.js b/__tests__/utils.test.js index c823e6cd..c362f1e6 100644 --- a/__tests__/utils.test.js +++ b/__tests__/utils.test.js @@ -1,3 +1,9 @@ +const os = require('os'); +const libWhich = require('which'); + +jest.mock('os'); +jest.mock('which'); + const utils = require('../src/utils'); const cases = { apt: { @@ -234,3 +240,25 @@ describe('pick', () => { expect(utils.pick({ one: true, two: true }, ['two'])).toEqual({ two: true }); }); }); + +describe('which', () => { + const homedir = '/home/testuser'; + + beforeAll(() => os.homedir.mockReturnValue(homedir)); + afterAll(() => os.homedir.mockRestore()); + afterEach(() => libWhich.mockRestore()); + + test('should not modify paths not in homedir', async () => { + const path = '/usr/local/bin/node'; + libWhich.mockImplementation((_, cb) => cb(null, path)); + const expected = '/usr/local/bin/node'; + expect(await utils.which()).toBe(expected); + }); + + test('should replace homedir with ~', async () => { + const path = `${homedir}/.local/bin/node`; + libWhich.mockImplementation((_, cb) => cb(null, path)); + const expected = '~/.local/bin/node'; + expect(await utils.which()).toBe(expected); + }); +}); diff --git a/src/helpers/binaries.js b/src/helpers/binaries.js index 57fc7b99..3206d3de 100644 --- a/src/helpers/binaries.js +++ b/src/helpers/binaries.js @@ -10,13 +10,13 @@ module.exports = { .which('node') .then(nodePath => (nodePath ? utils.run(nodePath + ' -v') : Promise.resolve(''))) .then(utils.findVersion), - utils.which('node').then(utils.condensePath), + utils.which('node'), ]).then(v => utils.determineFound('Node', v[0], v[1])); }, getnpmInfo: () => { utils.log('trace', 'getnpmInfo'); - return Promise.all([utils.run('npm -v'), utils.which('npm').then(utils.condensePath)]).then(v => + return Promise.all([utils.run('npm -v'), utils.which('npm')]).then(v => utils.determineFound('npm', v[0], v[1]) ); }, @@ -33,23 +33,21 @@ module.exports = { getYarnInfo: () => { utils.log('trace', 'getYarnInfo'); - return Promise.all([ - utils.run('yarn -v'), - utils.which('yarn').then(utils.condensePath), - ]).then(v => utils.determineFound('Yarn', v[0], v[1])); + return Promise.all([utils.run('yarn -v'), utils.which('yarn')]).then(v => + utils.determineFound('Yarn', v[0], v[1]) + ); }, getpnpmInfo: () => { utils.log('trace', 'getpnpmInfo'); - return Promise.all([ - utils.run('pnpm -v'), - utils.which('pnpm').then(utils.condensePath), - ]).then(v => utils.determineFound('pnpm', v[0], v[1])); + return Promise.all([utils.run('pnpm -v'), utils.which('pnpm')]).then(v => + utils.determineFound('pnpm', v[0], v[1]) + ); }, getbunInfo: () => { utils.log('trace', 'getbunInfo'); - return Promise.all([utils.run('bun -v'), utils.which('bun').then(utils.condensePath)]).then(v => + return Promise.all([utils.run('bun -v'), utils.which('bun')]).then(v => utils.determineFound('bun', v[0], v[1]) ); }, diff --git a/src/helpers/ides.js b/src/helpers/ides.js index 28a6c314..f90ae55e 100644 --- a/src/helpers/ides.js +++ b/src/helpers/ides.js @@ -86,7 +86,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('emacs --version').then(utils.findVersion), - utils.run('which emacs'), + utils.which('emacs'), ]).then(v => utils.determineFound('Emacs', v[0], v[1])); } return Promise.resolve(['Emacs', 'N/A']); @@ -104,7 +104,7 @@ module.exports = { if (utils.isLinux) { return Promise.all([ utils.run('nano --version').then(utils.findVersion), - utils.run('which nano'), + utils.which('nano'), ]).then(v => utils.determineFound('Nano', v[0], v[1])); } return Promise.resolve(['Nano', 'N/A']); @@ -115,7 +115,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('nvim --version').then(utils.findVersion), - utils.run('which nvim'), + utils.which('nvim'), ]).then(v => utils.determineFound('Nvim', v[0], v[1])); } return Promise.resolve(['Vim', 'N/A']); @@ -152,7 +152,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('vim --version').then(utils.findVersion), - utils.run('which vim'), + utils.which('vim'), ]).then(v => utils.determineFound('Vim', v[0], v[1])); } return Promise.resolve(['Vim', 'N/A']); diff --git a/src/helpers/languages.js b/src/helpers/languages.js index fb1445f5..9774b39e 100644 --- a/src/helpers/languages.js +++ b/src/helpers/languages.js @@ -46,7 +46,7 @@ module.exports = { utils .run('javac -version', { unify: true }) .then(v => utils.findVersion(v, /\d+\.[\w+|.|_|-]+/)), - utils.run('which javac'), + utils.which('javac'), ]).then(v => utils.determineFound('Java', v[0], v[1])); }, @@ -69,7 +69,7 @@ module.exports = { utils.log('trace', 'getProtocInfo'); return Promise.all([ utils.run('protoc --version').then(utils.findVersion), - utils.run('which protoc'), + utils.which('protoc'), ]).then(v => utils.determineFound('Protoc', v[0], v[1])); }, @@ -77,7 +77,7 @@ module.exports = { utils.log('trace', 'getPythonInfo'); return Promise.all([ utils.run('python -V 2>&1').then(utils.findVersion), - utils.run('which python'), + utils.which('python'), ]).then(v => utils.determineFound('Python', v[0], v[1])); }, @@ -85,7 +85,7 @@ module.exports = { utils.log('trace', 'getPython3Info'); return Promise.all([ utils.run('python3 -V 2>&1').then(utils.findVersion), - utils.run('which python3'), + utils.which('python3'), ]).then(v => utils.determineFound('Python3', v[0], v[1])); }, @@ -109,7 +109,7 @@ module.exports = { utils.log('trace', 'getRustInfo'); return Promise.all([ utils.run('rustc --version').then(utils.findVersion), - utils.run('which rustc'), + utils.which('rustc'), ]).then(v => utils.determineFound('Rust', v[0], v[1])); }, @@ -118,7 +118,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('scalac -version').then(utils.findVersion), - utils.run('which scalac'), + utils.which('scalac'), ]).then(v => utils.determineFound('Scala', v[0], v[1])); } return Promise.resolve(['Scala', 'N/A']); diff --git a/src/helpers/managers.js b/src/helpers/managers.js index 33b7b0f7..f59a46b2 100644 --- a/src/helpers/managers.js +++ b/src/helpers/managers.js @@ -15,7 +15,7 @@ module.exports = { utils.log('trace', 'getCargoInfo'); return Promise.all([ utils.run('cargo --version').then(utils.findVersion), - utils.which('cargo').then(utils.condensePath), + utils.which('cargo'), ]).then(v => utils.determineFound('Cargo', v[0], v[1])); }, @@ -33,7 +33,7 @@ module.exports = { utils.log('trace', 'getComposerInfo'); return Promise.all([ utils.run('composer --version').then(utils.findVersion), - utils.which('composer').then(utils.condensePath), + utils.which('composer'), ]).then(v => utils.determineFound('Composer', v[0], v[1])); }, @@ -41,7 +41,7 @@ module.exports = { utils.log('trace', 'getGradleInfo'); return Promise.all([ utils.run('gradle --version').then(utils.findVersion), - utils.which('gradle').then(utils.condensePath), + utils.which('gradle'), ]).then(v => utils.determineFound('Gradle', v[0], v[1])); }, @@ -59,7 +59,7 @@ module.exports = { utils.log('trace', 'getMavenInfo'); return Promise.all([ utils.run('mvn --version').then(utils.findVersion), - utils.which('mvn').then(utils.condensePath), + utils.which('mvn'), ]).then(v => utils.determineFound('Maven', v[0], v[1])); }, @@ -67,7 +67,7 @@ module.exports = { utils.log('trace', 'getpip2Info'); return Promise.all([ utils.run('pip2 --version').then(utils.findVersion), - utils.which('pip2').then(utils.condensePath), + utils.which('pip2'), ]).then(v => utils.determineFound('pip2', v[0], v[1])); }, @@ -75,7 +75,7 @@ module.exports = { utils.log('trace', 'getpip3Info'); return Promise.all([ utils.run('pip3 --version').then(utils.findVersion), - utils.which('pip3').then(utils.condensePath), + utils.which('pip3'), ]).then(v => utils.determineFound('pip3', v[0], v[1])); }, diff --git a/src/helpers/servers.js b/src/helpers/servers.js index a4ef4226..6c5f0e7d 100644 --- a/src/helpers/servers.js +++ b/src/helpers/servers.js @@ -6,7 +6,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('apachectl -v').then(utils.findVersion), - utils.run('which apachectl'), + utils.which('apachectl'), ]).then(v => utils.determineFound('Apache', v[0], v[1])); } return Promise.resolve(['Apache', 'N/A']); @@ -17,7 +17,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('nginx -v 2>&1').then(utils.findVersion), - utils.run('which nginx'), + utils.which('nginx'), ]).then(v => utils.determineFound('Nginx', v[0], v[1])); } return Promise.resolve(['Nginx', 'N/A']); diff --git a/src/helpers/utilities.js b/src/helpers/utilities.js index 16c6fbca..fa81c524 100644 --- a/src/helpers/utilities.js +++ b/src/helpers/utilities.js @@ -5,7 +5,7 @@ module.exports = { utils.log('trace', 'getBazelInfo'); return Promise.all([ utils.run('bazel --version').then(utils.findVersion), - utils.run('which bazel'), + utils.which('bazel'), ]).then(v => utils.determineFound('Bazel', v[0], v[1])); }, @@ -13,7 +13,7 @@ module.exports = { utils.log('trace', 'getCMakeInfo'); return Promise.all([ utils.run('cmake --version').then(utils.findVersion), - utils.run('which cmake'), + utils.which('cmake'), ]).then(v => utils.determineFound('CMake', v[0], v[1])); }, @@ -22,7 +22,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('gcc -v 2>&1').then(utils.findVersion), - utils.run('which gcc'), + utils.which('gcc'), ]).then(v => utils.determineFound('GCC', v[0], v[1])); } return Promise.resolve(['GCC', 'N/A']); @@ -40,7 +40,7 @@ module.exports = { utils.log('trace', 'getGitInfo'); return Promise.all([ utils.run('git --version').then(utils.findVersion), - utils.run('which git'), + utils.which('git'), ]).then(v => utils.determineFound('Git', v[0], v[1])); }, @@ -49,7 +49,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('make --version').then(utils.findVersion), - utils.run('which make'), + utils.which('make'), ]).then(v => utils.determineFound('Make', v[0], v[1])); } return Promise.resolve(['Make', 'N/A']); @@ -59,7 +59,7 @@ module.exports = { utils.log('trace', 'getNinjaInfo'); return Promise.all([ utils.run('ninja --version').then(utils.findVersion), - utils.run('which ninja'), + utils.which('ninja'), ]).then(v => utils.determineFound('Ninja', v[0], v[1])); }, @@ -68,7 +68,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('hg --version').then(utils.findVersion), - utils.run('which hg'), + utils.which('hg'), ]).then(v => utils.determineFound('Mercurial', v[0], v[1])); } return Promise.resolve(['Mercurial', 'N/A']); @@ -79,7 +79,7 @@ module.exports = { if (utils.isMacOS || utils.isLinux) { return Promise.all([ utils.run('svn --version').then(utils.findVersion), - utils.run('which svn'), + utils.which('svn'), ]).then(v => utils.determineFound('Subversion', v[0], v[1])); } return Promise.resolve(['Subversion', 'N/A']); diff --git a/src/utils.js b/src/utils.js index 077b3f11..6f589ef0 100644 --- a/src/utils.js +++ b/src/utils.js @@ -113,6 +113,10 @@ const parseSDKManagerOutput = output => { }; }; +const condensePath = pathString => { + return (pathString || '').replace(os.homedir(), '~'); +}; + module.exports = { run: run, log: log, @@ -167,7 +171,11 @@ module.exports = { }, which: binary => { - return new Promise(resolve => libWhich(binary, (err, binaryPath) => resolve(binaryPath))); + return new Promise(resolve => + libWhich(binary, (err, binaryPath) => { + resolve(condensePath(binaryPath)); + }) + ); }, getDarwinApplicationVersion: bundleIdentifier => { @@ -250,10 +258,6 @@ module.exports = { return matched ? matched[idx] : versionString; }, - condensePath: pathString => { - return (pathString || '').replace(os.homedir(), '~'); - }, - determineFound: (name, version, appPath) => { log('trace', 'determineFound', name, version, appPath); if (version === 'N/A') {