Permalink
Browse files

refactor: Put getGoBin() into a separate file

  • Loading branch information...
honzajavorek committed Nov 29, 2017
1 parent f848b28 commit e0966a9bb7ea483ae13371e53816881b468f1746
Showing with 178 additions and 29 deletions.
  1. +24 −0 src/get-go-bin.coffee
  2. +17 −26 src/hooks-worker-client.coffee
  3. +114 −0 test/unit/get-go-bin-test.coffee
  4. +23 −3 test/unit/hooks-worker-client-test.coffee
View
@@ -0,0 +1,24 @@
path = require('path')
childProcess = require('child_process')
# Docs:
# - https://golang.org/doc/code.html#GOPATH
# - https://golang.org/cmd/go/#hdr-GOPATH_environment_variable
getGoBin = (callback) ->
goBin = process.env.GOBIN
if goBin
process.nextTick( -> callback(null, goBin))
else
if process.env.GOPATH
process.nextTick( ->
callback(null, path.join(process.env.GOPATH, 'bin'))
)
else
childProcess.exec('go env GOPATH', (err, stdout) ->
return callback(err) if err
callback(null, path.join(stdout.trim(), 'bin'))
)
module.exports = getGoBin
@@ -1,11 +1,13 @@
net = require('net')
path = require('path')
{EventEmitter} = require('events')
spawnArgs = require('spawn-args')
generateUuid = require('uuid').v4
{spawn} = require('./child-process')
logger = require('./logger')
which = require('./which')
getGoBin = require('./get-go-bin')
class HooksWorkerClient
@@ -134,33 +136,22 @@ class HooksWorkerClient
return callback(new Error(msg))
else if @language == 'go'
# https://golang.org/cmd/go/
# "you can add DIR/bin to your PATH to get at the installed commands.
# If the GOBIN environment variable is set, commands are installed to the
# directory it names instead of DIR/bin. GOBIN must be an absolute path."
# Use the gobin if provided, otherwise fall back to gopath
{exec} = require 'child_process'
getGobin = (callback) ->
gobin = process.env.GOBIN
if !gobin
if process.env.GOPATH
gobin = "#{process.env.GOPATH}/bin"
else
exec "go env GOPATH", (error, stdout, stderr) -> callback "#{stdout.trim()}/bin"
return
callback gobin
getGobin (gobin) =>
@handlerCommand = "#{gobin}/goodman"
@handlerCommandArgs = []
unless which.which @handlerCommand
msg = '''\
Go hooks handler command not found in $GOBIN or $GOPATH/bin
Install go hooks handler by running:
$ go get github.com/snikch/goodman/cmd/goodman
'''
return callback(new Error(msg))
getGoBin((err, goBin) =>
if err
callback(new Error("Go doesn't seem to be installed: #{err.message}"))
else
callback()
@handlerCommand = path.join(goBin, 'goodman')
@handlerCommandArgs = []
if which.which(@handlerCommand)
callback()
else
msg = """\
Go hooks handler command not found: #{@handlerCommand}
Install go hooks handler by running:
$ go get github.com/snikch/goodman/cmd/goodman
"""
return callback(new Error(msg))
)
else
parsedArgs = spawnArgs(@language)
@handlerCommand = parsedArgs.shift()
@@ -0,0 +1,114 @@
{assert} = require('chai')
sinon = require('sinon')
childProcess = require('child_process')
getGoBin = require('../../src/get-go-bin')
describe('getGoBin()', ->
goBin = undefined
goPath = undefined
beforeEach( ->
goBin = process.env.GOBIN
delete process.env.GOBIN
goPath = process.env.GOPATH
delete process.env.GOPATH
)
afterEach( ->
process.env.GOBIN = goBin
process.env.GOPATH = goPath
)
describe('when $GOBIN is set', ->
callbackArgs = undefined
beforeEach((done) ->
process.env.GOBIN = '/dummy/gobin/path'
getGoBin((args...) ->
callbackArgs = args
done()
)
)
it('resolves as $GOBIN', ->
assert.deepEqual(callbackArgs, [null, '/dummy/gobin/path'])
)
)
describe('when $GOPATH is set', ->
callbackArgs = undefined
beforeEach((done) ->
process.env.GOPATH = '/dummy/gopath/path'
getGoBin((args...) ->
callbackArgs = args
done()
)
)
it('resolves as $GOPATH + /bin', ->
assert.deepEqual(callbackArgs, [null, '/dummy/gopath/path/bin'])
)
)
describe('when both $GOBIN and $GOPATH are set', ->
callbackArgs = undefined
beforeEach((done) ->
process.env.GOBIN = '/dummy/gobin/path'
process.env.GOPATH = '/dummy/gopath/path'
getGoBin((args...) ->
callbackArgs = args
done()
)
)
it('resolves as $GOBIN', ->
assert.deepEqual(callbackArgs, [null, '/dummy/gobin/path'])
)
)
describe('when neither $GOBIN nor $GOPATH are set', ->
callbackArgs = undefined
beforeEach((done) ->
sinon.stub(childProcess, 'exec').callsFake((command, callback) ->
callback(null, '/dummy/gopath/path')
)
getGoBin((args...) ->
callbackArgs = args
done()
)
)
afterEach( ->
childProcess.exec.restore()
)
it('calls \'go env GOPATH\' + /bin', ->
assert.deepEqual(callbackArgs, [null, '/dummy/gopath/path/bin'])
)
)
describe('when \'go env GOPATH\' fails', ->
error = new Error('Ouch!')
callbackArgs = undefined
beforeEach((done) ->
sinon.stub(childProcess, 'exec').callsFake((command, callback) ->
callback(error)
)
getGoBin((args...) ->
callbackArgs = args
done()
)
)
afterEach( ->
childProcess.exec.restore()
)
it('propagates the error', ->
assert.deepEqual(callbackArgs, [error])
)
)
)
@@ -344,8 +344,24 @@ describe 'Hooks worker client', ->
assert.include err.message, "composer require ddelnano/dredd-hooks-php --dev"
done()
describe 'when --language=go option is given and go is not installed', ->
beforeEach ->
runner.hooks['configuration'] =
options:
language: 'go'
hookfiles: 'gobinary'
it 'should write an error', (done) ->
loadWorkerClient (err) ->
assert.isOk err
assert.include err.message, "Go doesn't seem to be installed"
done()
describe 'when --language=go option is given and the worker is not installed', ->
goBin = undefined
beforeEach ->
goBin = process.env.GOBIN
process.env.GOBIN = '/dummy/gobin/path'
sinon.stub(whichStub, 'which').callsFake (command) -> false
runner.hooks['configuration'] =
@@ -354,6 +370,7 @@ describe 'Hooks worker client', ->
hookfiles: 'gobinary'
afterEach ->
whichStub.which.restore()
process.env.GOBIN = goBin
it 'should write a hint how to install', (done) ->
loadWorkerClient (err) ->
@@ -362,7 +379,10 @@ describe 'Hooks worker client', ->
done()
describe 'when --language=go option is given and the worker is installed', ->
goBin = undefined
beforeEach ->
goBin = process.env.GOBIN
process.env.GOBIN = '/dummy/gobin/path'
sinon.stub(crossSpawnStub, 'spawn').callsFake( ->
emitter = new EventEmitter
emitter.stdout = new EventEmitter
@@ -387,16 +407,16 @@ describe 'Hooks worker client', ->
whichStub.which.restore()
HooksWorkerClient.prototype.terminateHandler.restore()
process.env.GOBIN = goBin
it 'should spawn the server process with command "$GOPATH/bin/goodman"', (done) ->
process.env.GOPATH = 'gopath'
it 'should spawn the server process with command "$GOBIN/goodman"', (done) ->
loadWorkerClient (err) ->
assert.isUndefined err
hooksWorkerClient.stop (err) ->
assert.isUndefined err
assert.isTrue crossSpawnStub.spawn.called
assert.equal crossSpawnStub.spawn.getCall(0).args[0], 'gopath/bin/goodman'
assert.equal crossSpawnStub.spawn.getCall(0).args[0], path.join(dummyPath, 'goodman')
done()
it 'should pass --hookfiles option as an array of arguments', (done) ->

0 comments on commit e0966a9

Please sign in to comment.