balena-cli/lib/resin/log/log.spec.coffee

142 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-11-21 15:22:26 +00:00
_ = require('lodash')
chai = require('chai')
chai.use(require('sinon-chai'))
expect = chai.expect
sinon = require('sinon')
log = require('./log')
MESSAGE =
foobar: 'Foo Bar'
# Very handy to check that the real console functions
# were called without printing anything, and preventing
# us from having to mock console, which is used by Mocha
empty: ''
describe 'Log:', ->
testConsoleFunction = (logName, consoleName, message, assertions) ->
spy = sinon.spy(console, consoleName)
log[logName](message)
assertions(spy)
console[consoleName].restore()
testConsoleFunctionBeingCalled = (logName, consoleName, message) ->
testConsoleFunction logName, consoleName, message, (spy) ->
expect(spy).to.have.been.calledOnce
expect(spy).to.have.been.calledWith(message)
testConsoleFunctionNotBeingCalled = (logName, consoleName, message) ->
testConsoleFunction logName, consoleName, message, (spy) ->
expect(spy).to.not.have.been.called
describe 'if quiet is false', ->
beforeEach ->
log.setQuiet(false)
describe '#error()', ->
it 'should output to console.error', ->
testConsoleFunctionBeingCalled('error', 'error', MESSAGE.empty)
describe '#warning()', ->
it 'should output to console.warn', ->
testConsoleFunctionBeingCalled('warning', 'warn', MESSAGE.empty)
describe '#info()', ->
it 'should output to console.info', ->
testConsoleFunctionBeingCalled('info', 'info', MESSAGE.empty)
describe '#out()', ->
it 'should output to console.log', ->
testConsoleFunctionBeingCalled('out', 'log', MESSAGE.empty)
2014-11-28 16:46:24 +00:00
describe '#array()', ->
array = [ 1, 2, 3, 4 ]
it 'should call log function for every line', ->
spy = sinon.spy()
log.array(array, spy)
expect(spy.callCount).to.equal(array.length)
for item in array
expect(spy).to.have.been.calledWith(item)
it 'should throw an error if log function is missing', ->
func = _.partial(log.array, array)
expect(func).to.throw(Error)
it 'should throw an error if log function is not a function', ->
for input in [
undefined
null
123
'Hello World'
[ 1, 2, 3 ]
{ hello: 'world' }
]
func = _.partial(log.array, 'Hello', input)
expect(func).to.throw(Error)
it 'should call log function once if input is not an array', ->
for input in [
'Hello World'
{ hello: 'world' }
1234
]
spy = sinon.spy()
log.array(input, spy)
expect(spy).to.have.been.calledOnce
expect(spy).to.have.been.calledWith(input)
it 'should not call log function if input is undefined/null', ->
for input in [
undefined
null
]
spy = sinon.spy()
log.array(input, spy)
expect(spy).to.not.have.been.called
2014-11-21 15:22:26 +00:00
describe '#setQuiet()', ->
it 'should set the quietness', ->
expect(log.isQuiet()).to.be.false
log.setQuiet(true)
expect(log.isQuiet()).to.be.true
describe '#isQuiet()', ->
it 'should return false by default', ->
expect(log.isQuiet()).to.be.false
describe 'if quiet is true', ->
beforeEach ->
log.setQuiet(true)
describe '#error()', ->
it 'should still output to console.error', ->
testConsoleFunctionBeingCalled('error', 'error', MESSAGE.empty)
describe '#warning()', ->
it 'should still output to console.warn', ->
testConsoleFunctionBeingCalled('warning', 'warn', MESSAGE.empty)
describe '#info()', ->
it 'should not call console.info', ->
testConsoleFunctionNotBeingCalled('info', 'info', MESSAGE.empty)
describe '#out()', ->
it 'should not call console.log', ->
testConsoleFunctionBeingCalled('out', 'log', MESSAGE.empty)