Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3184 Compatibility with native AWS CloudFormation variable syntax #8279

Merged
merged 3 commits into from Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed that test: it tested that the ${self:} syntax worked. I had a look at the git history and the rest of the project but couldn't understand why this was tested.

Either I missed something and we should bring that back + cover it with the new regex (let me know), or it's something that isn't necessary anymore and we can move forward.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I would add testing some of ${self: ..} to `variables fixture as proposed in other comment


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}