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 2 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
50 changes: 50 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 @@ -1359,6 +1360,55 @@ module.exports = {
.populateProperty(property)
.should.eventually.eql('my stage is dev');
});

it('should support ${file(...)} syntax', () => {
return runServerless({
fixture: 'variables',
cliArgs: ['print'],
}).then(result => {
mnapoli marked this conversation as resolved.
Show resolved Hide resolved
expect(result.serverless.service.custom.importedFile).to.deep.equal({
foo: 'bar',
});
});
});

it('should support ${file(...):key} syntax', () => {
return runServerless({
fixture: 'variables',
cliArgs: ['print'],
}).then(result => {
expect(result.serverless.service.custom.importedFileWithKey).to.equal('bar');
});
});
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 added a higher level test here because there was no test covering that ${file(...)} is actually parsed correctly. When I changed the regex, initially the tests passed but it was a false positive: that syntax was broken. So I added that test and iterated on the regex.

mnapoli marked this conversation as resolved.
Show resolved Hide resolved

it('should ignore native CloudFormation variables', () => {
return runServerless({
fixture: 'variables',
cliArgs: ['print'],
}).then(result => {
expect(result.serverless.service.custom.awsVariable).to.equal('${AWS::Region}');
});
});

it('should ignore CloudFormation references', () => {
return runServerless({
fixture: 'variables',
cliArgs: ['print'],
}).then(result => {
expect(result.serverless.service.custom.cloudFormationReference).to.equal(
'${AnotherResource}'
);
});
});

it('should support ${self:key} syntax', () => {
return runServerless({
fixture: 'variables',
cliArgs: ['print'],
}).then(result => {
expect(result.serverless.service.custom.selfReference).to.equal('bar');
});
});
});

describe('#populateVariable()', () => {
Expand Down
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}