From 8b02b50e3212239dbca12a0f1658e3dbce4d0441 Mon Sep 17 00:00:00 2001 From: Tom Martin Date: Fri, 15 Apr 2016 17:45:25 +0100 Subject: [PATCH 1/5] Recursively look for oc.json, starting from componentsDir --- cli/domain/get-mocked-plugins.js | 31 ++++++++++++++++++++++++------- cli/facade/dev.js | 2 +- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/cli/domain/get-mocked-plugins.js b/cli/domain/get-mocked-plugins.js index 63d315ea6..2544b7800 100644 --- a/cli/domain/get-mocked-plugins.js +++ b/cli/domain/get-mocked-plugins.js @@ -1,7 +1,8 @@ 'use strict'; var colors = require('colors/safe'); -var fs = require('fs-extra'); +var fsExtra = require('fs-extra'); +var fs = require('fs'); var path = require('path'); var _ = require('underscore'); @@ -53,15 +54,31 @@ var registerDynamicMocks = function(mocks, logger){ }).filter(function(p){ return p; }); }; -module.exports = function(logger){ - var plugins = [], - ocJsonPath = path.resolve(settings.configFile.src); +var findOcJsonPath = function (plugins, pathToResolve) { - if(!fs.existsSync(ocJsonPath)){ - return plugins; + var parentDir, + rootDir = fs.realpathSync('.') + '/oc.json'; + + if (!fsExtra.existsSync(pathToResolve)) { + if (pathToResolve === rootDir) { + return plugins; + } else { + parentDir = pathToResolve ? pathToResolve.split('/').slice(0,-2).join('/') + '/oc.json' : rootDir; + return findOcJsonPath(parentDir); + } + } else { + return pathToResolve; } - var content = fs.readJsonSync(ocJsonPath); +}; + +module.exports = function(logger, componentsDir){ + var plugins = [], + componentsDir = componentsDir || '', + componentsPath = path.resolve(componentsDir, settings.configFile.src), + ocJsonPath = findOcJsonPath(plugins, componentsPath); + + var content = fsExtra.readJsonSync(ocJsonPath); if(!content.mocks || !content.mocks.plugins){ return plugins; } diff --git a/cli/facade/dev.js b/cli/facade/dev.js index d18519711..177b43b16 100644 --- a/cli/facade/dev.js +++ b/cli/facade/dev.js @@ -109,7 +109,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); From 9c01a0ce29330262b3be989113e67cd2b7e1900b Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Fri, 21 Oct 2016 12:26:00 +0100 Subject: [PATCH 2/5] Merge and cleanup --- src/cli/domain/get-mocked-plugins.js | 35 +++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/cli/domain/get-mocked-plugins.js b/src/cli/domain/get-mocked-plugins.js index 2544b7800..93e1e00e5 100644 --- a/src/cli/domain/get-mocked-plugins.js +++ b/src/cli/domain/get-mocked-plugins.js @@ -1,8 +1,7 @@ 'use strict'; var colors = require('colors/safe'); -var fsExtra = require('fs-extra'); -var fs = require('fs'); +var fs = require('fs-extra'); var path = require('path'); var _ = require('underscore'); @@ -54,31 +53,35 @@ var registerDynamicMocks = function(mocks, logger){ }).filter(function(p){ return p; }); }; -var findOcJsonPath = function (plugins, pathToResolve) { +var findPath = function(pathToResolve, fileName) { - var parentDir, - rootDir = fs.realpathSync('.') + '/oc.json'; + var rootDir = fs.realpathSync('.'), + fileToResolve = path.join(pathToResolve, fileName); - if (!fsExtra.existsSync(pathToResolve)) { + if (!fs.existsSync(fileToResolve)) { if (pathToResolve === rootDir) { - return plugins; + return undefined; } else { - parentDir = pathToResolve ? pathToResolve.split('/').slice(0,-2).join('/') + '/oc.json' : rootDir; - return findOcJsonPath(parentDir); + var getParent = function(x){ return x.split('/').slice(0, -1).join('/'); }, + parentDir = pathToResolve ? getParent(pathToResolve) : rootDir; + + return findPath(parentDir, fileName); } - } else { - return pathToResolve; } - + + return fileToResolve; }; module.exports = function(logger, componentsDir){ var plugins = [], - componentsDir = componentsDir || '', - componentsPath = path.resolve(componentsDir, settings.configFile.src), - ocJsonPath = findOcJsonPath(plugins, componentsPath); + componentsDir = path.resolve(componentsDir || ''), + ocJsonPath = findPath(componentsDir, settings.configFile.src.replace('./', '')); + + if(!ocJsonPath){ + return plugins; + } - var content = fsExtra.readJsonSync(ocJsonPath); + var content = fs.readJsonSync(ocJsonPath); if(!content.mocks || !content.mocks.plugins){ return plugins; } From 12e11ab91055a4ffa880c7f465bd61a2022c2907 Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Fri, 21 Oct 2016 15:03:09 +0100 Subject: [PATCH 3/5] Fixing mocked plugins location --- src/cli/domain/get-mocked-plugins.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/cli/domain/get-mocked-plugins.js b/src/cli/domain/get-mocked-plugins.js index 93e1e00e5..92b655734 100644 --- a/src/cli/domain/get-mocked-plugins.js +++ b/src/cli/domain/get-mocked-plugins.js @@ -25,11 +25,12 @@ var registerStaticMocks = function(mocks, logger){ }); }; -var registerDynamicMocks = function(mocks, logger){ +var registerDynamicMocks = function(mocks, ocJsonPath, logger){ return _.map(mocks, function(source, pluginName){ + var p; try { - p = require(path.resolve(source)); + p = require(path.resolve(ocJsonPath, source)); } catch(er) { logger.log(colors.red(er.toString())); return; @@ -75,13 +76,16 @@ var findPath = function(pathToResolve, fileName) { module.exports = function(logger, componentsDir){ var plugins = [], componentsDir = path.resolve(componentsDir || ''), - ocJsonPath = findPath(componentsDir, settings.configFile.src.replace('./', '')); + ocJsonFileName = settings.configFile.src.replace('./', ''), + ocJsonPath = findPath(componentsDir, ocJsonFileName); 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; } @@ -89,7 +93,7 @@ module.exports = function(logger, componentsDir){ 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(content.mocks.plugins.dynamic, ocJsonLocation, logger)); return plugins; }; From 26e370bb8dfa9e54d018589a62601efba57304c3 Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Fri, 21 Oct 2016 16:09:35 +0100 Subject: [PATCH 4/5] Added unit test --- src/cli/domain/get-mocked-plugins.js | 5 +- test/unit/cli-domain-get-mocked-plugins.js | 144 ++++++++++++++++----- 2 files changed, 114 insertions(+), 35 deletions(-) diff --git a/src/cli/domain/get-mocked-plugins.js b/src/cli/domain/get-mocked-plugins.js index 92b655734..3a1009b83 100644 --- a/src/cli/domain/get-mocked-plugins.js +++ b/src/cli/domain/get-mocked-plugins.js @@ -27,7 +27,7 @@ var registerStaticMocks = function(mocks, logger){ var registerDynamicMocks = function(mocks, ocJsonPath, logger){ return _.map(mocks, function(source, pluginName){ - + var p; try { p = require(path.resolve(ocJsonPath, source)); @@ -74,8 +74,9 @@ var findPath = function(pathToResolve, fileName) { }; module.exports = function(logger, componentsDir){ + componentsDir = path.resolve(componentsDir || '.'); + var plugins = [], - componentsDir = path.resolve(componentsDir || ''), ocJsonFileName = settings.configFile.src.replace('./', ''), ocJsonPath = findPath(componentsDir, ocJsonFileName); diff --git a/test/unit/cli-domain-get-mocked-plugins.js b/test/unit/cli-domain-get-mocked-plugins.js index df637c428..07218b665 100644 --- a/test/unit/cli-domain-get-mocked-plugins.js +++ b/test/unit/cli-domain-get-mocked-plugins.js @@ -2,45 +2,123 @@ 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){ + + 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: 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 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/'); }); it('should return an empty array', function(){ @@ -62,7 +140,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 +166,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 +197,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 +231,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 +264,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(){ From 5148facd3c0001389adba6e378a96159d6efa565 Mon Sep 17 00:00:00 2001 From: Matteo Figus Date: Mon, 24 Oct 2016 14:27:23 +0100 Subject: [PATCH 5/5] Requested changes --- src/cli/domain/get-mocked-plugins.js | 6 ++-- test/unit/cli-domain-get-mocked-plugins.js | 32 ++++++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/cli/domain/get-mocked-plugins.js b/src/cli/domain/get-mocked-plugins.js index 3a1009b83..d12005d86 100644 --- a/src/cli/domain/get-mocked-plugins.js +++ b/src/cli/domain/get-mocked-plugins.js @@ -25,12 +25,12 @@ var registerStaticMocks = function(mocks, logger){ }); }; -var registerDynamicMocks = function(mocks, ocJsonPath, logger){ +var registerDynamicMocks = function(ocJsonLocation, mocks, logger){ return _.map(mocks, function(source, pluginName){ var p; try { - p = require(path.resolve(ocJsonPath, source)); + p = require(path.resolve(ocJsonLocation, source)); } catch(er) { logger.log(colors.red(er.toString())); return; @@ -94,7 +94,7 @@ module.exports = function(logger, componentsDir){ 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, ocJsonLocation, logger)); + plugins = plugins.concat(registerDynamicMocks(ocJsonLocation, content.mocks.plugins.dynamic,logger)); return plugins; }; diff --git a/test/unit/cli-domain-get-mocked-plugins.js b/test/unit/cli-domain-get-mocked-plugins.js index 07218b665..1c6b962ce 100644 --- a/test/unit/cli-domain-get-mocked-plugins.js +++ b/test/unit/cli-domain-get-mocked-plugins.js @@ -12,7 +12,7 @@ describe('cli : domain : get-mocked-plugins', function(){ fsMock, getMockedPlugins; - var initialise = function(fs){ + var initialise = function(fs, pathJoinStub){ fsMock = _.extend({ existsSync: sinon.stub().returns(true), @@ -33,7 +33,7 @@ describe('cli : domain : get-mocked-plugins', function(){ getMockedPlugins = injectr('../../src/cli/domain/get-mocked-plugins.js', { 'fs-extra': fsMock, path: { - join: fakePathFunc, + join: pathJoinStub || fakePathFunc, resolve: fakePathFunc }, '/root/components/dynamic-plugin.js': dynamicPluginModule, @@ -43,6 +43,34 @@ describe('cli : domain : get-mocked-plugins', function(){ 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;