From 837faad9c634ebb7c9b00fb5926644a527912023 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 4 Aug 2017 17:43:35 +0900 Subject: [PATCH 1/3] Add function to enable / disable console.[log, warn, info] --- test/main.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/main.js b/test/main.js index f5782e3f..b5167428 100644 --- a/test/main.js +++ b/test/main.js @@ -113,6 +113,18 @@ const _awsRestore = () => { awsMock.restore('Lambda') } +const disableLog = () => { + ['log', 'warn', 'info'].forEach((f) => { + console[f] = () => {} + }) +} + +const enableLog = () => { + ['log', 'warn', 'info'].forEach((f) => { + if (String(console[f]) === '() => {}') delete console[f] + }) +} + /* global before, after, beforeEach, afterEach, describe, it */ describe('lib/main', function () { if (process.platform === 'win32') { From 370bd104efe51e5e71b28f2e399b80857d61d4af Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 4 Aug 2017 17:44:26 +0900 Subject: [PATCH 2/3] Remove meaningless test --- test/main.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/main.js b/test/main.js index b5167428..fa630772 100644 --- a/test/main.js +++ b/test/main.js @@ -527,9 +527,7 @@ describe('lib/main', function () { it('should throw any errors if script fails', () => { fs.writeFileSync(postInstallScriptPath, '___fails___') - return lambda._postInstallScript(program, codeDirectory).then((dummy) => { - assert.isUndefined(dummy) - }).catch((err) => { + return lambda._postInstallScript(program, codeDirectory).catch((err) => { assert.instanceOf(err, Error) assert.match(err.message, /^Error: Command failed:/) }) From bbaeae90ba3056db8563c76a3de50877bd928ccc Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 4 Aug 2017 17:44:58 +0900 Subject: [PATCH 3/3] Add invalidation of log output to make the test result easier to read --- test/main.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/main.js b/test/main.js index fa630772..bbb8bc75 100644 --- a/test/main.js +++ b/test/main.js @@ -520,14 +520,18 @@ describe('lib/main', function () { }) it('should not throw any errors if no script', () => { + disableLog() return lambda._postInstallScript(program, codeDirectory).then((dummy) => { + enableLog() assert.isUndefined(dummy) }) }) it('should throw any errors if script fails', () => { fs.writeFileSync(postInstallScriptPath, '___fails___') + disableLog() return lambda._postInstallScript(program, codeDirectory).catch((err) => { + enableLog() assert.instanceOf(err, Error) assert.match(err.message, /^Error: Command failed:/) }) @@ -573,7 +577,9 @@ describe('lib/main', function () { it('Compress the file. `index.js` and `bin/node-lambda` are included and the permission is also preserved.', function () { _timeout({ this: this, sec: 30 }) // give it time to zip + disableLog() return lambda._zip(program, codeDirectory).then((data) => { + enableLog() const archive = new Zip(data) assert.include(archive.files['index.js'].name, 'index.js') assert.include(archive.files['bin/node-lambda'].name, 'bin/node-lambda') @@ -607,7 +613,9 @@ describe('lib/main', function () { it('installs and zips with an index.js file and node_modules/aws-sdk (It is also a test of `_buildAndArchive`)', function () { _timeout({ this: this, sec: 30 }) // give it time to zip + disableLog() return lambda._archive(program).then((data) => { + enableLog() const archive = new Zip(data) const contents = Object.keys(archive.files).map((k) => { return archive.files[k].name.toString() @@ -630,7 +638,9 @@ describe('lib/main', function () { fs.writeFileSync(path.join(buildDir, 'd', 'testb'), '...') program.prebuiltDirectory = buildDir + disableLog() return lambda._archive(program).then((data) => { + enableLog() const archive = new Zip(data) const contents = Object.keys(archive.files).map((k) => { return archive.files[k].name.toString() @@ -652,7 +662,9 @@ describe('lib/main', function () { before(function () { _timeout({ this: this, sec: 30 }) // give it time to zip + disableLog() return lambda._zip(program, codeDirectory).then((data) => { + enableLog() bufferExpected = data fs.writeFileSync(testZipFile, data) }) @@ -692,7 +704,9 @@ describe('lib/main', function () { const filePath = path.join(path.resolve('/aaaa'), 'bbbb') const _program = Object.assign({ deployZipfile: filePath }, program) _timeout({ this: this, sec: 30 }) // give it time to zip + disableLog() return lambda._archive(_program).then((data) => { + enableLog() // same test as "installs and zips with an index.js file and node_modules/aws-sdk" const archive = new Zip(data) const contents = Object.keys(archive.files).map((k) => { @@ -746,7 +760,9 @@ describe('lib/main', function () { }) it('should create sample files', () => { + disableLog() lambda.setup(program) + enableLog() const libPath = path.join(__dirname, '..', 'lib') targetFiles.forEach((targetFile) => { @@ -1049,7 +1065,9 @@ describe('lib/main', function () { program.eventFile = 'newEvent.json' program.contextFile = 'newContext.json' + disableLog() lambda.setup(program) + enableLog() assert.equal(fs.readFileSync('newContext.json').toString(), '{"FOO"="bar"\n"BAZ"="bing"\n}') assert.equal(fs.readFileSync('newEvent.json').toString(), '{"FOO"="bar"}')