Merge pull request #2176 from kengos/add_options_to_yaml_load

add filename option to YAML.load
This commit is contained in:
Florian Motlik 2016-09-22 14:31:09 +02:00 committed by GitHub
commit 8a39f4e2c1
2 changed files with 29 additions and 1 deletions

View File

@ -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();
}

View File

@ -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()', () => {