Skip to content

Commit

Permalink
fix(cli): equals sign in a tag value is dropped (#27130)
Browse files Browse the repository at this point in the history
When overriding tags from the CLI the tag key-value string is split by an equal sign and the left and right parts are used for key and value respectively. The documentation for the [String.prototype.split()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) states that the second parameter sets a limit of the returned values after execution:
> If provided, splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. Any leftover text is not included in the array at all.

So basically if we have `foo=bar=test` and we apply a split with a limit of 2 (`"foo=bar=test".split("=", 2)`) we will get the following array `["foo", "bar"]` instead of the expected one`["foo", "bar=test"]`. 

There has been the same problem with the context values and since it is the same problem the solution is also the same: #5773 

With this fix the `context` and `tags` get the same behaviour when parsing the passed values.

Closes #21003 

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
snaksa authored Sep 19, 2023
1 parent e1d4187 commit b7eeda6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/aws-cdk/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export class Settings {
const tags: Tag[] = [];

for (const assignment of nonEmptyTags) {
const parts = assignment.split('=', 2);
const parts = assignment.split(/=(.*)/, 2);
if (parts.length === 2) {
debug('CLI argument tags: %s=%s', parts[0], parts[1]);
tags.push({
Expand Down
21 changes: 21 additions & 0 deletions packages/aws-cdk/test/settings.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable import/order */
import { Command, Context, Settings } from '../lib/settings';
import { Tag } from '../lib/cdk-toolkit';

test('can delete values from Context object', () => {
// GIVEN
Expand Down Expand Up @@ -81,6 +82,26 @@ test('can parse string context from command line arguments with equals sign in v
expect(settings2.get(['context']).foo).toEqual( 'bar=');
});

test('can parse tag values from command line arguments', () => {
// GIVEN
const settings1 = Settings.fromCommandLineArguments({ tags: ['foo=bar'], _: [Command.DEPLOY] });
const settings2 = Settings.fromCommandLineArguments({ tags: ['foo='], _: [Command.DEPLOY] });

// THEN
expect(settings1.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('bar');
expect(settings2.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('');
});

test('can parse tag values from command line arguments with equals sign in value', () => {
// GIVEN
const settings1 = Settings.fromCommandLineArguments({ tags: ['foo==bar='], _: [Command.DEPLOY] });
const settings2 = Settings.fromCommandLineArguments({ tags: ['foo=bar='], _: [Command.DEPLOY] });

// THEN
expect(settings1.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('=bar=');
expect(settings2.get(['tags']).find((tag: Tag) => tag.Key === 'foo').Value).toEqual('bar=');
});

test('bundling stacks defaults to an empty list', () => {
// GIVEN
const settings = Settings.fromCommandLineArguments({
Expand Down

0 comments on commit b7eeda6

Please sign in to comment.