task-master
Version:
A helper to make Grunt task declaration and organization cleaner.
104 lines (89 loc) • 4.2 kB
text/coffeescript
describe 'loader', ->
Given -> = spyObj 'load'
Given -> = spyObj 'readFileSync'
Given -> = spyObj 'sync'
Given -> = -> mango: true
Given -> = (foo) -> elderberry: foo
Given -> = sandbox '../lib/loader',
glob:
yamljs:
fs:
'banana.js':
banana: true
'apple.coffee':
apple: true
'pear.json':
pear: true
'mango.js':
'elderberry.js':
describe '.get', ->
context '.js file', ->
When -> = .get 'banana.js'
Then -> expect().to.deep.equal
banana: true
context '.coffee file', ->
When -> = .get 'apple.coffee'
Then -> expect().to.deep.equal
apple: true
context '.json file', ->
When -> = .get 'pear.json'
Then -> expect().to.deep.equal
pear: true
context 'file does not exist', ->
Then -> expect(=> .get('serviceberry.coffee')).to.throw "Cannot find module 'serviceberry.coffee'"
context 'exports a function', ->
When -> = .get 'mango.js'
Then -> expect().to.deep.equal mango: true
context 'exports a function and arguments are provided', ->
When -> = .get 'elderberry.js', true
Then -> expect().to.deep.equal elderberry: true
context '.yml file', ->
Given -> .load.withArgs('dragonfruit.yml').returns dragonfruit: true
When -> = .get 'dragonfruit.yml'
Then -> expect().to.deep.equal dragonfruit: true
context 'something else', ->
Given -> .readFileSync.withArgs('kiwi.html', 'utf8').returns "<div>kiwi</div>"
When -> = .get 'kiwi.opts'
Then -> expect().to.deep.equal {}
describe '.getAll', ->
afterEach -> .get.restore()
Given -> sinon.stub , 'get'
Given -> .sync.withArgs('1', { cwd: '/root/tasks' }).returns ['foo']
Given -> .sync.withArgs('2', { cwd: '/root/tasks' }).returns ['bar', 'baz']
Given -> .get.withArgs('/root/tasks/foo').returns foo: 1
Given -> .get.withArgs('/root/tasks/bar').returns bar: 1
Given -> .get.withArgs('/root/tasks/baz').returns baz: 1
When -> = .getAll ['1', '2'], '/root', ['tasks']
Then -> expect().to.deep.equal
foo: 1
bar: 1
baz: 1
describe '.load', ->
afterEach -> .getAll.restore()
Given -> sinon.stub , 'getAll'
context 'value is an object', ->
Given -> .getAll.withArgs(['_taskmaster.name*.{js,coffee,json,yml}'], '/root', ['tasks']).returns baz: 'quux'
When -> = .load 'name', { foo: 'bar' }, '/root', ['tasks']
Then -> expect().to.deep.equal
foo: 'bar'
baz: 'quux'
context 'value is a string', ->
Given -> .getAll.withArgs(['_taskmaster.name*.{js,coffee,json,yml}'], '/root', ['tasks']).returns baz: 'quux'
Given -> .getAll.withArgs(['blah.js'], '/root', ['tasks']).returns foo: 'bar'
When -> = .load 'name', 'blah.js', '/root', ['tasks']
Then -> expect().to.deep.equal
foo: 'bar'
baz: 'quux'
context 'value is an array', ->
Given -> .getAll.withArgs(['_taskmaster.name*.{js,coffee,json,yml}'], '/root', ['tasks']).returns baz: 'quux'
Given -> .getAll.withArgs(['blah.js'], '/root', ['tasks']).returns foo: 'bar'
When -> = .load 'name', ['blah.js'], '/root', ['tasks']
Then -> expect().to.deep.equal
foo: 'bar'
baz: 'quux'
context 'non-canonical files take precedence', ->
Given -> .getAll.withArgs(['_taskmaster.name*.{js,coffee,json,yml}'], '/root', ['tasks']).returns foo: 'Zippy ate a banana'
Given -> .getAll.withArgs(['blah.js'], '/root', ['tasks']).returns foo: 'Toto ate grass in the backyard'
When -> = .load 'name', 'blah.js', '/root', ['tasks']
Then -> expect().to.deep.equal
foo: 'Toto ate grass in the backyard'