Skip to content

Commit

Permalink
fix(Variables): Ensure no collisions with AWS CloudFormation variables
Browse files Browse the repository at this point in the history
(PR #8279)
  • Loading branch information
mnapoli committed Sep 24, 2020
1 parent a55009e commit 2fdeb51
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/classes/Service.js
Expand Up @@ -20,7 +20,7 @@ class Service {
this.serviceObject = null;
this.provider = {
stage: 'dev',
variableSyntax: '\\${([^{}]+?)}',
variableSyntax: '\\${([^{}:]+?(?:\\(|:)[^:{}][^{}]*?)}',
};
this.custom = {};
this.plugins = [];
Expand Down
34 changes: 34 additions & 0 deletions lib/classes/Variables.test.js
Expand Up @@ -21,6 +21,7 @@ const Utils = require('../../lib/classes/Utils');
const Variables = require('../../lib/classes/Variables');
const { getTmpDirPath } = require('../../test/utils/fs');
const skipOnDisabledSymlinksInWindows = require('@serverless/test/skip-on-disabled-symlinks-in-windows');
const runServerless = require('../../test/utils/run-serverless');

BbPromise.longStackTraces(true);

Expand Down Expand Up @@ -2736,4 +2737,37 @@ module.exports = {
expect(logWarningSpy.args[0][0]).to.contain('file');
});
});

describe('variable syntax', () => {
let processedConfig = null;
before(async () => {
const result = await runServerless({
fixture: 'variables',
cliArgs: ['print'],
});
processedConfig = result.serverless.service;
});

it('should support ${file(...)} syntax', () => {
expect(processedConfig.custom.importedFile).to.deep.equal({
foo: 'bar',
});
});

it('should support ${file(...):key} syntax', () => {
expect(processedConfig.custom.importedFileWithKey).to.equal('bar');
});

it('should ignore native CloudFormation variables', () => {
expect(processedConfig.custom.awsVariable).to.equal('${AWS::Region}');
});

it('should ignore CloudFormation references', () => {
expect(processedConfig.custom.cloudFormationReference).to.equal('${AnotherResource}');
});

it('should support ${self:key} syntax', () => {
expect(processedConfig.custom.selfReference).to.equal('bar');
});
});
});
32 changes: 0 additions & 32 deletions lib/plugins/print/print.test.js
Expand Up @@ -326,38 +326,6 @@ describe('Print', () => {
});
});

it('should resolve self references', () => {
const conf = {
custom: {
me: '${self:}',
},
provider: {},
};
getServerlessConfigFileStub.resolves(conf);

serverless.processedInput = {
commands: ['print'],
options: {},
};

const expected = {
custom: {
me: {
$ref: '$',
},
},
provider: {},
};

return print.print().then(() => {
const message = print.serverless.cli.consoleLog.args.join();

expect(getServerlessConfigFileStub.calledOnce).to.equal(true);
expect(print.serverless.cli.consoleLog.called).to.be.equal(true);
expect(YAML.load(message)).to.eql(expected);
});
});

describe('should resolve fallback', () => {
[
{ value: 'hello_123@~:/+', description: 'ascii chars' },
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/variables/config.json
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
12 changes: 12 additions & 0 deletions test/fixtures/variables/serverless.yml
@@ -0,0 +1,12 @@
service: service

provider:
name: aws
runtime: nodejs12.x

custom:
importedFile: ${file(config.json)}
importedFileWithKey: ${file(config.json):foo}
awsVariable: ${AWS::Region}
cloudFormationReference: ${AnotherResource}
selfReference: ${self:custom.importedFileWithKey}

0 comments on commit 2fdeb51

Please sign in to comment.