Skip to content
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

Merged
merged 7 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/cli/domain/get-mocked-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ var registerStaticMocks = function(mocks, logger){
});
};

var registerDynamicMocks = function(mocks, logger){
var registerDynamicMocks = function(ocJsonLocation, mocks, logger){
return _.map(mocks, function(source, pluginName){

var p;
try {
p = require(path.resolve(source));
p = require(path.resolve(ocJsonLocation, source));
} catch(er) {
logger.log(colors.red(er.toString()));
return;
Expand All @@ -53,23 +54,47 @@ var registerDynamicMocks = function(mocks, logger){
}).filter(function(p){ return p; });
};

module.exports = function(logger){
var findPath = function(pathToResolve, fileName) {

var rootDir = fs.realpathSync('.'),
fileToResolve = path.join(pathToResolve, fileName);

if (!fs.existsSync(fileToResolve)) {
if (pathToResolve === rootDir) {
return undefined;
} else {
var getParent = function(x){ return x.split('/').slice(0, -1).join('/'); },
parentDir = pathToResolve ? getParent(pathToResolve) : rootDir;

return findPath(parentDir, fileName);
}
}

return fileToResolve;
};

module.exports = function(logger, componentsDir){
componentsDir = path.resolve(componentsDir || '.');
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct.


var plugins = [],
ocJsonPath = path.resolve(settings.configFile.src);
ocJsonFileName = settings.configFile.src.replace('./', ''),
ocJsonPath = findPath(componentsDir, ocJsonFileName);

if(!fs.existsSync(ocJsonPath)){
if(!ocJsonPath){
return plugins;
}

var content = fs.readJsonSync(ocJsonPath);
var content = fs.readJsonSync(ocJsonPath),
ocJsonLocation = ocJsonPath.slice(0, -ocJsonFileName.length);

if(!content.mocks || !content.mocks.plugins){
return plugins;
}

logger.log(colors.yellow(strings.messages.cli.REGISTERING_MOCKED_PLUGINS));

plugins = plugins.concat(registerStaticMocks(content.mocks.plugins.static, logger));
plugins = plugins.concat(registerDynamicMocks(content.mocks.plugins.dynamic, logger));
plugins = plugins.concat(registerDynamicMocks(ocJsonLocation, content.mocks.plugins.dynamic,logger));

return plugins;
};
2 changes: 1 addition & 1 deletion src/cli/facade/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module.exports = function(dependencies){
};

var registerPlugins = function(registry){
var mockedPlugins = getMockedPlugins(logger);
var mockedPlugins = getMockedPlugins(logger, componentsDir);

mockedPlugins.forEach(function(p){
registry.register(p);
Expand Down
172 changes: 139 additions & 33 deletions test/unit/cli-domain-get-mocked-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/');
Copy link
Member

Choose a reason for hiding this comment

The 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 getMockedPlugins method; I think we should have tests w/o the second parameter as well what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do it.

});

it('should return an empty array', function(){
Expand All @@ -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(){
Expand All @@ -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(){
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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(){
Expand Down