Merge pull request #12396 from atom/jf-fix-zsh-launch-from-dock-regression

Fix Regression In #12317 For zsh and Patch Environment On OSX For Users Of fish
This commit is contained in:
Max Brunsfeld
2016-08-11 16:17:10 -07:00
committed by GitHub
3 changed files with 70 additions and 18 deletions

View File

@@ -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)
})
})
})
})

View File

@@ -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'

View File

@@ -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 }