11'use strict' ;
22
33const { expect } = require ( 'chai' ) ;
4- const _ = require ( 'lodash' ) ;
54
65const resolveMeta = require ( '../../../../../../../lib/configuration/variables/resolve-meta' ) ;
76const resolve = require ( '../../../../../../../lib/configuration/variables/resolve' ) ;
@@ -10,105 +9,159 @@ const getParamSource = require('../../../../../../../lib/configuration/variables
109const Serverless = require ( '../../../../../../../lib/serverless' ) ;
1110
1211describe ( 'test/unit/lib/configuration/variables/sources/instance-dependent/param.test.js' , ( ) => {
13- let configuration ;
14- let variablesMeta ;
15- let serverlessInstance ;
16-
17- const initializeServerless = async ( { configExt, options, setupOptions = { } } = { } ) => {
18- configuration = {
19- service : 'foo' ,
12+ const runServerless = async ( {
13+ cliParameters = [ ] ,
14+ stageParameters = { } ,
15+ stage,
16+ resolveWithoutInstance = false ,
17+ } = { } ) => {
18+ const configuration = {
19+ service : 'param-test-service' ,
2020 provider : {
21+ stage,
2122 name : 'aws' ,
2223 deploymentBucket : '${param:bucket}' ,
2324 timeout : '${param:timeout}' ,
25+ region : '${param:region}' ,
2426 } ,
2527 custom : {
2628 missingAddress : '${param:}' ,
2729 unsupportedAddress : '${param:foo}' ,
2830 nonStringAddress : '${param:${self:custom.someObject}}' ,
2931 someObject : { } ,
3032 } ,
31- params : {
32- default : {
33- bucket : 'global.bucket' ,
34- timeout : 10 ,
35- } ,
36- dev : {
37- bucket : 'my.bucket' ,
38- } ,
39- } ,
33+ params : stageParameters ,
4034 } ;
41- if ( configExt ) {
42- configuration = _ . merge ( configuration , configExt ) ;
43- }
44- variablesMeta = resolveMeta ( configuration ) ;
45- serverlessInstance = new Serverless ( {
35+
36+ const variablesMeta = resolveMeta ( configuration ) ;
37+
38+ const serverlessInstance = new Serverless ( {
4639 configuration,
40+ options : {
41+ param : cliParameters ,
42+ } ,
4743 serviceDir : process . cwd ( ) ,
4844 configurationFilename : 'serverless.yml' ,
4945 commands : [ 'package' ] ,
50- options : options || { } ,
5146 } ) ;
47+
5248 serverlessInstance . init ( ) ;
49+
5350 await resolve ( {
5451 serviceDir : process . cwd ( ) ,
5552 configuration,
5653 variablesMeta,
5754 sources : {
5855 self : selfSource ,
59- param : getParamSource ( setupOptions . withoutInstance ? null : serverlessInstance ) ,
56+ param : getParamSource ( resolveWithoutInstance ? null : serverlessInstance ) ,
57+ } ,
58+ options : {
59+ param : cliParameters ,
6060 } ,
61- options : options || { } ,
6261 fulfilledSources : new Set ( [ 'self' , 'param' ] ) ,
6362 } ) ;
63+
64+ return {
65+ configuration,
66+ serverlessInstance,
67+ variablesMeta,
68+ } ;
6469 } ;
6570
66- it ( 'should resolve ${param:timeout}' , async ( ) => {
67- await initializeServerless ( ) ;
68- if ( variablesMeta . get ( 'param\0timeout' ) ) throw variablesMeta . get ( 'param\0timeout' ) . error ;
69- expect ( configuration . provider . timeout ) . to . equal ( 10 ) ;
71+ it ( 'should resolve parameters from CLI parameters' , async ( ) => {
72+ const { configuration } = await runServerless ( {
73+ cliParameters : [ 'region=eu-west-1' ] ,
74+ } ) ;
75+ expect ( configuration . provider . region ) . to . equal ( 'eu-west-1' ) ;
7076 } ) ;
7177
72- it ( 'should resolve ${param:bucket} for different stages' , async ( ) => {
73- // Dev by default
74- await initializeServerless ( ) ;
75- expect ( configuration . provider . deploymentBucket ) . to . equal ( 'my.bucket' ) ;
78+ it ( 'should resolve parameter from parameters for the configured stage' , async ( ) => {
79+ const { configuration } = await runServerless ( {
80+ stageParameters : {
81+ staging : {
82+ timeout : 10 ,
83+ } ,
84+ } ,
85+ stage : 'staging' ,
86+ } ) ;
87+ expect ( configuration . provider . timeout ) . to . equal ( 10 ) ;
88+ } ) ;
7689
77- // Forced prod
78- await initializeServerless ( {
79- configExt : {
80- provider : {
81- stage : 'prod' ,
90+ it ( 'should resolve parameter from default parameters if the parameter is not set for the configured stage' , async ( ) => {
91+ const { configuration } = await runServerless ( {
92+ stageParameters : {
93+ staging : { } ,
94+ default : {
95+ bucket : 'global.bucket' ,
8296 } ,
8397 } ,
98+ stage : 'staging' ,
8499 } ) ;
85100 expect ( configuration . provider . deploymentBucket ) . to . equal ( 'global.bucket' ) ;
86101 } ) ;
87102
88- it ( 'should resolve ${param:bucket} when no serverless instance available' , async ( ) => {
89- await initializeServerless ( { setupOptions : { withoutInstance : true } } ) ;
90- expect ( variablesMeta . get ( 'provider\0timeout' ) ) . to . have . property ( 'variables' ) ;
91- expect ( variablesMeta . get ( 'provider\0timeout' ) ) . to . not . have . property ( 'error' ) ;
103+ it ( 'should resolve parameter from `dev` parameter if the stage is not configured' , async ( ) => {
104+ const { configuration } = await runServerless ( {
105+ stageParameters : {
106+ dev : {
107+ timeout : 5 ,
108+ } ,
109+ staging : {
110+ timeout : 10 ,
111+ } ,
112+ } ,
113+ } ) ;
114+ expect ( configuration . provider . timeout ) . to . equal ( 5 ) ;
92115 } ) ;
93116
94- it ( 'should report with an error missing address' , async ( ) => {
95- await initializeServerless ( ) ;
117+ it ( 'should treat CLI parameters with a higher precedence than stage parameters' , async ( ) => {
118+ const { configuration } = await runServerless ( {
119+ cliParameters : [ 'region=eu-west-2' ] ,
120+ stageParameters : {
121+ staging : {
122+ region : 'eu-west-1' ,
123+ } ,
124+ } ,
125+ stage : 'staging' ,
126+ } ) ;
127+ expect ( configuration . provider . region ) . to . equal ( 'eu-west-2' ) ;
128+ } ) ;
129+
130+ it ( 'should report with an error when the CLI parameter is invalid' , async ( ) => {
131+ const { variablesMeta } = await runServerless ( {
132+ cliParameters : [ 'region' ] ,
133+ } ) ;
134+
135+ expect ( variablesMeta . get ( 'provider\0region' ) . error . code ) . to . equal ( 'VARIABLE_RESOLUTION_ERROR' ) ;
136+ } ) ;
137+
138+ it ( 'should report with an error when the address is missing' , async ( ) => {
139+ const { variablesMeta } = await runServerless ( ) ;
96140 expect ( variablesMeta . get ( 'custom\0missingAddress' ) . error . code ) . to . equal (
97141 'VARIABLE_RESOLUTION_ERROR'
98142 ) ;
99143 } ) ;
100144
101- it ( 'should report with an error unsupported address' , async ( ) => {
102- await initializeServerless ( ) ;
145+ it ( 'should report with an error when the address is not supported ' , async ( ) => {
146+ const { variablesMeta } = await runServerless ( ) ;
103147 expect ( variablesMeta . get ( 'custom\0unsupportedAddress' ) . error . code ) . to . equal (
104148 'VARIABLE_RESOLUTION_ERROR'
105149 ) ;
106150 } ) ;
107151
108- it ( 'should report with an error a non-string address' , async ( ) => {
109- await initializeServerless ( ) ;
152+ it ( 'should report with an error when the address it not a string ' , async ( ) => {
153+ const { variablesMeta } = await runServerless ( ) ;
110154 expect ( variablesMeta . get ( 'custom\0nonStringAddress' ) . error . code ) . to . equal (
111155 'VARIABLE_RESOLUTION_ERROR'
112156 ) ;
113157 } ) ;
158+
159+ it ( 'should still resolve variables when no Serverless instance is available' , async ( ) => {
160+ const { variablesMeta } = await runServerless ( {
161+ cliParameters : [ 'timeout=10' ] ,
162+ resolveWithoutInstance : true ,
163+ } ) ;
164+ expect ( variablesMeta . get ( 'provider\0timeout' ) ) . to . have . property ( 'variables' ) ;
165+ expect ( variablesMeta . get ( 'provider\0timeout' ) ) . to . not . have . property ( 'error' ) ;
166+ } ) ;
114167} ) ;
0 commit comments