From bd8df1f7042997f628dfad44b893709e91dc30b2 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 7 Jun 2017 11:34:06 +0900 Subject: [PATCH 1/7] Modify `_mockSetting` returns `AWS` --- test/main.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/main.js b/test/main.js index 6b9bb454..1b78160e 100644 --- a/test/main.js +++ b/test/main.js @@ -95,6 +95,8 @@ const _mockSetting = () => { callback(null, lambdaMockSettings[method]) }) }) + + return require('aws-sdk') } /* global before, after, beforeEach, afterEach, describe, it */ @@ -841,9 +843,7 @@ describe('lib/main', function () { 'event_sources.json', JSON.stringify(eventSourcesJsonValue) ) - _mockSetting() - - awsLambda = new (require('aws-sdk')).Lambda({ + awsLambda = new (_mockSetting()).Lambda({ apiVersion: '2015-03-31' }) }) @@ -936,8 +936,8 @@ describe('lib/main', function () { JSON.stringify(eventSourcesJsonValue) ) - _mockSetting() - schedule = new ScheduleEvents(require('aws-sdk')) + const aws = _mockSetting() + schedule = new ScheduleEvents(aws) }) after(() => { From 7f0365d90cc2afb1a88e8711adcdb278b65d5fda Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 7 Jun 2017 11:35:05 +0900 Subject: [PATCH 2/7] Add test for _uploadNew --- test/main.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/main.js b/test/main.js index 1b78160e..ed871a95 100644 --- a/test/main.js +++ b/test/main.js @@ -985,6 +985,29 @@ describe('lib/main', function () { }) }) + describe('_uploadNew', () => { + let awsLambda = null + + before(() => { + awsLambda = new (_mockSetting()).Lambda({ + apiVersion: '2015-03-31' + }) + }) + after(() => { + awsMock.restore('CloudWatchEvents') + awsMock.restore('Lambda') + }) + + it('simple test with mock', (done) => { + const params = lambda._params(program, null) + lambda._uploadNew(awsLambda, params, (err, results) => { + assert.isNull(err) + assert.deepEqual(results, lambdaMockSettings.createFunction) + done() + }) + }) + }) + describe('check env vars before create sample files', function () { const filesCreatedBySetup = [ '.env', From ea74cc4ecf74cfea59e99f21e03e6fba5d8ebd93 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 7 Jun 2017 11:38:22 +0900 Subject: [PATCH 3/7] Add linefeed --- test/main.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/main.js b/test/main.js index ed871a95..bdc73b69 100644 --- a/test/main.js +++ b/test/main.js @@ -993,6 +993,7 @@ describe('lib/main', function () { apiVersion: '2015-03-31' }) }) + after(() => { awsMock.restore('CloudWatchEvents') awsMock.restore('Lambda') From 771340277abbd8ff2d54fae1742bd84fb8e273bf Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 7 Jun 2017 12:00:40 +0900 Subject: [PATCH 4/7] Add _awsRestore() this will restore all the methods and services --- test/main.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/main.js b/test/main.js index bdc73b69..ab13265d 100644 --- a/test/main.js +++ b/test/main.js @@ -99,6 +99,11 @@ const _mockSetting = () => { return require('aws-sdk') } +const _awsRestore = () => { + awsMock.restore('CloudWatchEvents') + awsMock.restore('Lambda') +} + /* global before, after, beforeEach, afterEach, describe, it */ describe('lib/main', function () { if (process.platform === 'win32') { From 85bd6dd3798cd7a365f06079215d49e4825d04c2 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 7 Jun 2017 12:02:14 +0900 Subject: [PATCH 5/7] Modifies that commonize the same process --- test/main.js | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/test/main.js b/test/main.js index ab13265d..0d00b122 100644 --- a/test/main.js +++ b/test/main.js @@ -112,6 +112,14 @@ describe('lib/main', function () { this.timeout(60000) } + let aws = null // mock + let awsLambda = null // mock + before(() => { + aws = _mockSetting() + awsLambda = new aws.Lambda({ apiVersion: '2015-03-31' }) + }) + after(() => _awsRestore()) + beforeEach(function () { program = Hoek.clone(originalProgram) }) @@ -841,23 +849,14 @@ describe('lib/main', function () { }] } - let awsLambda = null - before(() => { fs.writeFileSync( 'event_sources.json', JSON.stringify(eventSourcesJsonValue) ) - awsLambda = new (_mockSetting()).Lambda({ - apiVersion: '2015-03-31' - }) }) - after(() => { - fs.unlinkSync('event_sources.json') - awsMock.restore('CloudWatchEvents') - awsMock.restore('Lambda') - }) + after(() => fs.unlinkSync('event_sources.json')) it('program.eventSourceFile is empty value', (done) => { program.eventSourceFile = '' @@ -940,16 +939,10 @@ describe('lib/main', function () { 'event_sources.json', JSON.stringify(eventSourcesJsonValue) ) - - const aws = _mockSetting() schedule = new ScheduleEvents(aws) }) - after(() => { - fs.unlinkSync('event_sources.json') - awsMock.restore('CloudWatchEvents') - awsMock.restore('Lambda') - }) + after(() => fs.unlinkSync('event_sources.json')) it('program.eventSourceFile is empty value', (done) => { program.eventSourceFile = '' @@ -991,19 +984,6 @@ describe('lib/main', function () { }) describe('_uploadNew', () => { - let awsLambda = null - - before(() => { - awsLambda = new (_mockSetting()).Lambda({ - apiVersion: '2015-03-31' - }) - }) - - after(() => { - awsMock.restore('CloudWatchEvents') - awsMock.restore('Lambda') - }) - it('simple test with mock', (done) => { const params = lambda._params(program, null) lambda._uploadNew(awsLambda, params, (err, results) => { From 97796d2b639a59cbefe30200d6b6f9ef9d31f5fb Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 7 Jun 2017 12:07:08 +0900 Subject: [PATCH 6/7] Add test fro _uploadExisting --- test/main.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/main.js b/test/main.js index 0d00b122..a34a235f 100644 --- a/test/main.js +++ b/test/main.js @@ -994,6 +994,17 @@ describe('lib/main', function () { }) }) + describe('_uploadExisting', () => { + it('simple test with mock', (done) => { + const params = lambda._params(program, null) + lambda._uploadExisting(awsLambda, params, (err, results) => { + assert.isNull(err) + assert.deepEqual(results, lambdaMockSettings.updateFunctionConfiguration) + done() + }) + }) + }) + describe('check env vars before create sample files', function () { const filesCreatedBySetup = [ '.env', From 38ee0d579dbfb31316beb8f46e22928ec23c0f33 Mon Sep 17 00:00:00 2001 From: abetomo Date: Wed, 7 Jun 2017 12:12:46 +0900 Subject: [PATCH 7/7] Refactoring Modify mainly to Arrow function --- lib/main.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/main.js b/lib/main.js index fe6551ec..4bbacb9d 100644 --- a/lib/main.js +++ b/lib/main.js @@ -420,12 +420,12 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) { } } -Lambda.prototype._uploadExisting = function (lambda, params, cb) { - var request = lambda.updateFunctionCode({ +Lambda.prototype._uploadExisting = (lambda, params, cb) => { + const request = lambda.updateFunctionCode({ 'FunctionName': params.FunctionName, 'ZipFile': params.Code.ZipFile, 'Publish': params.Publish - }, function (err, data) { + }, (err, data) => { if (err) { return cb(err, data) } @@ -442,12 +442,12 @@ Lambda.prototype._uploadExisting = function (lambda, params, cb) { 'Environment': params.Environment, 'DeadLetterConfig': params.DeadLetterConfig, 'TracingConfig': params.TracingConfig - }, function (err, data) { + }, (err, data) => { return cb(err, data) }) }) - request.on('retry', function (response) { + request.on('retry', (response) => { console.log(response.error.message) console.log('=> Retrying') }) @@ -455,12 +455,9 @@ Lambda.prototype._uploadExisting = function (lambda, params, cb) { return request } -Lambda.prototype._uploadNew = function (lambda, params, cb) { - var request = lambda.createFunction(params, function (err, data) { - return cb(err, data) - }) - - request.on('retry', function (response) { +Lambda.prototype._uploadNew = (lambda, params, cb) => { + const request = lambda.createFunction(params, (err, data) => cb(err, data)) + request.on('retry', (response) => { console.log(response.error.message) console.log('=> Retrying') })