diff --git a/packages/aws-cdk-lib/aws-lambda/test/code.test.ts b/packages/aws-cdk-lib/aws-lambda/test/code.test.ts index 7fcc937ed7ee3..cdd393c271987 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/code.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/code.test.ts @@ -16,6 +16,20 @@ describe('code', () => { }); describe('lambda.Code.fromAsset', () => { + test('fails if path is empty', () => { + // GIVEN + const fileAsset = lambda.Code.fromAsset(''); + + // THEN + expect(() => defineFunction(fileAsset)).toThrow(/Asset path cannot be empty/); + }); + test('fails if path does not exist', () => { + // GIVEN + const fileAsset = lambda.Code.fromAsset('/path/not/found/' + Math.random() * 999999); + + // THEN + expect(() => defineFunction(fileAsset)).toThrow(/Cannot find asset/); + }); test('fails if a non-zip asset is used', () => { // GIVEN const fileAsset = lambda.Code.fromAsset(path.join(__dirname, 'my-lambda-handler', 'index.py')); diff --git a/packages/aws-cdk-lib/aws-s3-assets/lib/asset.ts b/packages/aws-cdk-lib/aws-s3-assets/lib/asset.ts index d20059505d674..5993a9a2a06e9 100644 --- a/packages/aws-cdk-lib/aws-s3-assets/lib/asset.ts +++ b/packages/aws-cdk-lib/aws-s3-assets/lib/asset.ts @@ -137,6 +137,10 @@ export class Asset extends Construct implements cdk.IAsset { constructor(scope: Construct, id: string, props: AssetProps) { super(scope, id); + if (!props.path) { + throw new Error('Asset path cannot be empty'); + } + this.isBundled = props.bundling != null; // stage the asset source (conditionally). diff --git a/packages/aws-cdk-lib/aws-s3-assets/test/asset.test.ts b/packages/aws-cdk-lib/aws-s3-assets/test/asset.test.ts index 4aa70b59bf24d..77d174956393e 100644 --- a/packages/aws-cdk-lib/aws-s3-assets/test/asset.test.ts +++ b/packages/aws-cdk-lib/aws-s3-assets/test/asset.test.ts @@ -143,11 +143,18 @@ test('"readers" or "grantRead" can be used to grant read permissions on the asse }); }); +test('fails if path is empty', () => { + const stack = new cdk.Stack(); + expect(() => new Asset(stack, 'MyDirectory', { + path: '', + })).toThrow(/Asset path cannot be empty/); +}); + test('fails if directory not found', () => { const stack = new cdk.Stack(); expect(() => new Asset(stack, 'MyDirectory', { path: '/path/not/found/' + Math.random() * 999999, - })).toThrow(); + })).toThrow(/Cannot find asset/); }); test('multiple assets under the same parent', () => {