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..56952475f 100644 --- a/tests/classes/Utils.js +++ b/tests/classes/Utils.js @@ -107,6 +107,34 @@ 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'); + + serverless.utils.writeFileSync(tmpFilePath, ': a'); + + expect(() => { + serverless.utils.readFileSync(tmpFilePath); + }).to.throw(new RegExp(`in "${tmpFilePath}"`)); + }); }); describe('#readFile()', () => {