From 5b2efb0753ad61abf6277fd5b6abeedb4c299087 Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Thu, 20 Apr 2017 13:12:54 +0200 Subject: [PATCH 1/3] Control command flow for deploy at framework level. --- lib/plugins/aws/deploy/index.js | 4 +--- lib/plugins/deploy/deploy.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/plugins/aws/deploy/index.js b/lib/plugins/aws/deploy/index.js index 53cd0ca7d..5a9e91331 100644 --- a/lib/plugins/aws/deploy/index.js +++ b/lib/plugins/aws/deploy/index.js @@ -69,9 +69,7 @@ class AwsDeploy { .then(() => this.serverless.pluginManager.spawn('aws:common:validate')) .then(() => { if (!this.options.package && !this.serverless.service.package.path) { - return BbPromise.bind(this) - .then(() => this.serverless.pluginManager.spawn('package')) - .then(this.extendedValidate); + return this.extendedValidate(); } return BbPromise.bind(this) .then(() => this.serverless.pluginManager.spawn('aws:common:moveArtifactsToTemp')) diff --git a/lib/plugins/deploy/deploy.js b/lib/plugins/deploy/deploy.js index 8dd22dd69..8cf748024 100644 --- a/lib/plugins/deploy/deploy.js +++ b/lib/plugins/deploy/deploy.js @@ -1,5 +1,7 @@ 'use strict'; +const BbPromise = require('bluebird'); + class Deploy { constructor(serverless) { this.serverless = serverless; @@ -68,6 +70,16 @@ class Deploy { }, }, }; + + this.hooks = { + 'before:deploy:deploy': () => BbPromise.bind(this) + .then(() => { + if (!this.options.package && !this.serverless.service.package.path) { + return this.serverless.pluginManager.spawn('package'); + } + return BbPromise.resolve(); + }), + }; } } From 8cabda3a18bdbc065a987d1b28c5cb1728ea3de3 Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Thu, 20 Apr 2017 13:40:16 +0200 Subject: [PATCH 2/3] Added missing map options to this.options --- lib/plugins/deploy/deploy.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/plugins/deploy/deploy.js b/lib/plugins/deploy/deploy.js index 8cf748024..1313a010a 100644 --- a/lib/plugins/deploy/deploy.js +++ b/lib/plugins/deploy/deploy.js @@ -3,8 +3,9 @@ const BbPromise = require('bluebird'); class Deploy { - constructor(serverless) { + constructor(serverless, options) { this.serverless = serverless; + this.options = options || {}; this.commands = { deploy: { From c74d9c6ddaa99755af5cfee0b1b1240b4a3894d2 Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Thu, 20 Apr 2017 14:30:55 +0200 Subject: [PATCH 3/3] Fixed unit tests and got coverage to 100% again --- lib/plugins/aws/deploy/index.test.js | 3 +- lib/plugins/deploy/deploy.test.js | 53 ++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/plugins/aws/deploy/index.test.js b/lib/plugins/aws/deploy/index.test.js index 598f9d21a..56a1d1428 100644 --- a/lib/plugins/aws/deploy/index.test.js +++ b/lib/plugins/aws/deploy/index.test.js @@ -126,8 +126,7 @@ describe('AwsDeploy', () => { return awsDeploy.hooks['before:deploy:deploy']().then(() => { expect(spawnAwsCommonValidateStub.calledOnce).to.equal(true); - expect(spawnPackageStub.calledAfter(spawnAwsCommonValidateStub)).to.equal(true); - expect(extendedValidateStub.calledAfter(spawnPackageStub)).to.equal(true); + expect(extendedValidateStub.calledAfter(spawnAwsCommonValidateStub)).to.equal(true); expect(spawnAwsCommonMoveArtifactsToTemp.calledOnce).to.equal(false); }); }); diff --git a/lib/plugins/deploy/deploy.test.js b/lib/plugins/deploy/deploy.test.js index d6a1aff94..afd8d376f 100644 --- a/lib/plugins/deploy/deploy.test.js +++ b/lib/plugins/deploy/deploy.test.js @@ -3,18 +3,67 @@ const expect = require('chai').expect; const Deploy = require('./deploy'); const Serverless = require('../../Serverless'); - +const sinon = require('sinon'); describe('Deploy', () => { let deploy; let serverless; + let options; beforeEach(() => { serverless = new Serverless(); - deploy = new Deploy(serverless); + options = {}; + deploy = new Deploy(serverless, options); }); describe('#constructor()', () => { it('should have commands', () => expect(deploy.commands).to.be.not.empty); + it('should have hooks', () => expect(deploy.hooks).to.be.not.empty); + it('should work without options', () => { + const noOptionDeploy = new Deploy(serverless); + expect(noOptionDeploy).to.have.property('options').to.be.eql({}); + }); + }); + + describe('"before:deploy:deploy" hook', () => { + let spawnStub; + let spawnPackageStub; + + beforeEach(() => { + spawnStub = sinon + .stub(serverless.pluginManager, 'spawn'); + spawnPackageStub = spawnStub.withArgs('package').resolves(); + }); + + afterEach(() => { + serverless.pluginManager.spawn.restore(); + }); + + it('should resolve if the package option is set', () => { + deploy.options.package = false; + deploy.serverless.service.package.path = 'some_path'; + + return deploy.hooks['before:deploy:deploy']().then(() => { + expect(spawnPackageStub.called).to.equal(false); + }); + }); + + it('should resolve if the service package path is set', () => { + deploy.options.package = 'some_path'; + deploy.serverless.service.package.path = false; + + return deploy.hooks['before:deploy:deploy']().then(() => { + expect(spawnPackageStub.called).to.equal(false); + }); + }); + + it('should use the default packaging mechanism if no packaging config is provided', () => { + deploy.options.package = false; + deploy.serverless.service.package.path = false; + + return deploy.hooks['before:deploy:deploy']().then(() => { + expect(spawnPackageStub.calledOnce).to.equal(true); + }); + }); }); });