From 5697d2d193d33598d71eb1fb87fd5040c30fd983 Mon Sep 17 00:00:00 2001 From: horike37 Date: Wed, 12 Oct 2016 02:54:52 +0900 Subject: [PATCH 1/5] Add Catch Errors for Tracking --- lib/plugins/tracking/tracking.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/plugins/tracking/tracking.js b/lib/plugins/tracking/tracking.js index 10115bdaa..3f5e75a23 100644 --- a/lib/plugins/tracking/tracking.js +++ b/lib/plugins/tracking/tracking.js @@ -1,7 +1,6 @@ 'use strict'; const path = require('path'); -const fs = require('fs'); const fse = require('fs-extra'); class Tracking { @@ -41,10 +40,16 @@ class Tracking { fse.removeSync(path.join(serverlessPath, trackingFileName)); this.serverless.cli.log('Tracking successfully enabled'); } + if (this.options.disable && !this.options.enable) { - fs.writeFileSync(path.join(serverlessPath, trackingFileName), - 'Keep this file to disable tracking'); - this.serverless.cli.log('Tracking successfully disabled'); + const that = this; + this.serverless.utils.writeFile(path.join(serverlessPath, trackingFileName), + 'Keep this file to disable tracking').then(() => { + that.serverless.cli.log('Tracking successfully disabled'); + }).catch((e) => { + throw new that.serverless.classes.Error(`Tracking Failed to disable.The following message: + ${e.message}`); + }); } } } From 45f2d82f597d034da59dffb140f5701c8f98757a Mon Sep 17 00:00:00 2001 From: horike37 Date: Tue, 1 Nov 2016 05:02:29 +0900 Subject: [PATCH 2/5] Add Catch Errors for slstats --- lib/plugins/slstats/slstats.js | 23 ++++++++++++++--------- lib/plugins/slstats/tests/slstats.js | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/plugins/slstats/slstats.js b/lib/plugins/slstats/slstats.js index cc13b397b..52de89092 100644 --- a/lib/plugins/slstats/slstats.js +++ b/lib/plugins/slstats/slstats.js @@ -38,17 +38,22 @@ class SlStats { const statsDisabledFilePath = path.join(serverlessDirPath, 'stats-disabled'); const statsEnabledFilePath = path.join(serverlessDirPath, 'stats-enabled'); - if (this.options.enable && !this.options.disable) { - if (this.serverless.utils.fileExistsSync(statsDisabledFilePath)) { - fse.renameSync(statsDisabledFilePath, statsEnabledFilePath); + try { + if (this.options.enable && !this.options.disable) { + if (fse.lstatSync(statsDisabledFilePath).isFile()) { + fse.renameSync(statsDisabledFilePath, statsEnabledFilePath); + } + this.serverless.cli.log('Stats successfully enabled'); } - this.serverless.cli.log('Stats successfully enabled'); - } - if (this.options.disable && !this.options.enable) { - if (this.serverless.utils.fileExistsSync(statsEnabledFilePath)) { - fse.renameSync(statsEnabledFilePath, statsDisabledFilePath); + if (this.options.disable && !this.options.enable) { + if (fse.lstatSync(statsEnabledFilePath).isFile()) { + fse.renameSync(statsEnabledFilePath, statsDisabledFilePath); + } + this.serverless.cli.log('Stats successfully disabled'); } - this.serverless.cli.log('Stats successfully disabled'); + } catch (e) { + throw new this.serverless.classes + .Error(`slstats failed. The following message: ${e.message}`); } } } diff --git a/lib/plugins/slstats/tests/slstats.js b/lib/plugins/slstats/tests/slstats.js index f01c22393..c070f9aa5 100644 --- a/lib/plugins/slstats/tests/slstats.js +++ b/lib/plugins/slstats/tests/slstats.js @@ -82,6 +82,24 @@ describe('SlStats', () => { ).to.equal(false); }); + it('should throw error if the stats file is not exists', () => { + // create a stats-disabled file + serverless.utils.writeFileSync( + path.join(serverlessDirPath, 'stats-error-file'), + 'some content' + ); + + slStats.options = { enable: true }; + + expect(() => slStats.toggleStats()).to.throw(); + expect( + serverless.utils.fileExistsSync(path.join(serverlessDirPath, 'stats-enabled')) + ).to.equal(false); + expect( + serverless.utils.fileExistsSync(path.join(serverlessDirPath, 'stats-disabled')) + ).to.equal(false); + }); + afterEach(() => { // recover the homeDir process.env.HOME = homeDir; From 27e4c20edeef291ffde52ec103068765fc34cde8 Mon Sep 17 00:00:00 2001 From: horike37 Date: Tue, 1 Nov 2016 05:23:47 +0900 Subject: [PATCH 3/5] some fix test code --- lib/plugins/slstats/tests/slstats.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/slstats/tests/slstats.js b/lib/plugins/slstats/tests/slstats.js index c070f9aa5..8f54260a1 100644 --- a/lib/plugins/slstats/tests/slstats.js +++ b/lib/plugins/slstats/tests/slstats.js @@ -91,7 +91,7 @@ describe('SlStats', () => { slStats.options = { enable: true }; - expect(() => slStats.toggleStats()).to.throw(); + expect(() => slStats.toggleStats()).to.throw(Error, /slstats failed. The following message: ENOENT: no such file or directory, lstat/); expect( serverless.utils.fileExistsSync(path.join(serverlessDirPath, 'stats-enabled')) ).to.equal(false); From 7ea0133bf466b3fbbfb123bc576f1efc786d11f6 Mon Sep 17 00:00:00 2001 From: horike37 Date: Tue, 1 Nov 2016 05:29:17 +0900 Subject: [PATCH 4/5] fixed lint error --- lib/plugins/slstats/tests/slstats.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/plugins/slstats/tests/slstats.js b/lib/plugins/slstats/tests/slstats.js index 8f54260a1..60a6a74bc 100644 --- a/lib/plugins/slstats/tests/slstats.js +++ b/lib/plugins/slstats/tests/slstats.js @@ -91,7 +91,8 @@ describe('SlStats', () => { slStats.options = { enable: true }; - expect(() => slStats.toggleStats()).to.throw(Error, /slstats failed. The following message: ENOENT: no such file or directory, lstat/); + expect(() => slStats.toggleStats()).to.throw(Error, + /slstats failed. The following message: ENOENT: no such file or directory, lstat/); expect( serverless.utils.fileExistsSync(path.join(serverlessDirPath, 'stats-enabled')) ).to.equal(false); From e8aaecef87c1037e0b6f1f61befb0eb199b3630b Mon Sep 17 00:00:00 2001 From: horike37 Date: Sat, 5 Nov 2016 16:24:35 +0900 Subject: [PATCH 5/5] some updates --- lib/plugins/slstats/slstats.js | 4 ++-- lib/plugins/slstats/tests/slstats.js | 10 ++-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/plugins/slstats/slstats.js b/lib/plugins/slstats/slstats.js index 52de89092..5eff6ca01 100644 --- a/lib/plugins/slstats/slstats.js +++ b/lib/plugins/slstats/slstats.js @@ -51,9 +51,9 @@ class SlStats { } this.serverless.cli.log('Stats successfully disabled'); } - } catch (e) { + } catch (error) { throw new this.serverless.classes - .Error(`slstats failed. The following message: ${e.message}`); + .Error(`Enabling / Disabling of statistics failed: ${error.message}`); } } } diff --git a/lib/plugins/slstats/tests/slstats.js b/lib/plugins/slstats/tests/slstats.js index 60a6a74bc..3ce6ad13e 100644 --- a/lib/plugins/slstats/tests/slstats.js +++ b/lib/plugins/slstats/tests/slstats.js @@ -82,17 +82,11 @@ describe('SlStats', () => { ).to.equal(false); }); - it('should throw error if the stats file is not exists', () => { - // create a stats-disabled file - serverless.utils.writeFileSync( - path.join(serverlessDirPath, 'stats-error-file'), - 'some content' - ); - + it('should throw an error if the stats file does not exist', () => { slStats.options = { enable: true }; expect(() => slStats.toggleStats()).to.throw(Error, - /slstats failed. The following message: ENOENT: no such file or directory, lstat/); + /Enabling \/ Disabling of statistics failed: ENOENT: no such file or directory, lstat/); expect( serverless.utils.fileExistsSync(path.join(serverlessDirPath, 'stats-enabled')) ).to.equal(false);