Skip to content

Commit 353cd4c

Browse files
committed
Add template builder unit tests
1 parent 4568804 commit 353cd4c

File tree

4 files changed

+101
-22
lines changed

4 files changed

+101
-22
lines changed

deployment/cdk-solution-helper/asset-packager/__tests__/handler.test.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,67 @@ import { existsSync } from "fs";
88
import { mkdir, writeFile, rm } from "node:fs/promises";
99
import { handler } from "../index";
1010

11-
const mockAssetDirectoryPath = path.join(__dirname, "mock-dir");
12-
const mockOutputPath = path.join(__dirname, "mock-dir-output");
11+
const __assetDirectoryPath = path.join(__dirname, "mock-dir");
12+
const __outputPath = path.join(__dirname, "mock-dir-output");
1313
describe("Handler", () => {
1414
beforeAll(async function Arrange() {
15-
await rm(mockAssetDirectoryPath, { recursive: true, force: true });
16-
await rm(mockOutputPath, { recursive: true, force: true });
17-
await mkdir(mockAssetDirectoryPath);
18-
await mkdir(mockOutputPath);
15+
await rm(__assetDirectoryPath, { recursive: true, force: true });
16+
await rm(__outputPath, { recursive: true, force: true });
17+
await mkdir(__assetDirectoryPath);
18+
await mkdir(__outputPath);
1919
});
2020

2121
it("should fail in absence of path inputs ", async function () {
2222
expect.assertions(2);
2323
await expect(handler("", "")).rejects.toThrowError("undefined input path");
24-
await expect(handler(undefined, undefined)).rejects.toThrowError("undefined input path");
24+
await expect(handler(undefined, undefined)).rejects.toThrowError(
25+
"undefined input path"
26+
);
2527
});
2628

2729
it("should fail for invalid cdk asset path", async function () {
2830
expect.assertions(1);
29-
await expect(handler("invalidPath", mockOutputPath)).rejects.toThrowError(/(ENOENT).+(invalidPath)/g);
31+
await expect(handler("invalidPath", __outputPath)).rejects.toThrowError(
32+
/(ENOENT).+(invalidPath)/g
33+
);
3034
});
3135

3236
it("should succeed if cdk assets not found", async function () {
33-
await expect(handler(mockAssetDirectoryPath, "invalidPath")).resolves.toBeUndefined();
37+
await expect(
38+
handler(__assetDirectoryPath, "invalidPath")
39+
).resolves.toBeUndefined();
3440
});
3541

3642
it("should fail for invalid output path", async function () {
3743
// Arrange
3844
expect.assertions(1);
39-
const mockAssetPath = path.join(mockAssetDirectoryPath, "./asset.cdkAsset.zip");
45+
const mockAssetPath = path.join(
46+
__assetDirectoryPath,
47+
"./asset.cdkAsset.zip"
48+
);
4049
await writeFile(mockAssetPath, "NoOp");
4150
// Act, Assert
42-
await expect(handler(mockAssetDirectoryPath, "invalidPath")).rejects.toThrowError(/(ENOENT).+(invalidPath)/g);
51+
await expect(
52+
handler(__assetDirectoryPath, "invalidPath")
53+
).rejects.toThrowError(/(ENOENT).+(invalidPath)/g);
4354
// Cleanup
4455
await rm(mockAssetPath);
4556
});
4657

4758
it("should successfully stage zip for valid paths", async function () {
4859
const zipName = "asset.cdkAsset.zip";
49-
const mockAssetPath = path.join(mockAssetDirectoryPath, zipName);
60+
const mockAssetPath = path.join(__assetDirectoryPath, zipName);
5061
await writeFile(mockAssetPath, "NoOp");
51-
await expect(handler(mockAssetDirectoryPath, mockOutputPath)).resolves.toBeUndefined();
52-
expect(existsSync(path.join(mockOutputPath, zipName.split("asset.").pop()!))).toBe(true);
62+
await expect(
63+
handler(__assetDirectoryPath, __outputPath)
64+
).resolves.toBeUndefined();
65+
expect(
66+
existsSync(path.join(__outputPath, zipName.split("asset.").pop()!))
67+
).toBe(true);
5368
});
5469

5570
afterAll(async function Cleanup() {
56-
await rm(mockAssetDirectoryPath, { recursive: true, force: true });
57-
await rm(mockOutputPath, { recursive: true, force: true });
71+
await rm(__assetDirectoryPath, { recursive: true, force: true });
72+
await rm(__outputPath, { recursive: true, force: true });
5873
});
5974
});

