-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recursively look for oc.json, starting from componentsDir #313
Changes from all commits
8b02b50
f514abc
9c01a0c
15d52ef
12e11ab
26e370b
5148fac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,45 +2,151 @@ | |
|
||
var expect = require('chai').expect; | ||
var injectr = require('injectr'); | ||
var path = require('path'); | ||
var sinon = require('sinon'); | ||
var _ = require('underscore'); | ||
var dynamicPluginModule = function(a){ return a ? 'blarg' : 'flarg'; }; | ||
var notAFunctionModule = { 'foo' : 'bar' }; | ||
|
||
var fsMock, | ||
getMockedPlugins; | ||
|
||
var initialise = function(fs){ | ||
|
||
fsMock = _.extend({ | ||
existsSync: sinon.stub().returns(true), | ||
readFileSync: sinon.stub().returns('file content'), | ||
readJsonSync: sinon.stub().returns({ content: true }), | ||
writeFile: sinon.stub().yields(null, 'ok') | ||
}, fs || {}); | ||
|
||
getMockedPlugins = injectr('../../src/cli/domain/get-mocked-plugins.js', { | ||
'fs-extra': fsMock, | ||
path: { | ||
resolve: function(){ | ||
return _.toArray(arguments).join('/'); | ||
} | ||
}, | ||
'./dynamic-plugin.js': dynamicPluginModule, | ||
'./not-a-function.js': notAFunctionModule | ||
}); | ||
}; | ||
|
||
describe('cli : domain : get-mocked-plugins', function(){ | ||
|
||
var dynamicPluginModule = function(a){ return a ? 'blarg' : 'flarg'; }, | ||
notAFunctionModule = { 'foo' : 'bar' }, | ||
fsMock, | ||
getMockedPlugins; | ||
|
||
var initialise = function(fs, pathJoinStub){ | ||
|
||
fsMock = _.extend({ | ||
existsSync: sinon.stub().returns(true), | ||
readFileSync: sinon.stub().returns('file content'), | ||
readJsonSync: sinon.stub().returns({ content: true }), | ||
realpathSync: sinon.stub().returns('/root/'), | ||
writeFile: sinon.stub().yields(null, 'ok') | ||
}, fs || {}); | ||
|
||
var fakePathFunc = function(){ | ||
return _.toArray(arguments) | ||
.map(function(x){ | ||
return x.replace(/\.\//g, ''); | ||
}) | ||
.join(''); | ||
}; | ||
|
||
getMockedPlugins = injectr('../../src/cli/domain/get-mocked-plugins.js', { | ||
'fs-extra': fsMock, | ||
path: { | ||
join: pathJoinStub || fakePathFunc, | ||
resolve: fakePathFunc | ||
}, | ||
'/root/components/dynamic-plugin.js': dynamicPluginModule, | ||
'/root/components/not-a-function.js': notAFunctionModule | ||
}); | ||
}; | ||
|
||
describe('when setting up mocked plugins', function(){ | ||
|
||
describe('when componentsDir parameter is undefined', function(){ | ||
|
||
var joinStub = sinon.stub(); | ||
|
||
beforeEach(function(){ | ||
initialise({}, joinStub); | ||
getMockedPlugins({ log: _.noop }, undefined); | ||
}); | ||
|
||
it('should use . as default', function(){ | ||
expect(joinStub.args[0][0]).to.equal('.'); | ||
}); | ||
}); | ||
|
||
describe('when componentsDir parameter is omitted', function(){ | ||
|
||
var joinStub = sinon.stub(); | ||
|
||
beforeEach(function(){ | ||
initialise({}, joinStub); | ||
getMockedPlugins({ log: _.noop }); | ||
}); | ||
|
||
it('should use . as default', function(){ | ||
expect(joinStub.args[0][0]).to.equal('.'); | ||
}); | ||
}); | ||
|
||
describe('when oc.json is in both root and component folder', function(){ | ||
|
||
var result; | ||
var ocJsonComponent = { | ||
registries: [], | ||
mocks: { | ||
plugins: { | ||
static: { foo: 1, bar: 2 } | ||
} | ||
} | ||
}; | ||
|
||
var readMock = sinon.stub().returns(ocJsonComponent); | ||
|
||
beforeEach(function(){ | ||
initialise({ | ||
existsSync: sinon.stub().returns(true), | ||
readJsonSync: readMock | ||
}); | ||
result = getMockedPlugins({ log: _.noop }, '/root/components/'); | ||
}); | ||
|
||
it('should use components folder oc.json as default', function(){ | ||
expect(readMock.calledOnce).to.be.true; | ||
expect(readMock.args[0][0]).to.equal('/root/components/oc.json'); | ||
expect(result.length).to.equal(2); | ||
}); | ||
}); | ||
|
||
describe('when oc.json is in root folder', function(){ | ||
|
||
var result; | ||
var ocJsonComponent = { | ||
registries: [], | ||
mocks: { | ||
plugins: { | ||
static: { foo: 1, bar: 2 } | ||
} | ||
} | ||
}; | ||
var ocJsonRoot = { | ||
registries: [], | ||
mocks: { | ||
plugins: { | ||
static: { foo: 1, bar: 2, baz: 3 } | ||
} | ||
} | ||
}; | ||
|
||
var readMock = sinon.stub(), | ||
existsMock = sinon.stub(); | ||
|
||
readMock.withArgs('/root/components/oc.json').returns(ocJsonComponent); | ||
readMock.withArgs('/root/oc.json').returns(ocJsonRoot); | ||
|
||
existsMock.withArgs('/root/components/oc.json').returns(false); | ||
existsMock.withArgs('/root/oc.json').returns(true); | ||
|
||
beforeEach(function(){ | ||
initialise({ | ||
existsSync: existsMock, | ||
readJsonSync: readMock | ||
}); | ||
result = getMockedPlugins({ log: _.noop }, '/root/components/'); | ||
}); | ||
|
||
it('should use root oc.json', function(){ | ||
expect(result.length).to.equal(3); | ||
}); | ||
}); | ||
|
||
describe('when oc.json is missing', function(){ | ||
var result; | ||
beforeEach(function(){ | ||
initialise({ existsSync: sinon.stub().returns(false) }); | ||
result = getMockedPlugins(console); | ||
result = getMockedPlugins(console, '/root/components/'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that now all the tests are passing the path parameter to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can do it. |
||
}); | ||
|
||
it('should return an empty array', function(){ | ||
|
@@ -62,7 +168,7 @@ describe('cli : domain : get-mocked-plugins', function(){ | |
existsSync: sinon.stub().returns(true), | ||
readJsonSync: sinon.stub().returns(ocJson) | ||
}); | ||
result = getMockedPlugins({log: sinon.stub()}); | ||
result = getMockedPlugins({log: sinon.stub()}, '/root/components/'); | ||
}); | ||
|
||
it('should return an empty array', function(){ | ||
|
@@ -88,7 +194,7 @@ describe('cli : domain : get-mocked-plugins', function(){ | |
existsSync: sinon.stub().returns(true), | ||
readJsonSync: sinon.stub().returns(ocJson) | ||
}); | ||
result = getMockedPlugins({log: sinon.stub()}); | ||
result = getMockedPlugins({log: sinon.stub()}, '/root/components/'); | ||
}); | ||
|
||
it('should return the static plugin', function(){ | ||
|
@@ -119,7 +225,7 @@ describe('cli : domain : get-mocked-plugins', function(){ | |
existsSync: sinon.stub().returns(true), | ||
readJsonSync: sinon.stub().returns(ocJson) | ||
}); | ||
result = getMockedPlugins({ log: sinon.stub() }); | ||
result = getMockedPlugins({ log: sinon.stub() }, '/root/components/'); | ||
}); | ||
|
||
it('should return the dynamic plugin', function(){ | ||
|
@@ -153,7 +259,7 @@ describe('cli : domain : get-mocked-plugins', function(){ | |
existsSync: sinon.stub().returns(true), | ||
readJsonSync: sinon.stub().returns(ocJson) | ||
}); | ||
result = getMockedPlugins(logger); | ||
result = getMockedPlugins(logger, '/root/components/'); | ||
}); | ||
|
||
it('should log an error', function(){ | ||
|
@@ -186,7 +292,7 @@ describe('cli : domain : get-mocked-plugins', function(){ | |
existsSync: sinon.stub().returns(true), | ||
readJsonSync: sinon.stub().returns(ocJson) | ||
}); | ||
result = getMockedPlugins(logger); | ||
result = getMockedPlugins(logger, '/root/components/'); | ||
}); | ||
|
||
it('should log an error', function(){ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so if you do not pass the
componentsDir
parameter you set it to '.
correct?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
correct.