Skip to content

Commit

Permalink
Merge pull request #397 from guardian/aa-silence!
Browse files Browse the repository at this point in the history
refactor: Stop producing migration messages in TEST
  • Loading branch information
akash1810 authored Apr 8, 2021
2 parents 01e271b + 479db3d commit 5450280
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/constructs/core/migrating.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@aws-cdk/assert/jest";
import { SynthUtils } from "@aws-cdk/assert";
import type { BucketProps } from "@aws-cdk/aws-s3";
import { Bucket } from "@aws-cdk/aws-s3";
import { Logger } from "../../utils/logger";
import type { SynthedStack } from "../../utils/test";
import { simpleGuStackForTesting } from "../../utils/test";
import type { GuStatefulConstruct } from "./migrating";
Expand All @@ -23,11 +24,8 @@ We're calling it here to test the function in isolation.
*/

describe("GuMigratingResource", () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function -- we are testing `console.info` being called, we don't need to see the message
const info = jest.spyOn(console, "info").mockImplementation(() => {});

// eslint-disable-next-line @typescript-eslint/no-empty-function -- we are testing `console.warn` being called, we don't need to see the message
const warn = jest.spyOn(console, "warn").mockImplementation(() => {});
const info = jest.spyOn(Logger, "info");
const warn = jest.spyOn(Logger, "warn");

afterEach(() => {
warn.mockReset();
Expand Down
7 changes: 4 additions & 3 deletions src/constructs/core/migrating.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CfnElement, IConstruct } from "@aws-cdk/core";
import { Logger } from "../../utils/logger";
import type { GuStack } from "./stack";

export interface GuMigratingResource {
Expand Down Expand Up @@ -46,19 +47,19 @@ export const GuMigratingResource = {
}

if (isStateful) {
console.warn(
Logger.warn(
`GuStack has 'migratedFromCloudFormation' set to true. ${id} is a stateful construct and 'existingLogicalId' has not been set. ${id}'s logicalId will be auto-generated and consequently AWS will create a new resource rather than inheriting an existing one. This is not advised as downstream services, such as DNS, will likely need updating.`
);
}
} else {
if (existingLogicalId) {
console.warn(
Logger.warn(
`GuStack has 'migratedFromCloudFormation' set to false. ${id} has an 'existingLogicalId' set to ${existingLogicalId}. This will have no effect - the logicalId will be auto-generated. Set 'migratedFromCloudFormation' to true for 'existingLogicalId' to be observed.`
);
}

if (isStateful) {
console.info(
Logger.info(
`GuStack has 'migratedFromCloudFormation' set to false. ${id} is a stateful construct, it's logicalId will be auto-generated and AWS will create a new resource.`
);
}
Expand Down
18 changes: 18 additions & 0 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* A small wrapper around `console`.
*
* Messages will only get printed if NODE_ENV is not test.
* This is to reduce noise in a test environment.
* Jest's `--silent` flag isn't used as that has a global impact.
*/
export class Logger {
private static isTest = process.env.NODE_ENV === "test";

static info(message: string): void {
!Logger.isTest && console.info(message);
}

static warn(message: string): void {
!Logger.isTest && console.warn(message);
}
}

0 comments on commit 5450280

Please sign in to comment.