deployment/cdk-solution-helper/jest.config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const config: Config = {
1111
transform: {
1212
"^.+\\.(t)sx?$": "ts-jest",
1313
},
14-
collectCoverageFrom: ["**/*.ts", "!**/*.test.ts", "!./jest.config.ts", "!./jest.setup.ts"],
14+
collectCoverageFrom: [
15+
"**/*.ts",
16+
"!**/*.test.ts",
17+
"!./jest.config.ts",
18+
"!./jest.setup.ts",
19+
],
1520
coverageReporters: [["lcov", { projectRoot: "../" }], "text"],
1621
};
1722

deployment/cdk-solution-helper/template-builder/__tests__/handler.test.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,56 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
// TODO
6+
import { mkdir, rm } from "node:fs/promises";
7+
import { handler } from "../index";
8+
import path from "path";
9+
10+
const __templateDirectoryPath = path.join(__dirname, "myTemplateDir");
11+
const __solutionName = "sih";
12+
const __solutionVersion = "myMockVersion";
13+
const __lambdaAssetBucketName = "mockAssetBucket";
14+
const __template = {};
15+
describe("Handler", () => {
16+
beforeAll(async function Arrange() {
17+
await rm(__templateDirectoryPath, { recursive: true, force: true });
18+
await mkdir(__templateDirectoryPath);
19+
});
20+
21+
it("should fail in absence of path inputs ", async function () {
22+
expect.assertions(2);
23+
await expect(handler("", "", "", "")).rejects.toThrowError(
24+
"undefined arguments"
25+
);
26+
await expect(
27+
handler(undefined, undefined, undefined, undefined)
28+
).rejects.toThrowError("undefined arguments");
29+
});
30+
31+
it("should succeed for invalid template directory path", async function () {
32+
await expect(
33+
handler(
34+
"invalidPath",
35+
__solutionName,
36+
__lambdaAssetBucketName,
37+
__solutionVersion
38+
)
39+
).resolves.toBeUndefined();
40+
});
41+
42+
it("should succeed if templates not found", async function () {
43+
await expect(
44+
handler(
45+
__templateDirectoryPath,
46+
__solutionName,
47+
__lambdaAssetBucketName,
48+
__solutionVersion
49+
)
50+
).resolves.toBeUndefined();
51+
});
52+
53+
xit("should update template resources", function () {});
54+
55+
afterAll(async function Cleanup() {
56+
await rm(__templateDirectoryPath, { recursive: true, force: true });
57+
});
58+
});

deployment/cdk-solution-helper/template-builder/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@ import { writeFile } from "node:fs/promises";
77
import { TemplateBuilder } from "./template-builder";
88

99
export async function handler(
10-
templatePath: string | undefined,
10+
templateDirectoryPath: string | undefined,
1111
solutionName: string | undefined,
1212
lambdaBucket: string | undefined,
1313
version: string | undefined
1414
) {
15-
if (!templatePath || !solutionName || !lambdaBucket || !version) throw new Error("undefined arguments");
16-
const CDKHelper = new TemplateBuilder(templatePath, solutionName, lambdaBucket, version);
15+
if (!templateDirectoryPath || !solutionName || !lambdaBucket || !version)
16+
throw new Error("undefined arguments");
17+
const CDKHelper = new TemplateBuilder(
18+
templateDirectoryPath,
19+
solutionName,
20+
lambdaBucket,
21+
version
22+
);
1723
const templatePaths = await CDKHelper.getTemplateFilePaths();
1824
for (const path of templatePaths) {
1925
const templateContents = await CDKHelper.parseJsonTemplate(path);
20-
const templateWithUpdatedLambdaCodeReference = await CDKHelper.updateLambdaAssetReference(templateContents);
26+
const templateWithUpdatedLambdaCodeReference =
27+
await CDKHelper.updateLambdaAssetReference(templateContents);
2128
const templateWithUpdatedBucketReference = CDKHelper.updateBucketReference(
2229
JSON.stringify(templateWithUpdatedLambdaCodeReference, null, 2)
2330
);

0 commit comments

Comments
 (0)