Add basic atom.project.getEnv API

This commit is contained in:
Lee Dohm
2016-03-03 21:15:52 -08:00
committed by Joe Fitzgerald
parent 501eadccca
commit 3201d7f746
3 changed files with 93 additions and 1 deletions

View File

@@ -1 +1 @@
Some textSome textSome text.
Some textSome textSome textSome text.

View File

@@ -537,3 +537,56 @@ describe "Project", ->
randomPath = path.join("some", "random", "path")
expect(atom.project.contains(randomPath)).toBe false
describe ".getEnv", ->
afterEach ->
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", ->
it "returns the PATH unchanged", ->
spyOn(process.env, "TERM").andReturn(undefined)
expect(atom.project.getEnv().PATH).toEqual process.env.PATH
describe "when TERM is set", ->
it "returns the PATH unchanged", ->
spyOn(process.env, "TERM").andReturn("foo")
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", ->
it "replaces the PATH with the one obtained from the shell", ->
env = _.clone(process.env)
delete env.TERM
spyOn(process, "env").andReturn(env)
spyOn(atom.project, "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.getShellPath()).toEqual "/usr/bin:/bin:/usr/sbin:/sbin:/some/crazy/path/entry/that/should/not/exist"
expect(atom.project.getEnv().PATH).toEqual "/usr/bin:/bin:/usr/sbin:/sbin:/some/crazy/path/entry/that/should/not/exist"
describe "when TERM is set", ->
it "returns the PATH unchanged", ->
spyOn(process.env, "TERM").andReturn("foo")
expect(atom.project.getEnv().PATH).toEqual process.env.PATH

View File

@@ -12,6 +12,9 @@ TextEditor = require './text-editor'
Task = require './task'
GitRepositoryProvider = require './git-repository-provider'
child_process = require 'child_process'
os = require 'os'
# Extended: Represents a project that's opened in Atom.
#
# An instance of this class is always available as the `atom.project` global.
@@ -271,6 +274,42 @@ class Project extends Model
contains: (pathToCheck) ->
@rootDirectories.some (dir) -> dir.contains(pathToCheck)
###
Section: Environment
###
getEnv: ->
unless @env?
@env = _.clone(process.env)
if process.platform is "darwin" and not process.env.TERM?
@env.PATH = @getShellPath()
_.clone(@env)
getShellPath: ->
shellEnvText = @getShellEnv()
env = {}
for line in shellEnvText.split(os.EOL)
if line.includes("=")
components = line.split("=")
if components.length is 2
env[components[0]] = components[1]
else
k = components.shift()
v = components.join("=")
env[k] = v
env.PATH
getShellEnv: ->
shell = process.env.SHELL ? "/bin/bash"
results = child_process.spawnSync shell, ["--login"], input: "env", encoding: "utf8"
return if results.error?
return unless results.stdout and results.stdout.length > 0
results.stdout
###
Section: Private
###