Remove Project API, Work With process.env Directly

- Convert environment.coffee and environment-spec.coffee to JavaScript
- Pass the process's environment across the wire when launching atom multiple times from the command line
This commit is contained in:
Joe Fitzgerald
2016-03-10 15:16:41 -07:00
parent b98388fa76
commit 6b38049b8d
11 changed files with 275 additions and 189 deletions

View File

@@ -362,16 +362,3 @@ describe "AtomEnvironment", ->
version = '1.7.0-dev-5340c91'
expect(atom.getReleaseChannel()).toBe 'dev'
describe "environment patching", ->
it "patches process.env on startup", ->
configDirPath = temp.mkdirSync()
fakeDocument = {
addEventListener: ->
removeEventListener: ->
head: document.createElement('head')
body: document.createElement('body')
}
atomEnvironment = new AtomEnvironment({applicationDelegate: atom.applicationDelegate, window, document: fakeDocument})
expect(process.env).toEqual atomEnvironment.project.getEnv()

View File

@@ -1,27 +0,0 @@
child_process = require('child_process')
{getShellEnv} = require("../src/environment")
fdescribe "Environment handling", ->
describe "when things are configured properly", ->
beforeEach ->
spyOn(child_process, "spawnSync").andReturn
stdout: """
FOO=BAR
TERM=xterm-something
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/some/crazy/path/entry/that/should/not/exist
"""
it "returns an object containing the information from the user's shell environment", ->
env = getShellEnv()
expect(env.FOO).toEqual "BAR"
expect(env.TERM).toEqual "xterm-something"
expect(env.PATH).toEqual "/usr/bin:/bin:/usr/sbin:/sbin:/some/crazy/path/entry/that/should/not/exist"
describe "when an error occurs", ->
beforeEach ->
spyOn(child_process, "spawnSync").andReturn
error: new Error("testing when an error occurs")
it "returns undefined", ->
expect(getShellEnv()).toBeUndefined()

161
spec/environment-spec.js Normal file
View File

@@ -0,0 +1,161 @@
'use babel'
/* eslint-env jasmine */
import child_process from 'child_process'
import environment from '../src/environment'
import os from 'os'
import _ from 'underscore-plus'
fdescribe('Environment handling', () => {
let originalEnv
let options
beforeEach(() => {
originalEnv = process.env
delete process._originalEnv
options = {
platform: process.platform,
env: _.clone(process.env)
}
})
afterEach(() => {
process.env = originalEnv
delete process._originalEnv
})
describe('on OSX, when PWD is not set', () => {
beforeEach(() => {
options.platform = 'darwin'
})
describe('needsPatching', () => {
it('returns true if PWD is unset', () => {
delete options.env.PWD
expect(environment.needsPatching(options)).toBe(true)
options.env.PWD = undefined
expect(environment.needsPatching(options)).toBe(true)
options.env.PWD = null
expect(environment.needsPatching(options)).toBe(true)
options.env.PWD = false
expect(environment.needsPatching(options)).toBe(true)
})
it('returns false if PWD is set', () => {
options.env.PWD = 'xterm'
expect(environment.needsPatching(options)).toBe(false)
})
})
describe('normalize', () => {
it('changes process.env if PWD is unset', () => {
if (process.platform === 'win32') {
return
}
delete options.env.PWD
environment.normalize(options)
expect(process._originalEnv).toBeDefined()
expect(process._originalEnv).toBeTruthy()
expect(process.env).toBeDefined()
expect(process.env).toBeTruthy()
expect(process.env.PWD).toBeDefined()
expect(process.env.PWD).toBeTruthy()
expect(process.env.PATH).toBeDefined()
expect(process.env.PATH).toBeTruthy()
expect(process.env.ATOM_HOME).toBeDefined()
expect(process.env.ATOM_HOME).toBeTruthy()
})
})
})
describe('on a platform other than OSX', () => {
beforeEach(() => {
options.platform = 'penguin'
})
describe('needsPatching', () => {
it('returns false if PWD is set or unset', () => {
delete options.env.PWD
expect(environment.needsPatching(options)).toBe(false)
options.env.PWD = undefined
expect(environment.needsPatching(options)).toBe(false)
options.env.PWD = null
expect(environment.needsPatching(options)).toBe(false)
options.env.PWD = false
expect(environment.needsPatching(options)).toBe(false)
options.env.PWD = '/'
expect(environment.needsPatching(options)).toBe(false)
})
it('returns false for linux', () => {
options.platform = 'linux'
options.PWD = '/'
expect(environment.needsPatching(options)).toBe(false)
})
it('returns false for windows', () => {
options.platform = 'win32'
options.PWD = 'c:\\'
expect(environment.needsPatching(options)).toBe(false)
})
})
describe('normalize', () => {
it('does not change the environment', () => {
if (process.platform === 'win32') {
return
}
delete options.env.PWD
environment.normalize(options)
expect(process._originalEnv).toBeUndefined()
expect(process.env).toBeDefined()
expect(process.env).toBeTruthy()
expect(process.env.PATH).toBeDefined()
expect(process.env.PATH).toBeTruthy()
expect(process.env.PWD).toBeUndefined()
expect(process.env.PATH).toBe(originalEnv.PATH)
expect(process.env.ATOM_HOME).toBeDefined()
expect(process.env.ATOM_HOME).toBeTruthy()
})
})
})
describe('getFromShell', () => {
describe('when things are configured properly', () => {
beforeEach(() => {
spyOn(child_process, 'spawnSync').andReturn({
stdout: 'FOO=BAR' + os.EOL + 'TERM=xterm-something' + os.EOL +
'PATH=/usr/bin:/bin:/usr/sbin:/sbin:/crazy/path'
})
})
it('returns an object containing the information from the user\'s shell environment', () => {
let env = environment.getFromShell()
expect(env.FOO).toEqual('BAR')
expect(env.TERM).toEqual('xterm-something')
expect(env.PATH).toEqual('/usr/bin:/bin:/usr/sbin:/sbin:/crazy/path')
})
})
describe('when an error occurs launching the shell', () => {
beforeEach(() => {
spyOn(child_process, 'spawnSync').andReturn({
error: new Error('testing when an error occurs')
})
})
it('returns undefined', () => {
expect(environment.getFromShell()).toBeUndefined()
})
it('leaves the environment as-is when normalize() is called', () => {
options.platform = 'darwin'
delete options.env.PWD
expect(environment.needsPatching(options)).toBe(true)
environment.normalize(options)
expect(process.env).toBeDefined()
expect(process._originalEnv).toBeUndefined()
})
})
})
})

