diff --git a/lib/classes/Service.js b/lib/classes/Service.js index 1a74555308d..d87ff5a1f74 100644 --- a/lib/classes/Service.js +++ b/lib/classes/Service.js @@ -20,7 +20,7 @@ class Service { this.serviceObject = null; this.provider = { stage: 'dev', - variableSyntax: '\\${([^{}]+?)}', + variableSyntax: '\\${([^{}:]+?(?:\\(|:)[^:{}][^{}]*?)}', }; this.custom = {}; this.plugins = []; diff --git a/lib/classes/Variables.test.js b/lib/classes/Variables.test.js index c2a380c776b..7eb09d04b42 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); @@ -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'); + }); + }); }); 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}