Merge pull request #103 from cypress-io/102

fix $SHELL undefined in docker, use process.env.SHELL, fixes #102
This commit is contained in:
Brian Mann
2017-05-27 11:40:32 -04:00
committed by GitHub
3 changed files with 64 additions and 37 deletions
+26 -14
View File
@@ -1,8 +1,6 @@
_ = require("lodash")
cp = require("child_process")
Promise = require("bluebird")
exec = Promise.promisify(cp.exec)
cp = require("./util/child_process")
profiles = {
"~/.profile": /\/sh$/
@@ -14,33 +12,47 @@ profiles = {
}
sourced = false
shell = null
getProfilePath = (shellPath) ->
for profilePath, regex of profiles
return profilePath if regex.test(shellPath)
shell = null
getShellObj = (pathToShell) ->
pathToShell = _.trim(pathToShell)
shell = {
shellPath: pathToShell
profilePath: getProfilePath(pathToShell)
}
Promise.resolve(shell)
getShell = ->
return Promise.resolve(shell) if shell
exec("echo $SHELL").then (shell) ->
path = _.trim(shell)
shell = {
shellPath: path
profilePath: getProfilePath(path)
}
return shell
## if we didn't get a shell such
## as when we're in docker
if not s = process.env.SHELL
cp.execAsync("which bash")
.then(getShellObj)
.catch ->
getShellObj("")
else
getShellObj(s)
module.exports = {
reset: ->
## for testing purposes
shell = null
run: (projectRoot, options) ->
child = null
run = ->
getShell().then (shell) ->
getShell()
.then (shell) ->
new Promise (resolve, reject) ->
cmd = if shell.profilePath and not sourced
## we only need to source once
sourced = true
@@ -0,0 +1,4 @@
cp = require("child_process")
Promise = require("bluebird")
module.exports = Promise.promisifyAll(cp)
+34 -23
View File
@@ -1,6 +1,7 @@
require("../spec_helper")
_ = require("lodash")
_ = require("lodash")
cp = require("#{root}lib/util/child_process")
exec = require("#{root}lib/exec")
runCommand = (cmd, options = {}) ->
@@ -17,59 +18,69 @@ fail = (message) -> throw new Error(message)
describe "lib/exec", ->
@timeout(10000)
beforeEach ->
@shell = process.env.SHELL
exec.reset()
afterEach ->
if @shell
process.env.SHELL = @shell
describe "when exit code is 0", ->
it "reports the stdout, stderr, and code", ->
runCommand("echo 'foo'")
.catch (err) ->
fail("should not reject, but rejected with: #{err.message}")
.then (result)->
.then (result) ->
expect(result.stdout).to.equal "foo\n"
expect(result.stderr).to.equal ""
expect(result.code).to.equal 0
it "handles multiple data events", ->
runCommand("echo 'foo'; sleep 0.01; echo 'bar'")
.catch (err) ->
fail("should not reject, but rejected with: #{err.message}")
.then (result)->
.then (result) ->
expect(result.stdout).to.equal "foo\nbar\n"
it "handles arguments and pipes", ->
runCommand("echo 'foo\nbar\nbaz' | grep -n -C 1 ar")
.catch (err) ->
fail("should not reject, but rejected with: #{err.message}")
.then (result)->
.then (result) ->
expect(result.stdout).to.equal "1-foo\n2:bar\n3-baz\n"
it "passes through environment variables already in env", ->
process.env.ALREADY_THERE = "already there"
runCommand("echo $ALREADY_THERE")
.catch (err) ->
fail("should not reject, but rejected with: #{err.message}")
.then (result)->
.then (result) ->
expect(result.stdout).to.equal "already there\n"
it "passes through environment variables specified", ->
runCommand("echo $SOME_VAR", { env: { SOME_VAR: "foo" } })
.catch (err) ->
fail("should not reject, but rejected with: #{err.message}")
.then (result)->
.then (result) ->
expect(result.stdout).to.equal "foo\n"
it "falls back to asking for bash on docker when there is no $SHELL", ->
delete process.env.SHELL
@sandbox.stub(cp, "execAsync").withArgs("which bash").resolves("/bin/bash")
runCommand("echo foo")
.then (result) ->
expect(result.stdout).to.eq("foo\n")
expect(cp.execAsync).to.be.calledWith("which bash")
it.skip "TODO: test what happens when which bash fails - like on windows", ->
@sandbox.stub(cp, "execAsync").withArgs("which bash").rejects(new Error)
runCommand("echo foo")
.then (result) ->
expect(result.stdout).to.eq("foo\n")
it "reports the stderr", ->
runCommand(">&2 echo 'some error'")
.catch (err) ->
fail("should not reject, but rejected with: #{err.message}")
.then (result)->
.then (result) ->
expect(result.stderr).to.equal "some error\n"
describe "when exit code is non-zero", ->
it "reports the stdout, stderr, and code", ->
runCommand("cat nooope")
.catch (err) ->
fail("should not reject, but rejected with: #{err.message}")
.then (result)->
.then (result) ->
expect(result.stdout).to.equal ""
expect(result.stderr).to.equal "cat: nooope: No such file or directory\n"
expect(result.code).to.equal 1