Skip to content
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

feat: ignore metadata #37

Merged
merged 2 commits into from
Aug 14, 2024
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,27 @@ test('propertyMatchers', () => {
});
```

## Ignore Metadata

With this enabled CloudFormation metadata both on Stack and Resource level will be ignored.
Metadata is often added by Aspects or other libraries.

```typescript
import { Stack } from 'aws-cdk-lib/core';
import { Bucket } from 'aws-cdk-lib/aws-s3';
import 'jest-cdk-snapshot';

test('default setup', () => {
const stack = new Stack();

new Bucket(stack, 'Foo');

expect(stack).toMatchCdkSnapshot({
ignoreMetadata: true,
});
});
```

## License

[MIT](LICENSE)
12 changes: 12 additions & 0 deletions src/__tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ exports[`ignore current version 1`] = `
}
`;

exports[`metadata should not be included if skipped 1`] = `
{
"Resources": {
"FooDFE0DD70": {
"DeletionPolicy": "Retain",
"Type": "AWS::S3::Bucket",
"UpdateReplacePolicy": "Retain",
},
},
}
`;

exports[`multiple resources 1`] = `
{
"Resources": {
Expand Down
40 changes: 39 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { CfnOutput, CfnParameter, Stack } from "aws-cdk-lib";
import {
Aspects,
CfnOutput,
CfnParameter,
CfnResource,
IAspect,
Stack,
} from "aws-cdk-lib";
import { Code, Function, Runtime } from "aws-cdk-lib/aws-lambda";
import { AccountPrincipal } from "aws-cdk-lib/aws-iam";
import { Bucket } from "aws-cdk-lib/aws-s3";
Expand Down Expand Up @@ -143,3 +150,34 @@ test("show bootstrap version if ignoreBootstrapVersion is explicitly false", ()
ignoreBootstrapVersion: false,
});
});

test("metadata should not be included if skipped", () => {
const stack = new Stack();
// There is different methods and places where metadata can be added. We want to ensure none leave a trace
// adding metadata via Aspect and cfnOptions
const dataAdder: IAspect = {
visit: (node) => {
if (CfnResource.isCfnResource(node)) {
node.cfnOptions.metadata = {
...node.cfnOptions.metadata,
["dummyKey"]: "dummyValue",
};
}
},
};
Aspects.of(stack).add(dataAdder);

// Add metadata on Stack level
stack.addMetadata("stackMeta", "dummy");

// Add metadata on different resource levels
const bucket = new Bucket(stack, "Foo");
bucket.node.addMetadata("nodeMetadata", "dummy");
if (bucket.node.defaultChild) {
bucket.node.defaultChild.node.addMetadata("resourceMetadata", "dummy");
}

expect(stack).toMatchCdkSnapshot({
ignoreMetadata: true,
});
});
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export type Options = StageSynthesisOptions & {
* Defaults to `true`.
*/
ignoreBootstrapVersion?: boolean;

/**
* Ignore Metadata
*/
ignoreMetadata?: boolean;
};

const currentVersionRegex = /^(.+CurrentVersion[0-9A-F]{8})[0-9a-f]{32}$/;
Expand Down Expand Up @@ -121,6 +126,7 @@ const convertStack = (stack: Stack, options: Options = {}) => {
ignoreAssets = false,
ignoreBootstrapVersion = true,
ignoreCurrentVersion = false,
ignoreMetadata = false,
subsetResourceTypes,
subsetResourceKeys,
...synthOptions
Expand Down Expand Up @@ -184,6 +190,19 @@ const convertStack = (stack: Stack, options: Options = {}) => {
}
}

if (ignoreMetadata && template.Metadata) {
delete template.Metadata;
}

if (ignoreMetadata && template.Resources) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Object.values(template.Resources).forEach((resource: any) => {
if (resource?.Metadata) {
delete resource.Metadata;
}
});
}

return yaml ? jsYaml.safeDump(template) : template;
};

Expand Down