diff --git a/spec/fixtures/sample.txt b/spec/fixtures/sample.txt index 9701a96c5..0a2b6a502 100644 --- a/spec/fixtures/sample.txt +++ b/spec/fixtures/sample.txt @@ -1 +1 @@ -Some textSome textSome text. +Some textSome textSome textSome text. diff --git a/spec/project-spec.coffee b/spec/project-spec.coffee index 499efd017..61dd244e8 100644 --- a/spec/project-spec.coffee +++ b/spec/project-spec.coffee @@ -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 diff --git a/src/project.coffee b/src/project.coffee index 93a3ed496..f2ace97e2 100644 --- a/src/project.coffee +++ b/src/project.coffee @@ -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 ###