Skip to content

Commit eafb61b

Browse files
author
lsphillips
committed
Added back support for CLI parameters.
1 parent 580e1d6 commit eafb61b

File tree

2 files changed

+115
-47
lines changed
  • lib/configuration/variables/sources/instance-dependent
  • test/unit/lib/configuration/variables/sources/instance-dependent

2 files changed

+115
-47
lines changed

lib/configuration/variables/sources/instance-dependent/param.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ const resolveParams = memoizee(async (stage, serverlessInstance) => {
1212

1313
const resultParams = Object.create(null);
1414

15+
if (serverlessInstance.processedInput.options.param) {
16+
const regex = /(?<key>[^=]+)=(?<value>.+)/;
17+
for (const item of serverlessInstance.processedInput.options.param) {
18+
const res = item.match(regex);
19+
if (!res) {
20+
throw new ServerlessError(
21+
`Encountered invalid "--param" CLI option value: "${item}". Supported format: "--param='<key>=<val>'"`,
22+
'INVALID_CLI_PARAM_FORMAT'
23+
);
24+
}
25+
resultParams[res.groups.key] = { value: res.groups.value.trimEnd(), type: 'cli' };
26+
}
27+
}
28+
1529
for (const [name, value] of Object.entries(configParams.get(stage) || {})) {
1630
if (value == null) continue;
1731
if (resultParams[name] != null) continue;

test/unit/lib/configuration/variables/sources/instance-dependent/param.test.js

Lines changed: 101 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,105 +10,159 @@ const getParamSource = require('../../../../../../../lib/configuration/variables
1010
const Serverless = require('../../../../../../../lib/serverless');
1111

1212
describe('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',
13+
const runServerless = async ({
14+
cliParameters = [],
15+
stageParameters = {},
16+
stage,
17+
resolveWithoutInstance = false,
18+
} = {}) => {
19+
const configuration = {
20+
service: 'param-test-service',
2021
provider: {
22+
stage,
2123
name: 'aws',
2224
deploymentBucket: '${param:bucket}',
2325
timeout: '${param:timeout}',
26+
region: '${param:region}',
2427
},
2528
custom: {
2629
missingAddress: '${param:}',
2730
unsupportedAddress: '${param:foo}',
2831
nonStringAddress: '${param:${self:custom.someObject}}',
2932
someObject: {},
3033
},
31-
params: {
32-
default: {
33-
bucket: 'global.bucket',
34-
timeout: 10,
35-
},
36-
dev: {
37-
bucket: 'my.bucket',
38-
},
39-
},
34+
params: stageParameters,
4035
};
41-
if (configExt) {
42-
configuration = _.merge(configuration, configExt);
43-
}
44-
variablesMeta = resolveMeta(configuration);
45-
serverlessInstance = new Serverless({
36+
37+
const variablesMeta = resolveMeta(configuration);
38+
39+
const serverlessInstance = new Serverless({
4640
configuration,
41+
options: {
42+
param: cliParameters,
43+
},
4744
serviceDir: process.cwd(),
4845
configurationFilename: 'serverless.yml',
4946
commands: ['package'],
50-
options: options || {},
5147
});
48+
5249
serverlessInstance.init();
50+
5351
await resolve({
5452
serviceDir: process.cwd(),
5553
configuration,
5654
variablesMeta,
5755
sources: {
5856
self: selfSource,
59-
param: getParamSource(setupOptions.withoutInstance ? null : serverlessInstance),
57+
param: getParamSource(resolveWithoutInstance ? null : serverlessInstance),
58+
},
59+
options: {
60+
param: cliParameters,
6061
},
61-
options: options || {},
6262
fulfilledSources: new Set(['self', 'param']),
6363
});
64+
65+
return {
66+
configuration,
67+
serverlessInstance,
68+
variablesMeta,
69+
};
6470
};
6571

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);
72+
it('should resolve parameters from CLI parameters', async () => {
73+
const { configuration } = await runServerless({
74+
cliParameters: ['region=eu-west-1'],
75+
});
76+
expect(configuration.provider.region).to.equal('eu-west-1');
7077
});
7178

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');
79+
it('should resolve parameter from parameters for the configured stage', async () => {
80+
const { configuration } = await runServerless({
81+
stageParameters: {
82+
staging: {
83+
timeout: 10,
84+
},
85+
},
86+
stage: 'staging',
87+
});
88+
expect(configuration.provider.timeout).to.equal(10);
89+
});
7690

77-
// Forced prod
78-
await initializeServerless({
79-
configExt: {
80-
provider: {
81-
stage: 'prod',
91+
it('should resolve parameter from default parameters if the parameter is not set for the configured stage', async () => {
92+
const { configuration } = await runServerless({
93+
stageParameters: {
94+
staging: {},
95+
default: {
96+
bucket: 'global.bucket',
8297
},
8398
},
99+
stage: 'staging',
84100
});
85101
expect(configuration.provider.deploymentBucket).to.equal('global.bucket');
86102
});
87103

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');
104+
it('should resolve parameter from `dev` parameter if the stage is not configured', async () => {
105+
const { configuration } = await runServerless({
106+
stageParameters: {
107+
dev: {
108+
timeout: 5,
109+
},
110+
staging: {
111+
timeout: 10,
112+
},
113+
},
114+
});
115+
expect(configuration.provider.timeout).to.equal(5);
92116
});
93117

94-
it('should report with an error missing address', async () => {
95-
await initializeServerless();
118+
it('should treat CLI parameters with a higher precedence than stage parameters', async () => {
119+
const { configuration } = await runServerless({
120+
cliParameters: ['region=eu-west-2'],
121+
stageParameters: {
122+
staging: {
123+
region: 'eu-west-1',
124+
},
125+
},
126+
stage: 'staging',
127+
});
128+
expect(configuration.provider.region).to.equal('eu-west-2');
129+
});
130+
131+
it('should report with an error when the CLI parameter is invalid', async () => {
132+
const { variablesMeta } = await runServerless({
133+
cliParameters: ['region'],
134+
});
135+
136+
expect(variablesMeta.get('provider\0region').error.code).to.equal('VARIABLE_RESOLUTION_ERROR');
137+
});
138+
139+
it('should report with an error when the address is missing', async () => {
140+
const { variablesMeta } = await runServerless();
96141
expect(variablesMeta.get('custom\0missingAddress').error.code).to.equal(
97142
'VARIABLE_RESOLUTION_ERROR'
98143
);
99144
});
100145

101-
it('should report with an error unsupported address', async () => {
102-
await initializeServerless();
146+
it('should report with an error when the address is not supported', async () => {
147+
const { variablesMeta } = await runServerless();
103148
expect(variablesMeta.get('custom\0unsupportedAddress').error.code).to.equal(
104149
'VARIABLE_RESOLUTION_ERROR'
105150
);
106151
});
107152

108-
it('should report with an error a non-string address', async () => {
109-
await initializeServerless();
153+
it('should report with an error when the address it not a string', async () => {
154+
const { variablesMeta } = await runServerless();
110155
expect(variablesMeta.get('custom\0nonStringAddress').error.code).to.equal(
111156
'VARIABLE_RESOLUTION_ERROR'
112157
);
113158
});
159+
160+
it('should still resolve varibales when no Serverless instance is available', async () => {
161+
const { variablesMeta } = await runServerless({
162+
cliParameters: ['timeout=10'],
163+
resolveWithoutInstance: true,
164+
});
165+
expect(variablesMeta.get('provider\0timeout')).to.have.property('variables');
166+
expect(variablesMeta.get('provider\0timeout')).to.not.have.property('error');
167+
});
114168
});

0 commit comments

Comments
 (0)