diff --git a/spec/update-process-env-spec.js b/spec/update-process-env-spec.js index 41c80c842..403e8fa7d 100644 --- a/spec/update-process-env-spec.js +++ b/spec/update-process-env-spec.js @@ -2,7 +2,7 @@ /* eslint-env jasmine */ import child_process from 'child_process' -import {updateProcessEnv, shellShouldBePatched} from '../src/update-process-env' +import {updateProcessEnv, envShouldBePatched} from '../src/update-process-env' import dedent from 'dedent' describe('updateProcessEnv(launchEnv)', function () { @@ -86,38 +86,30 @@ describe('updateProcessEnv(launchEnv)', function () { }) describe('shells on osx', function () { - it('shellShouldBePatched() returns the shell when the shell should be patched', function () { + it('envShouldBePatched() returns the shell when the shell should be patched', function () { process.platform = 'darwin' - let shellsToTest = new Set([ - '/bin/sh', - '/usr/local/bin/sh', - '/bin/bash', - '/usr/local/bin/bash', - '/bin/zsh', - '/usr/local/bin/zsh', - '/bin/fish', - '/usr/local/bin/fish' - ]) - for (let shell of shellsToTest) { - process.env.SHELL = shell - expect(shellShouldBePatched()).toBe(shell) - } + expect(envShouldBePatched('/bin/sh')).toBe(true) + expect(envShouldBePatched('/usr/local/bin/sh')).toBe(true) + expect(envShouldBePatched('/bin/bash')).toBe(true) + expect(envShouldBePatched('/usr/local/bin/bash')).toBe(true) + expect(envShouldBePatched('/bin/zsh')).toBe(true) + expect(envShouldBePatched('/usr/local/bin/zsh')).toBe(true) + expect(envShouldBePatched('/bin/fish')).toBe(true) + expect(envShouldBePatched('/usr/local/bin/fish')).toBe(true) }) - it('shellShouldBePatched() returns false when the shell should not be patched', function () { + it('envShouldBePatched() returns false when the shell should not be patched', function () { process.platform = 'darwin' - let shellsToTest = new Set([ - '/bin/unsupported', - '/bin/shh', - '/bin/tcsh', - '/usr/csh' - ]) - for (let shell of shellsToTest) { - process.env.SHELL = shell - let result = shellShouldBePatched() - console.log(result) - expect(result).toBe(false) - } + expect(envShouldBePatched('/bin/unsupported')).toBe(false) + expect(envShouldBePatched('/bin/shh')).toBe(false) + expect(envShouldBePatched('/bin/tcsh')).toBe(false) + expect(envShouldBePatched('/usr/csh')).toBe(false) + }) + + it('envShouldBePatched() returns false when the shell is undefined or empty', function () { + process.platform = 'darwin' + expect(envShouldBePatched(undefined)).toBe(false) + expect(envShouldBePatched('')).toBe(false) }) }) }) diff --git a/src/update-process-env.js b/src/update-process-env.js index dd9ec6299..565d566d9 100644 --- a/src/update-process-env.js +++ b/src/update-process-env.js @@ -8,7 +8,7 @@ const ENVIRONMENT_VARIABLES_TO_PRESERVE = new Set([ 'ATOM_HOME' ]) -const OSX_SHELLS_TO_PATCH = new Set([ +const OSX_SHELLS = new Set([ '/sh', '/bash', '/zsh', @@ -40,14 +40,13 @@ function updateProcessEnv (launchEnv) { } } -function shellShouldBePatched () { - let shell = process.env.SHELL - if (!shell) { +function envShouldBePatched (shell) { + if (!shell || shell.trim() === '') { return false } - for (let s of OSX_SHELLS_TO_PATCH) { + for (let s of OSX_SHELLS) { if (shell.endsWith(s)) { - return shell + return true } } @@ -55,22 +54,24 @@ function shellShouldBePatched () { } function getEnvFromShell () { - let shell = shellShouldBePatched() - if (shell) { - let {stdout} = spawnSync(shell, ['-ilc', 'command env'], {encoding: 'utf8'}) - if (stdout) { - let result = {} - for (let line of stdout.split('\n')) { - if (line.includes('=')) { - let components = line.split('=') - let key = components.shift() - let value = components.join('=') - result[key] = value - } + let shell = process.env.SHELL + if (!envShouldBePatched(shell)) { + return + } + + let {stdout} = spawnSync(shell, ['-ilc', 'command env'], {encoding: 'utf8'}) + if (stdout) { + let result = {} + for (let line of stdout.split('\n')) { + if (line.includes('=')) { + let components = line.split('=') + let key = components.shift() + let value = components.join('=') + result[key] = value } - return result } + return result } } -export default { updateProcessEnv, shellShouldBePatched } +export default { updateProcessEnv, envShouldBePatched }