Skip to content

Commit 33176a7

Browse files
author
wuychloe@amazon.com chloe1818
committed
Increased unit test coverage
1 parent b311cfd commit 33176a7

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

__tests__/code_artifacts.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
const { packageCodeArtifacts } = require('../index');
2+
const fs = require('fs/promises');
3+
const AdmZip = require('adm-zip');
4+
const path = require('path');
5+
const os = require('os');
6+
const validations = require('../validations');
27

38
jest.mock('@actions/core');
49
jest.mock('fs/promises');
510
jest.mock('adm-zip');
611
jest.mock('path');
12+
jest.mock('os');
13+
jest.mock('../validations');
714

815
describe('Code Artifacts Tests', () => {
916
test('should throw error when artifactsDir is not provided', async () => {
@@ -17,4 +24,37 @@ describe('Code Artifacts Tests', () => {
1724
test('should throw error when artifactsDir is empty string', async () => {
1825
await expect(packageCodeArtifacts('')).rejects.toThrow('Code artifacts directory path must be provided');
1926
});
27+
28+
test('should throw ZIP validation error when stat fails', async () => {
29+
const mockTimestamp = 1234567890;
30+
global.Date.now = jest.fn().mockReturnValue(mockTimestamp);
31+
32+
os.tmpdir = jest.fn().mockReturnValue('/mock/tmp');
33+
path.join.mockImplementation((...parts) => parts.join('/'));
34+
35+
fs.rm.mockResolvedValue(undefined);
36+
fs.mkdir.mockResolvedValue(undefined);
37+
fs.access.mockResolvedValue(undefined);
38+
fs.readdir.mockResolvedValue(['file1.js']);
39+
fs.cp.mockResolvedValue(undefined);
40+
41+
const mockZipInstance = {
42+
addLocalFile: jest.fn(),
43+
writeZip: jest.fn()
44+
};
45+
AdmZip.mockImplementation(() => mockZipInstance);
46+
47+
fs.readdir.mockImplementation((dir, options) => {
48+
if (options && options.withFileTypes) {
49+
return Promise.resolve([{ name: 'file1.js', isDirectory: () => false }]);
50+
}
51+
return Promise.resolve(['file1.js']);
52+
});
53+
54+
fs.stat.mockRejectedValue(new Error('Stat failed'));
55+
56+
validations.validateAndResolvePath = jest.fn().mockReturnValue('/resolved/path');
57+
58+
await expect(packageCodeArtifacts('/mock/artifacts')).rejects.toThrow('ZIP validation failed: Stat failed');
59+
});
2060
});

__tests__/dry_run_mode.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jest.mock('path');
1414
describe('Dry Run Mode Tests', () => {
1515
beforeEach(() => {
1616
jest.clearAllMocks();
17-
process.env.AWS_REGION = 'us-east-1';
17+
process.env.WS_REGION = 'us-east-1';
1818
});
1919

2020
test('should skip configuration updates in dry run mode when config changed', async () => {

__tests__/s3_bucket_operations.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,25 @@ describe('S3 Bucket Operations Tests', () => {
504504
expect(core.error).toHaveBeenCalledWith('Invalid bucket name: Invalid_Bucket_Name. Bucket names must follow S3 naming rules.');
505505
expect(core.error).toHaveBeenCalledWith('See s3-troubleshooting.md for S3 bucket naming rules.');
506506
});
507+
508+
test('should handle S3 upload failure with stack trace in createFunction', async () => {
509+
const uploadError = new Error('S3 upload failed');
510+
uploadError.stack = 'Error: S3 upload failed\n at uploadToS3';
511+
512+
jest.spyOn(mainModule, 'uploadToS3').mockRejectedValue(uploadError);
513+
514+
const mockClient = { send: jest.fn() };
515+
const inputs = {
516+
functionName: 'test-function',
517+
finalZipPath: '/path/to/file.zip',
518+
s3Bucket: 'test-bucket',
519+
s3Key: 'test-key.zip',
520+
region: 'us-east-1',
521+
role: 'arn:aws:iam::123456789012:role/test-role'
522+
};
523+
524+
await expect(mainModule.createFunction(mockClient, inputs, false)).rejects.toThrow('Cannot read properties of undefined');
525+
});
507526
});
508527

509528
describe('End-to-End S3 Deployment Flow', () => {
@@ -633,7 +652,7 @@ describe('S3 Bucket Operations Tests', () => {
633652
const functionName = core.getInput('function-name');
634653
const s3Bucket = core.getInput('s3-bucket');
635654
if (s3Bucket) {
636-
await mainModule.uploadToS3('file.zip', s3Bucket, 'key.zip', 'region', '123456789012'); // Adding expected bucket owner
655+
await mainModule.uploadToS3('file.zip', s3Bucket, 'key.zip', 'region');
637656
}
638657
expect(mainModule.uploadToS3).not.toHaveBeenCalled();
639658
});

0 commit comments

Comments
 (0)