Skip to content

Added back support for CLI parameters. #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/configuration/variables/sources/instance-dependent/param.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ const resolveParams = memoizee(async (stage, serverlessInstance) => {

const resultParams = Object.create(null);

if (serverlessInstance.processedInput.options.param) {
const regex = /(?<key>[^=]+)=(?<value>.+)/;
for (const item of serverlessInstance.processedInput.options.param) {
const res = item.match(regex);
if (!res) {
throw new ServerlessError(
`Encountered invalid "--param" CLI option value: "${item}". Supported format: "--param='<key>=<val>'"`,
'INVALID_CLI_PARAM_FORMAT'
);
}
resultParams[res.groups.key] = { value: res.groups.value.trimEnd(), type: 'cli' };
}
}

for (const [name, value] of Object.entries(configParams.get(stage) || {})) {
if (value == null) continue;
if (resultParams[name] != null) continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const { expect } = require('chai');
const _ = require('lodash');

const resolveMeta = require('../../../../../../../lib/configuration/variables/resolve-meta');
const resolve = require('../../../../../../../lib/configuration/variables/resolve');
Expand All @@ -10,105 +9,159 @@ const getParamSource = require('../../../../../../../lib/configuration/variables
const Serverless = require('../../../../../../../lib/serverless');

describe('test/unit/lib/configuration/variables/sources/instance-dependent/param.test.js', () => {
let configuration;
let variablesMeta;
let serverlessInstance;

const initializeServerless = async ({ configExt, options, setupOptions = {} } = {}) => {
configuration = {
service: 'foo',
const runServerless = async ({
cliParameters = [],
stageParameters = {},
stage,
resolveWithoutInstance = false,
} = {}) => {
const configuration = {
service: 'param-test-service',
provider: {
stage,
name: 'aws',
deploymentBucket: '${param:bucket}',
timeout: '${param:timeout}',
region: '${param:region}',
},
custom: {
missingAddress: '${param:}',
unsupportedAddress: '${param:foo}',
nonStringAddress: '${param:${self:custom.someObject}}',
someObject: {},
},
params: {
default: {
bucket: 'global.bucket',
timeout: 10,
},
dev: {
bucket: 'my.bucket',
},
},
params: stageParameters,
};
if (configExt) {
configuration = _.merge(configuration, configExt);
}
variablesMeta = resolveMeta(configuration);
serverlessInstance = new Serverless({

const variablesMeta = resolveMeta(configuration);

const serverlessInstance = new Serverless({
configuration,
options: {
param: cliParameters,
},
serviceDir: process.cwd(),
configurationFilename: 'serverless.yml',
commands: ['package'],
options: options || {},
});

serverlessInstance.init();

await resolve({
serviceDir: process.cwd(),
configuration,
variablesMeta,
sources: {
self: selfSource,
param: getParamSource(setupOptions.withoutInstance ? null : serverlessInstance),
param: getParamSource(resolveWithoutInstance ? null : serverlessInstance),
},
options: {
param: cliParameters,
},
options: options || {},
fulfilledSources: new Set(['self', 'param']),
});

return {
configuration,
serverlessInstance,
variablesMeta,
};
};

it('should resolve ${param:timeout}', async () => {
await initializeServerless();
if (variablesMeta.get('param\0timeout')) throw variablesMeta.get('param\0timeout').error;
expect(configuration.provider.timeout).to.equal(10);
it('should resolve parameters from CLI parameters', async () => {
const { configuration } = await runServerless({
cliParameters: ['region=eu-west-1'],
});
expect(configuration.provider.region).to.equal('eu-west-1');
});

it('should resolve ${param:bucket} for different stages', async () => {
// Dev by default
await initializeServerless();
expect(configuration.provider.deploymentBucket).to.equal('my.bucket');
it('should resolve parameter from parameters for the configured stage', async () => {
const { configuration } = await runServerless({
stageParameters: {
staging: {
timeout: 10,
},
},
stage: 'staging',
});
expect(configuration.provider.timeout).to.equal(10);
});

// Forced prod
await initializeServerless({
configExt: {
provider: {
stage: 'prod',
it('should resolve parameter from default parameters if the parameter is not set for the configured stage', async () => {
const { configuration } = await runServerless({
stageParameters: {
staging: {},
default: {
bucket: 'global.bucket',
},
},
stage: 'staging',
});
expect(configuration.provider.deploymentBucket).to.equal('global.bucket');
});

it('should resolve ${param:bucket} when no serverless instance available', async () => {
await initializeServerless({ setupOptions: { withoutInstance: true } });
expect(variablesMeta.get('provider\0timeout')).to.have.property('variables');
expect(variablesMeta.get('provider\0timeout')).to.not.have.property('error');
it('should resolve parameter from `dev` parameter if the stage is not configured', async () => {
const { configuration } = await runServerless({
stageParameters: {
dev: {
timeout: 5,
},
staging: {
timeout: 10,
},
},
});
expect(configuration.provider.timeout).to.equal(5);
});

it('should report with an error missing address', async () => {
await initializeServerless();
it('should treat CLI parameters with a higher precedence than stage parameters', async () => {
const { configuration } = await runServerless({
cliParameters: ['region=eu-west-2'],
stageParameters: {
staging: {
region: 'eu-west-1',
},
},
stage: 'staging',
});
expect(configuration.provider.region).to.equal('eu-west-2');
});

it('should report with an error when the CLI parameter is invalid', async () => {
const { variablesMeta } = await runServerless({
cliParameters: ['region'],
});

expect(variablesMeta.get('provider\0region').error.code).to.equal('VARIABLE_RESOLUTION_ERROR');
});

it('should report with an error when the address is missing', async () => {
const { variablesMeta } = await runServerless();
expect(variablesMeta.get('custom\0missingAddress').error.code).to.equal(
'VARIABLE_RESOLUTION_ERROR'
);
});

it('should report with an error unsupported address', async () => {
await initializeServerless();
it('should report with an error when the address is not supported', async () => {
const { variablesMeta } = await runServerless();
expect(variablesMeta.get('custom\0unsupportedAddress').error.code).to.equal(
'VARIABLE_RESOLUTION_ERROR'
);
});

it('should report with an error a non-string address', async () => {
await initializeServerless();
it('should report with an error when the address it not a string', async () => {
const { variablesMeta } = await runServerless();
expect(variablesMeta.get('custom\0nonStringAddress').error.code).to.equal(
'VARIABLE_RESOLUTION_ERROR'
);
});

it('should still resolve variables when no Serverless instance is available', async () => {
const { variablesMeta } = await runServerless({
cliParameters: ['timeout=10'],
resolveWithoutInstance: true,
});
expect(variablesMeta.get('provider\0timeout')).to.have.property('variables');
expect(variablesMeta.get('provider\0timeout')).to.not.have.property('error');
});
});
Loading