From cb74b3b1bb0d3eb28da859253caaea2c5467ae37 Mon Sep 17 00:00:00 2001 From: kengos Date: Wed, 21 Sep 2016 22:36:13 +0900 Subject: [PATCH 1/2] add filename option to YAML.load --- lib/classes/Utils.js | 2 +- tests/classes/Utils.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/classes/Utils.js b/lib/classes/Utils.js index b03055cf5..6eef6587b 100644 --- a/lib/classes/Utils.js +++ b/lib/classes/Utils.js @@ -76,7 +76,7 @@ class Utils { if (filePath.endsWith('.json')) { contents = JSON.parse(contents); } else if (filePath.endsWith('.yml') || filePath.endsWith('.yaml')) { - contents = YAML.load(contents.toString()); + contents = YAML.load(contents.toString(), { filename: filePath }); } else { contents = contents.toString().trim(); } diff --git a/tests/classes/Utils.js b/tests/classes/Utils.js index 7711df8a6..08362e850 100644 --- a/tests/classes/Utils.js +++ b/tests/classes/Utils.js @@ -5,7 +5,13 @@ const os = require('os'); const expect = require('chai').expect; const Serverless = require('../../lib/Serverless'); const testUtils = require('../../tests/utils'); +const BbPromise = require('bluebird'); +const fse = BbPromise.promisifyAll(require('fs-extra')); +const writeFileSync = (filePath, contents) => { + fse.mkdirsSync(path.dirname(filePath)); + return fse.writeFileSync(filePath, contents); +}; const serverless = new Serverless(); describe('Utils', () => { @@ -107,6 +113,33 @@ describe('Utils', () => { expect(obj.foo).to.equal('bar'); }); + + it('should read a filename extension .yml', () => { + const tmpFilePath = testUtils.getTmpFilePath('anything.yml'); + + serverless.utils.writeFileSync(tmpFilePath, { foo: 'bar' }); + const obj = serverless.utils.readFileSync(tmpFilePath); + + expect(obj.foo).to.equal('bar'); + }); + + it('should read a filename extension .yaml', () => { + const tmpFilePath = testUtils.getTmpFilePath('anything.yaml'); + + serverless.utils.writeFileSync(tmpFilePath, { foo: 'bar' }); + const obj = serverless.utils.readFileSync(tmpFilePath); + + expect(obj.foo).to.equal('bar'); + }); + + it('should throw YAMLException with filename if yml file is invalid format', () => { + const tmpFilePath = testUtils.getTmpFilePath('invalid.yml'); + const contents = ': a'; + writeFileSync(tmpFilePath, contents); + expect(() => { + serverless.utils.readFileSync(tmpFilePath); + }).to.throw(new RegExp(`in "${tmpFilePath}"`)); + }); }); describe('#readFile()', () => { From eaaeade956fdc04ff697bbe34fb2f39ba297e1e9 Mon Sep 17 00:00:00 2001 From: kengos Date: Thu, 22 Sep 2016 11:41:26 +0900 Subject: [PATCH 2/2] remove unnecessary requires and method --- tests/classes/Utils.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/classes/Utils.js b/tests/classes/Utils.js index 08362e850..56952475f 100644 --- a/tests/classes/Utils.js +++ b/tests/classes/Utils.js @@ -5,13 +5,7 @@ const os = require('os'); const expect = require('chai').expect; const Serverless = require('../../lib/Serverless'); const testUtils = require('../../tests/utils'); -const BbPromise = require('bluebird'); -const fse = BbPromise.promisifyAll(require('fs-extra')); -const writeFileSync = (filePath, contents) => { - fse.mkdirsSync(path.dirname(filePath)); - return fse.writeFileSync(filePath, contents); -}; const serverless = new Serverless(); describe('Utils', () => { @@ -134,8 +128,9 @@ describe('Utils', () => { it('should throw YAMLException with filename if yml file is invalid format', () => { const tmpFilePath = testUtils.getTmpFilePath('invalid.yml'); - const contents = ': a'; - writeFileSync(tmpFilePath, contents); + + serverless.utils.writeFileSync(tmpFilePath, ': a'); + expect(() => { serverless.utils.readFileSync(tmpFilePath); }).to.throw(new RegExp(`in "${tmpFilePath}"`));