From a3ba15c8a13c5cc61155df9b33abdf6c5c58ddbf Mon Sep 17 00:00:00 2001 From: Lee Dohm Date: Tue, 8 Mar 2016 15:50:53 -0800 Subject: [PATCH] Update Project to use new environment module --- spec/project-spec.coffee | 20 +++++++++---------- src/project.coffee | 43 +++------------------------------------- 2 files changed, 13 insertions(+), 50 deletions(-) diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 35cd0157f..f5fcad8e3 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -8,6 +8,8 @@ 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')]) @@ -581,21 +583,19 @@ describe "Project", -> beforeEach -> delete process.env.TERM - it "replaces the PATH with the one obtained from the shell", -> - 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 - """ + 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.getShellPath()).toEqual "/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).not.toEqual "BAR" + expect(atom.project.getEnv().FOO).toEqual "BAR" it "does the best it can when there is an error retrieving the shell environment", -> - spyOn(atom.project, "getShellEnv").andReturn(undefined) + spyOn(environment, "getShellEnv").andReturn(undefined) - expect(atom.project.getShellPath()).toBeUndefined() expect(atom.project.getEnv().PATH).not.toBeUndefined() expect(atom.project.getEnv().PATH).toEqual process.env.PATH diff --git a/src/project.coffee b/src/project.coffee index c8fd3ddbe..2a21aa2d8 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -288,8 +288,9 @@ class Project extends Model unless @env? @env = _.clone(process.env) if process.platform is "darwin" and not process.env.TERM? - shellPath = @getShellPath() - @env.PATH = shellPath if shellPath? + {getShellEnv} = require("../src/environment") + shellEnv = getShellEnv() + @env = shellEnv if shellEnv? _.clone(@env) @@ -297,44 +298,6 @@ class Project extends Model Section: Private ### - # Gets the user's configured shell `PATH`. - # - # Returns the value of `PATH` or `undefined` if there was an error. - getShellPath: -> - shellEnvText = @getShellEnv() - return unless shellEnvText? - - 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 - - # Gets a dump of the user's configured shell environment. - # - # Returns the output of the `env` command or `undefined` if there was an error. - getShellEnv: -> - shell = process.env.SHELL ? "/bin/bash" - - # The `-ilc` set of options was tested to work with the OS X v10.11 - # default-installed versions of bash, zsh, sh, and ksh. It *does not* - # work with csh or tcsh. Given that bash and zsh should cover the - # vast majority of users and it gracefully falls back to prior behavior, - # this should be safe. - results = child_process.spawnSync shell, ["-ilc"], input: "env", encoding: "utf8" - return if results.error? - return unless results.stdout and results.stdout.length > 0 - - results.stdout - consumeServices: ({serviceHub}) -> serviceHub.consume( 'atom.directory-provider',