diff --git a/spec/update-process-env-spec.js b/spec/update-process-env-spec.js index 3501bfebf..39376b4c2 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 from '../src/update-process-env' +import {updateProcessEnv, shouldGetEnvFromShell} from '../src/update-process-env' import dedent from 'dedent' describe('updateProcessEnv(launchEnv)', function () { @@ -84,5 +84,33 @@ describe('updateProcessEnv(launchEnv)', function () { expect(process.env).toEqual({FOO: 'bar'}) }) }) + + describe('shouldGetEnvFromShell()', function () { + it('returns the shell when the shell should be patched', function () { + process.platform = 'darwin' + expect(shouldGetEnvFromShell('/bin/sh')).toBe(true) + expect(shouldGetEnvFromShell('/usr/local/bin/sh')).toBe(true) + expect(shouldGetEnvFromShell('/bin/bash')).toBe(true) + expect(shouldGetEnvFromShell('/usr/local/bin/bash')).toBe(true) + expect(shouldGetEnvFromShell('/bin/zsh')).toBe(true) + expect(shouldGetEnvFromShell('/usr/local/bin/zsh')).toBe(true) + expect(shouldGetEnvFromShell('/bin/fish')).toBe(true) + expect(shouldGetEnvFromShell('/usr/local/bin/fish')).toBe(true) + }) + + it('returns false when the shell should not be patched', function () { + process.platform = 'darwin' + expect(shouldGetEnvFromShell('/bin/unsupported')).toBe(false) + expect(shouldGetEnvFromShell('/bin/shh')).toBe(false) + expect(shouldGetEnvFromShell('/bin/tcsh')).toBe(false) + expect(shouldGetEnvFromShell('/usr/csh')).toBe(false) + }) + + it('returns false when the shell is undefined or empty', function () { + process.platform = 'darwin' + expect(shouldGetEnvFromShell(undefined)).toBe(false) + expect(shouldGetEnvFromShell('')).toBe(false) + }) + }) }) }) diff --git a/src/initialize-application-window.coffee b/src/initialize-application-window.coffee index f85207153..9787977e2 100644 --- a/src/initialize-application-window.coffee +++ b/src/initialize-application-window.coffee @@ -1,6 +1,6 @@ # Like sands through the hourglass, so are the days of our lives. module.exports = ({blobStore}) -> - updateProcessEnv = require('./update-process-env') + {updateProcessEnv} = require('./update-process-env') path = require 'path' require './window' {getWindowLoadSettings} = require './window-load-settings-helpers' diff --git a/src/update-process-env.js b/src/update-process-env.js index 63dc601bb..2bbbbfe7e 100644 --- a/src/update-process-env.js +++ b/src/update-process-env.js @@ -8,7 +8,14 @@ const ENVIRONMENT_VARIABLES_TO_PRESERVE = new Set([ 'ATOM_HOME' ]) -export default function updateProcessEnv (launchEnv) { +const OSX_SHELLS = new Set([ + '/sh', + '/bash', + '/zsh', + '/fish' +]) + +function updateProcessEnv (launchEnv) { let envToAssign if (launchEnv && launchEnv.PWD) { envToAssign = launchEnv @@ -33,21 +40,38 @@ export default function updateProcessEnv (launchEnv) { } } -function getEnvFromShell () { - let shell = process.env.SHELL - if (shell && (shell.endsWith('/bash') || shell.endsWith('/sh'))) { - 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 +function shouldGetEnvFromShell (shell) { + if (!shell || shell.trim() === '') { + return false + } + for (let s of OSX_SHELLS) { + if (shell.endsWith(s)) { + return true } } + + return false } + +function getEnvFromShell () { + let shell = process.env.SHELL + if (!shouldGetEnvFromShell(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 + } +} + +export default { updateProcessEnv, shouldGetEnvFromShell }