View File

@@ -8,8 +8,6 @@ BufferedProcess = require '../src/buffered-process'
{Directory} = require 'pathwatcher'
GitRepository = require '../src/git-repository'
environment = require '../src/environment'
describe "Project", ->
beforeEach ->
atom.project.setPaths([atom.project.getDirectories()[0]?.resolve('dir')])
@@ -539,69 +537,3 @@ describe "Project", ->
randomPath = path.join("some", "random", "path")
expect(atom.project.contains(randomPath)).toBe false
describe ".getEnv", ->
[originalTerm] = []
beforeEach ->
originalTerm = process.env.TERM
afterEach ->
process.env.TERM = originalTerm
delete atom.project.env
it "returns a copy of the environment", ->
env = atom.project.getEnv()
env.PROJECT_GET_ENV_TESTING = "foo"
expect(process.env.PROJECT_GET_ENV_TESTING).not.toEqual "foo"
expect(atom.project.getEnv().PROJECT_GET_ENV_TESTING).not.toEqual "foo"
describe "on platforms other than OS X", ->
beforeEach ->
spyOn(process, "platform").andReturn("foo")
describe "when TERM is not set", ->
beforeEach ->
delete process.env.TERM
it "returns the PATH unchanged", ->
expect(atom.project.getEnv().PATH).toEqual process.env.PATH
describe "when TERM is set", ->
beforeEach ->
process.env.TERM = "foo"
it "returns the PATH unchanged", ->
expect(atom.project.getEnv().PATH).toEqual process.env.PATH
describe "on OS X", ->
beforeEach ->
spyOn(process, "platform").andReturn("darwin")
describe "when TERM is not set", ->
beforeEach ->
delete process.env.TERM
it "replaces the environment with the one obtained from the shell", ->
spyOn(environment, "getShellEnv").andReturn
FOO: "BAR"
TERM: "xterm-something"
PATH: "/usr/bin:/bin:/usr/sbin:/sbin:/some/crazy/path/entry/that/should/not/exist"
expect(atom.project.getEnv().TERM).toEqual "xterm-something"
expect(atom.project.getEnv().PATH).toEqual "/usr/bin:/bin:/usr/sbin:/sbin:/some/crazy/path/entry/that/should/not/exist"
expect(atom.project.getEnv().FOO).toEqual "BAR"
it "does the best it can when there is an error retrieving the shell environment", ->
spyOn(environment, "getShellEnv").andReturn(undefined)
expect(atom.project.getEnv().PATH).not.toBeUndefined()
expect(atom.project.getEnv().PATH).toEqual process.env.PATH
describe "when TERM is set", ->
beforeEach ->
process.env.TERM = "foo"
it "returns the PATH unchanged", ->
expect(atom.project.getEnv().PATH).toEqual process.env.PATH