From 8b593fd4cba053c12bafc67ef0e5dec3dfd08c68 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Wed, 23 Sep 2020 16:31:50 +0200 Subject: [PATCH] Fix #3184 Compatibility with native AWS CloudFormation variable syntax --- lib/classes/Service.js | 9 ++++- lib/classes/Variables.test.js | 50 ++++++++++++++++++++++++++ lib/plugins/print/print.test.js | 32 ----------------- test/fixtures/variables/config.json | 3 ++ test/fixtures/variables/serverless.yml | 12 +++++++ 5 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 test/fixtures/variables/config.json create mode 100644 test/fixtures/variables/serverless.yml diff --git a/lib/classes/Service.js b/lib/classes/Service.js index 1a74555308d..7e8a4c859df 100644 --- a/lib/classes/Service.js +++ b/lib/classes/Service.js @@ -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 = []; diff --git a/lib/classes/Variables.test.js b/lib/classes/Variables.test.js index c2a380c776b..d9247fb2aa3 100644 --- a/lib/classes/Variables.test.js +++ b/lib/classes/Variables.test.js @@ -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); @@ -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 => { + 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'); + }); + }); + + 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()', () => { diff --git a/lib/plugins/print/print.test.js b/lib/plugins/print/print.test.js index 917760b6ae0..9819ba371fb 100644 --- a/lib/plugins/print/print.test.js +++ b/lib/plugins/print/print.test.js @@ -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' }, diff --git a/test/fixtures/variables/config.json b/test/fixtures/variables/config.json new file mode 100644 index 00000000000..c8c4105eb57 --- /dev/null +++ b/test/fixtures/variables/config.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} diff --git a/test/fixtures/variables/serverless.yml b/test/fixtures/variables/serverless.yml new file mode 100644 index 00000000000..dc34906a217 --- /dev/null +++ b/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}