Skip to content

Commit

Permalink
Fix #3184 Compatibility with native AWS CloudFormation variable syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Sep 24, 2020
1 parent 76e02cc commit abfe512
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
9 changes: 8 additions & 1 deletion lib/classes/Service.js
Expand Up @@ -20,7 +20,14 @@ class Service {
this.serviceObject = null;
this.provider = {
stage: 'dev',
variableSyntax: '\\${([^{}]+?)}',
variableSyntax:
'\\${(' +
// either 'foo:bar'
'[^{}:]+?:[^:{}][^{}]*?' +
'|' +
// or 'file(...)'
'file\\([^\\)]*\\)' +
')}',
};
this.custom = {};
this.plugins = [];
Expand Down
43 changes: 43 additions & 0 deletions lib/classes/Variables.test.js
Expand Up @@ -1359,6 +1359,49 @@ module.exports = {
.populateProperty(property)
.should.eventually.eql('my stage is dev');
});

it('should support ${file(...)} syntax', () => {
// eslint-disable-next-line no-template-curly-in-string
const property = '${file(~/somedir/../config.yml)}';

const expectedFileName = `${os.homedir()}/somedir/../config.yml`;
const configYml = {
foo: 'bar',
};
const fileExistsStub = sinon.stub(serverless.utils, 'fileExistsSync').returns(true);
const realpathSync = sinon.stub(fse, 'realpathSync').returns(expectedFileName);
const readFileSyncStub = sinon.stub(serverless.utils, 'readFileSync').returns(configYml);

return serverless.variables
.populateProperty(property)
.then(valueToPopulate => {
expect(realpathSync).to.not.have.been.called;
expect(fileExistsStub).to.have.been.calledWithMatch(expectedFileName);
expect(readFileSyncStub).to.have.been.calledWithMatch(expectedFileName);
expect(valueToPopulate).to.deep.equal(configYml);
})
.finally(() => {
realpathSync.restore();
readFileSyncStub.restore();
fileExistsStub.restore();
});
});

it('should ignore native CloudFormation variables', () => {
// eslint-disable-next-line no-template-curly-in-string
const property = 'the region is ${AWS::Region}';
return serverless.variables
.populateProperty(property)
.should.eventually.eql('the region is ${AWS::Region}');
});

it('should ignore CloudFormation references', () => {
// eslint-disable-next-line no-template-curly-in-string
const property = 'the region is ${AnotherResource}';
return serverless.variables
.populateProperty(property)
.should.eventually.eql('the region is ${AnotherResource}');
});
});

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

describe('should resolve fallback', () => {
[
{ value: 'hello_123@~:/+', description: 'ascii chars' },
Expand Down

0 comments on commit abfe512

Please sign in to comment.