2016-08-04 01:50:21 +03:00
|
|
|
/** @babel */
|
2016-03-11 01:16:41 +03:00
|
|
|
/* eslint-env jasmine */
|
|
|
|
|
2016-08-16 03:05:18 +03:00
|
|
|
import path from 'path'
|
|
|
|
import temp from 'temp'
|
2016-03-11 01:16:41 +03:00
|
|
|
import child_process from 'child_process'
|
2016-08-12 02:07:21 +03:00
|
|
|
import {updateProcessEnv, shouldGetEnvFromShell} from '../src/update-process-env'
|
2016-08-04 01:50:21 +03:00
|
|
|
import dedent from 'dedent'
|
2016-03-11 01:16:41 +03:00
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
describe('updateProcessEnv(launchEnv)', function () {
|
|
|
|
let originalProcessEnv, originalProcessPlatform
|
2016-03-11 01:16:41 +03:00
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
originalProcessEnv = process.env
|
|
|
|
originalProcessPlatform = process.platform
|
|
|
|
process.env = {}
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
afterEach(function () {
|
|
|
|
process.env = originalProcessEnv
|
|
|
|
process.platform = originalProcessPlatform
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
describe('when the launch environment appears to come from a shell', function () {
|
|
|
|
it('updates process.env to match the launch environment', function () {
|
|
|
|
process.env = {
|
|
|
|
WILL_BE_DELETED: 'hi',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
2016-08-10 00:38:19 +03:00
|
|
|
ATOM_HOME: '/the/atom/home'
|
2016-08-04 01:50:21 +03:00
|
|
|
}
|
2016-08-31 09:07:15 +03:00
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
const initialProcessEnv = process.env
|
|
|
|
|
2016-08-31 09:07:15 +03:00
|
|
|
updateProcessEnv({ATOM_SUPPRESS_ENV_PATCHING: 'true', PWD: '/the/dir', TERM: 'xterm-something', KEY1: 'value1', KEY2: 'value2'})
|
2016-08-04 01:50:21 +03:00
|
|
|
expect(process.env).toEqual({
|
2016-08-31 09:07:15 +03:00
|
|
|
ATOM_SUPPRESS_ENV_PATCHING: 'true',
|
2016-08-04 01:50:21 +03:00
|
|
|
PWD: '/the/dir',
|
2016-08-30 22:52:56 +03:00
|
|
|
TERM: 'xterm-something',
|
2016-08-04 01:50:21 +03:00
|
|
|
KEY1: 'value1',
|
|
|
|
KEY2: 'value2',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
2016-08-10 00:38:19 +03:00
|
|
|
ATOM_HOME: '/the/atom/home'
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
// See #11302. On Windows, `process.env` is a magic object that offers
|
|
|
|
// case-insensitive environment variable matching, so we cannot replace it
|
|
|
|
// with another object.
|
|
|
|
expect(process.env).toBe(initialProcessEnv)
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
2016-08-16 03:05:18 +03:00
|
|
|
|
|
|
|
it('allows ATOM_HOME to be overwritten only if the new value is a valid path', function () {
|
|
|
|
newAtomHomePath = temp.mkdirSync('atom-home')
|
|
|
|
|
|
|
|
process.env = {
|
|
|
|
WILL_BE_DELETED: 'hi',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
|
|
|
ATOM_HOME: '/the/atom/home'
|
|
|
|
}
|
|
|
|
|
2016-08-31 09:07:15 +03:00
|
|
|
updateProcessEnv({ATOM_SUPPRESS_ENV_PATCHING: 'true', PWD: '/the/dir'})
|
2016-08-16 03:05:18 +03:00
|
|
|
expect(process.env).toEqual({
|
|
|
|
PWD: '/the/dir',
|
2016-08-31 09:07:15 +03:00
|
|
|
ATOM_SUPPRESS_ENV_PATCHING: 'true',
|
2016-08-16 03:05:18 +03:00
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
|
|
|
ATOM_HOME: '/the/atom/home'
|
|
|
|
})
|
|
|
|
|
2016-08-31 09:07:15 +03:00
|
|
|
updateProcessEnv({ATOM_SUPPRESS_ENV_PATCHING: 'true', PWD: '/the/dir', ATOM_HOME: path.join(newAtomHomePath, 'non-existent')})
|
2016-08-16 03:05:18 +03:00
|
|
|
expect(process.env).toEqual({
|
2016-08-31 09:07:15 +03:00
|
|
|
ATOM_SUPPRESS_ENV_PATCHING: 'true',
|
2016-08-16 03:05:18 +03:00
|
|
|
PWD: '/the/dir',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
|
|
|
ATOM_HOME: '/the/atom/home'
|
|
|
|
})
|
|
|
|
|
2016-08-31 09:07:15 +03:00
|
|
|
updateProcessEnv({ATOM_SUPPRESS_ENV_PATCHING: 'true', PWD: '/the/dir', ATOM_HOME: newAtomHomePath})
|
2016-08-16 03:05:18 +03:00
|
|
|
expect(process.env).toEqual({
|
2016-08-31 09:07:15 +03:00
|
|
|
ATOM_SUPPRESS_ENV_PATCHING: 'true',
|
2016-08-16 03:05:18 +03:00
|
|
|
PWD: '/the/dir',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
|
|
|
ATOM_HOME: newAtomHomePath
|
|
|
|
})
|
|
|
|
})
|
2016-08-31 09:07:15 +03:00
|
|
|
|
|
|
|
it('allows ATOM_SUPPRESS_ENV_PATCHING to be preserved if set', function () {
|
|
|
|
newAtomHomePath = temp.mkdirSync('atom-home')
|
|
|
|
|
|
|
|
process.env = {
|
|
|
|
WILL_BE_DELETED: 'hi',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
|
|
|
ATOM_HOME: '/the/atom/home'
|
|
|
|
}
|
|
|
|
|
|
|
|
updateProcessEnv({ATOM_SUPPRESS_ENV_PATCHING: 'true', PWD: '/the/dir'})
|
|
|
|
expect(process.env).toEqual({
|
|
|
|
PWD: '/the/dir',
|
|
|
|
ATOM_SUPPRESS_ENV_PATCHING: 'true',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
|
|
|
ATOM_HOME: '/the/atom/home'
|
|
|
|
})
|
|
|
|
|
|
|
|
updateProcessEnv({PWD: '/the/dir'})
|
|
|
|
expect(process.env).toEqual({
|
|
|
|
ATOM_SUPPRESS_ENV_PATCHING: 'true',
|
|
|
|
PWD: '/the/dir',
|
|
|
|
NODE_ENV: 'the-node-env',
|
|
|
|
NODE_PATH: '/the/node/path',
|
|
|
|
ATOM_HOME: '/the/atom/home'
|
|
|
|
})
|
|
|
|
})
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
describe('when the launch environment does not come from a shell', function () {
|
|
|
|
describe('on osx', function () {
|
|
|
|
it('updates process.env to match the environment in the user\'s login shell', function () {
|
|
|
|
process.platform = 'darwin'
|
|
|
|
process.env.SHELL = '/my/custom/bash'
|
2016-03-11 01:16:41 +03:00
|
|
|
|
|
|
|
spyOn(child_process, 'spawnSync').andReturn({
|
2016-08-04 01:50:21 +03:00
|
|
|
stdout: dedent`
|
|
|
|
FOO=BAR=BAZ=QUUX
|
|
|
|
TERM=xterm-something
|
|
|
|
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/crazy/path
|
|
|
|
`
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
updateProcessEnv(process.env)
|
|
|
|
expect(child_process.spawnSync.mostRecentCall.args[0]).toBe('/my/custom/bash')
|
|
|
|
expect(process.env).toEqual({
|
|
|
|
FOO: 'BAR=BAZ=QUUX',
|
|
|
|
TERM: 'xterm-something',
|
|
|
|
PATH: '/usr/bin:/bin:/usr/sbin:/sbin:/crazy/path'
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
2016-08-05 02:01:55 +03:00
|
|
|
|
|
|
|
// Doesn't error
|
|
|
|
updateProcessEnv(null)
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
2016-08-04 01:50:21 +03:00
|
|
|
})
|
2016-03-11 01:16:41 +03:00
|
|
|
|
2016-08-30 22:52:56 +03:00
|
|
|
describe('on linux', function () {
|
|
|
|
it('updates process.env to match the environment in the user\'s login shell', function () {
|
|
|
|
process.platform = 'linux'
|
|
|
|
process.env.SHELL = '/my/custom/bash'
|
|
|
|
|
|
|
|
spyOn(child_process, 'spawnSync').andReturn({
|
|
|
|
stdout: dedent`
|
|
|
|
FOO=BAR=BAZ=QUUX
|
|
|
|
TERM=xterm-something
|
|
|
|
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/crazy/path
|
|
|
|
`
|
|
|
|
})
|
|
|
|
|
|
|
|
updateProcessEnv(process.env)
|
|
|
|
expect(child_process.spawnSync.mostRecentCall.args[0]).toBe('/my/custom/bash')
|
|
|
|
expect(process.env).toEqual({
|
|
|
|
FOO: 'BAR=BAZ=QUUX',
|
|
|
|
TERM: 'xterm-something',
|
|
|
|
PATH: '/usr/bin:/bin:/usr/sbin:/sbin:/crazy/path'
|
|
|
|
})
|
|
|
|
|
|
|
|
// Doesn't error
|
|
|
|
updateProcessEnv(null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-08-31 09:07:15 +03:00
|
|
|
describe('on windows', function () {
|
2016-08-04 01:50:21 +03:00
|
|
|
it('does not update process.env', function () {
|
|
|
|
process.platform = 'win32'
|
|
|
|
spyOn(child_process, 'spawnSync')
|
|
|
|
process.env = {FOO: 'bar'}
|
2016-03-11 01:16:41 +03:00
|
|
|
|
2016-08-04 01:50:21 +03:00
|
|
|
updateProcessEnv(process.env)
|
|
|
|
expect(child_process.spawnSync).not.toHaveBeenCalled()
|
|
|
|
expect(process.env).toEqual({FOO: 'bar'})
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
|
|
|
})
|
2016-08-11 21:45:02 +03:00
|
|
|
|
2016-08-12 02:08:28 +03:00
|
|
|
describe('shouldGetEnvFromShell()', function () {
|
2016-08-30 22:52:56 +03:00
|
|
|
it('indicates when the environment should be fetched from the shell', function () {
|
2016-08-11 21:45:02 +03:00
|
|
|
process.platform = 'darwin'
|
2016-08-30 22:52:56 +03:00
|
|
|
expect(shouldGetEnvFromShell({SHELL: '/bin/sh'})).toBe(true)
|
|
|
|
process.platform = 'linux'
|
|
|
|
expect(shouldGetEnvFromShell({SHELL: '/bin/sh'})).toBe(true)
|
2016-08-11 21:45:02 +03:00
|
|
|
})
|
|
|
|
|
2016-08-12 02:08:28 +03:00
|
|
|
it('returns false when the shell should not be patched', function () {
|
2016-08-11 21:45:02 +03:00
|
|
|
process.platform = 'darwin'
|
2016-08-31 09:07:15 +03:00
|
|
|
expect(shouldGetEnvFromShell({ATOM_SUPPRESS_ENV_PATCHING: 'true', SHELL: '/bin/sh'})).toBe(false)
|
2016-08-30 22:52:56 +03:00
|
|
|
process.platform = 'linux'
|
2016-08-31 09:07:15 +03:00
|
|
|
expect(shouldGetEnvFromShell({ATOM_SUPPRESS_ENV_PATCHING: 'true', SHELL: '/bin/sh'})).toBe(false)
|
2016-08-12 01:45:19 +03:00
|
|
|
})
|
|
|
|
|
2016-08-12 02:08:28 +03:00
|
|
|
it('returns false when the shell is undefined or empty', function () {
|
2016-08-12 01:45:19 +03:00
|
|
|
process.platform = 'darwin'
|
2016-08-12 02:07:21 +03:00
|
|
|
expect(shouldGetEnvFromShell(undefined)).toBe(false)
|
2016-08-30 22:52:56 +03:00
|
|
|
expect(shouldGetEnvFromShell({})).toBe(false)
|
|
|
|
|
|
|
|
process.platform = 'linux'
|
|
|
|
expect(shouldGetEnvFromShell(undefined)).toBe(false)
|
|
|
|
expect(shouldGetEnvFromShell({})).toBe(false)
|
2016-08-11 21:45:02 +03:00
|
|
|
})
|
|
|
|
})
|
2016-03-11 01:16:41 +03:00
|
|
|
})
|
|
|
|
})
|