From 91baabedb2f8f4a58f7b4ee92ba62e30424be6b9 Mon Sep 17 00:00:00 2001 From: Setu Shah Date: Sat, 18 Dec 2021 02:27:44 -0800 Subject: [PATCH] test: Update tests to use updated `Bundling` class --- .../aws-lambda-python/test/bundling.test.ts | 12 ++-- .../aws-lambda-python/test/function.test.ts | 60 ++++++++++--------- .../aws-lambda-python/test/layer.test.ts | 28 +++++---- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts b/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts index 5043a1501853e..cbc384aec599c 100644 --- a/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts +++ b/packages/@aws-cdk/aws-lambda-python/test/bundling.test.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as path from 'path'; import { Architecture, Code, Runtime } from '@aws-cdk/aws-lambda'; import { DockerImage, FileSystem } from '@aws-cdk/core'; -import { stageDependencies, bundle } from '../lib/bundling'; +import { stageDependencies, Bundling } from '../lib/bundling'; jest.spyOn(Code, 'fromAsset'); jest.spyOn(DockerImage, 'fromBuild'); @@ -26,7 +26,7 @@ beforeEach(() => { test('Bundling a function without dependencies', () => { const entry = path.join(__dirname, 'lambda-handler-nodeps'); - bundle({ + Bundling.bundle({ entry: entry, runtime: Runtime.PYTHON_3_7, architecture: Architecture.X86_64, @@ -53,7 +53,7 @@ test('Bundling a function without dependencies', () => { test('Bundling a function with requirements.txt installed', () => { const entry = path.join(__dirname, 'lambda-handler'); - bundle({ + Bundling.bundle({ entry: entry, runtime: Runtime.PYTHON_3_7, architecture: Architecture.X86_64, @@ -73,7 +73,7 @@ test('Bundling a function with requirements.txt installed', () => { test('Bundling Python 2.7 with requirements.txt installed', () => { const entry = path.join(__dirname, 'lambda-handler'); - bundle({ + Bundling.bundle({ entry: entry, runtime: Runtime.PYTHON_2_7, architecture: Architecture.X86_64, @@ -94,7 +94,7 @@ test('Bundling Python 2.7 with requirements.txt installed', () => { test('Bundling a layer with dependencies', () => { const entry = path.join(__dirname, 'lambda-handler'); - bundle({ + Bundling.bundle({ entry: entry, runtime: Runtime.PYTHON_3_9, architecture: Architecture.X86_64, @@ -114,7 +114,7 @@ test('Bundling a layer with dependencies', () => { test('Bundling a python code layer', () => { const entry = path.join(__dirname, 'lambda-handler-nodeps'); - bundle({ + Bundling.bundle({ entry: path.join(entry, '.'), runtime: Runtime.PYTHON_3_9, architecture: Architecture.X86_64, diff --git a/packages/@aws-cdk/aws-lambda-python/test/function.test.ts b/packages/@aws-cdk/aws-lambda-python/test/function.test.ts index c0636329b2ecc..5028b70925acd 100644 --- a/packages/@aws-cdk/aws-lambda-python/test/function.test.ts +++ b/packages/@aws-cdk/aws-lambda-python/test/function.test.ts @@ -2,37 +2,39 @@ import { Template } from '@aws-cdk/assertions'; import { Code, Runtime } from '@aws-cdk/aws-lambda'; import { AssetHashType, AssetOptions, Stack } from '@aws-cdk/core'; import { PythonFunction } from '../lib'; -import { bundle } from '../lib/bundling'; +import { Bundling } from '../lib/bundling'; jest.mock('../lib/bundling', () => { return { - bundle: jest.fn().mockImplementation((options: AssetOptions): Code => { - const mockObjectKey = (() => { - const hashType = options.assetHashType ?? (options.assetHash ? 'custom' : 'source'); - switch (hashType) { - case 'source': return 'SOURCE_MOCK'; - case 'output': return 'OUTPUT_MOCK'; - case 'custom': { - if (!options.assetHash) { throw new Error('no custom hash'); } - return options.assetHash; + Bundling: { + bundle: jest.fn().mockImplementation((options: AssetOptions): Code => { + const mockObjectKey = (() => { + const hashType = options.assetHashType ?? (options.assetHash ? 'custom' : 'source'); + switch (hashType) { + case 'source': return 'SOURCE_MOCK'; + case 'output': return 'OUTPUT_MOCK'; + case 'custom': { + if (!options.assetHash) { throw new Error('no custom hash'); } + return options.assetHash; + } } - } - - throw new Error('unexpected asset hash type'); - })(); - - return { - isInline: false, - bind: () => ({ - s3Location: { - bucketName: 'mock-bucket-name', - objectKey: mockObjectKey, - }, - }), - bindToResource: () => { return; }, - }; - }), - hasDependencies: jest.fn().mockReturnValue(false), + + throw new Error('unexpected asset hash type'); + })(); + + return { + isInline: false, + bind: () => ({ + s3Location: { + bucketName: 'mock-bucket-name', + objectKey: mockObjectKey, + }, + }), + bindToResource: () => { return; }, + }; + }), + hasDependencies: jest.fn().mockReturnValue(false), + }, }; }); @@ -47,7 +49,7 @@ test('PythonFunction with defaults', () => { entry: 'test/lambda-handler', }); - expect(bundle).toHaveBeenCalledWith(expect.objectContaining({ + expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({ entry: expect.stringMatching(/aws-lambda-python\/test\/lambda-handler$/), outputPathSuffix: '.', })); @@ -64,7 +66,7 @@ test('PythonFunction with index in a subdirectory', () => { handler: 'custom_handler', }); - expect(bundle).toHaveBeenCalledWith(expect.objectContaining({ + expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({ entry: expect.stringMatching(/aws-lambda-python\/test\/lambda-handler-sub$/), outputPathSuffix: '.', })); diff --git a/packages/@aws-cdk/aws-lambda-python/test/layer.test.ts b/packages/@aws-cdk/aws-lambda-python/test/layer.test.ts index 4f0199878a205..501f2a27c696b 100644 --- a/packages/@aws-cdk/aws-lambda-python/test/layer.test.ts +++ b/packages/@aws-cdk/aws-lambda-python/test/layer.test.ts @@ -1,22 +1,24 @@ import * as path from 'path'; import { Runtime } from '@aws-cdk/aws-lambda'; import { Stack } from '@aws-cdk/core'; -import { stageDependencies, bundle } from '../lib/bundling'; +import { stageDependencies, Bundling } from '../lib/bundling'; import { PythonLayerVersion } from '../lib/layer'; jest.mock('../lib/bundling', () => { return { - bundle: jest.fn().mockReturnValue({ - bind: () => { - return { - s3Location: { - bucketName: 'bucket', - objectKey: 'key', - }, - }; - }, - bindToResource: () => { return; }, - }), + Bundling: { + bundle: jest.fn().mockReturnValue({ + bind: () => { + return { + s3Location: { + bucketName: 'bucket', + objectKey: 'key', + }, + }; + }, + bindToResource: () => { return; }, + }), + }, stageDependencies: jest.fn().mockReturnValue(true), }; }); @@ -37,7 +39,7 @@ test('Bundling a layer from files', () => { entry, }); - expect(bundle).toHaveBeenCalledWith(expect.objectContaining({ + expect(Bundling.bundle).toHaveBeenCalledWith(expect.objectContaining({ entry, outputPathSuffix: 'python', }));