mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
Merge pull request #2176 from kengos/add_options_to_yaml_load
add filename option to YAML.load
This commit is contained in:
commit
8a39f4e2c1
@ -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();
|
||||
}
|
||||
|
||||
@ -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()', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user