Skip to content

Commit 87c9881

Browse files
authored
Merge pull request #1 from chloe1818/mainline
Mainline
2 parents 5a5063e + 79de6ca commit 87c9881

17 files changed

+3045
-2817
lines changed

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ jobs:
2323
- name: Configure AWS credentials
2424
uses: aws-actions/configure-aws-credentials@v2
2525
with:
26-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
27-
aws-region: us-east-1
28-
26+
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
2927
- name: Deploy Lambda function
3028
uses: aws-actions/amazon-lambda-deploy@v1
3129
with:
@@ -55,7 +53,6 @@ jobs:
5553
uses: aws-actions/configure-aws-credentials@v2
5654
with:
5755
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
58-
aws-region: us-east-1
5956

6057
- name: Deploy Lambda function via S3
6158
uses: aws-actions/amazon-lambda-deploy@v1
@@ -87,9 +84,7 @@ jobs:
8784
- name: Configure AWS credentials with OIDC
8885
uses: aws-actions/configure-aws-credentials@v2
8986
with:
90-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
91-
aws-region: us-east-1
92-
87+
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
9388
- name: Update Lambda configuration
9489
uses: aws-actions/amazon-lambda-deploy@v1
9590
with:
@@ -121,9 +116,7 @@ jobs:
121116
- name: Configure AWS credentials with OIDC
122117
uses: aws-actions/configure-aws-credentials@v2
123118
with:
124-
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
125-
aws-region: us-east-1
126-
119+
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
127120
- name: Validate Lambda deployment (no changes)
128121
uses: aws-actions/amazon-lambda-deploy@v1
129122
with:
@@ -137,7 +130,6 @@ jobs:
137130
| Name | Description | Required | Default |
138131
|------|-------------|----------|---------|
139132
| `function-name` | Name of the Lambda function | Yes | |
140-
| `region` | AWS region | No | (from AWS config) |
141133
| `code-artifacts-dir` | Path to a directory of code artifacts to zip and deploy | Yes | |
142134
| `s3-bucket` | S3 bucket name for Lambda deployment package. Uses S3 deployment method if provided | No | |
143135
| `s3-key` | S3 key (path) for the Lambda deployment package | No | Auto-generated |
@@ -197,7 +189,6 @@ jobs:
197189
uses: aws-actions/configure-aws-credentials@v2
198190
with:
199191
role-to-assume: arn:aws:iam::123456789012:role/GitHubActionRole
200-
aws-region: us-east-1
201192
202193
- name: Deploy Lambda function
203194
uses: aws-actions/amazon-lambda-deploy@v1

__tests__/code_artifacts.test.js

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
1-
// Make sure Jest mocks are defined before any imports
21
jest.mock('@actions/core');
32
jest.mock('@aws-sdk/client-lambda');
4-
5-
// Mock the waitForFunctionUpdated function in index.js
63
jest.mock('../index', () => {
74
const actualModule = jest.requireActual('../index');
85
const originalRun = actualModule.run;
96

107
return {
118
...actualModule,
12-
// Create a mock for run that calls specific behaviors for each test
139
run: jest.fn().mockImplementation(async () => {
14-
// This will be overridden in each test
1510
const fs = require('fs/promises');
1611
const AdmZip = require('adm-zip');
1712
const { glob } = require('glob');
1813
const core = require('@actions/core');
1914

20-
// Create a mock implementation that simulates the basic flow without timeout
2115
await fs.mkdir('/mock/cwd/lambda-package', { recursive: true });
2216
await glob('**/*', { cwd: '/mock/artifacts', dot: true });
2317
const zip = new AdmZip();
2418
zip.addLocalFolder('/mock/cwd/lambda-package');
2519

26-
// Log success
2720
core.info('Packaging code artifacts from /mock/artifacts');
2821
core.info('Lambda function deployment completed successfully');
2922
}),
@@ -36,8 +29,6 @@ jest.mock('../index', () => {
3629
waitForFunctionUpdated: jest.fn().mockResolvedValue(undefined)
3730
};
3831
});
39-
40-
// Mock fs/promises
4132
jest.mock('fs/promises', () => ({
4233
mkdir: jest.fn().mockResolvedValue(undefined),
4334
stat: jest.fn().mockImplementation(async (path) => ({
@@ -46,23 +37,17 @@ jest.mock('fs/promises', () => ({
4637
copyFile: jest.fn().mockResolvedValue(undefined),
4738
readFile: jest.fn().mockResolvedValue(Buffer.from('mock file content'))
4839
}));
49-
50-
// Create manual mocks for modules that may not be installed
5140
jest.mock('glob', () => ({
5241
glob: jest.fn().mockResolvedValue(['file1.js', 'directory/file2.js', 'directory'])
5342
}));
54-
55-
// Manual mock for AdmZip
5643
jest.mock('adm-zip', () =>
5744
jest.fn().mockImplementation(() => ({
5845
addLocalFolder: jest.fn(),
5946
writeZip: jest.fn()
6047
}))
6148
);
62-
6349
jest.mock('path');
6450

65-
// Now we can import modules
6651
const core = require('@actions/core');
6752
const { LambdaClient } = require('@aws-sdk/client-lambda');
6853
const fs = require('fs/promises');
@@ -71,21 +56,15 @@ const { glob } = require('glob');
7156
const AdmZip = require('adm-zip');
7257
const mainModule = require('../index');
7358

74-
// Increase the default timeout for all tests in this file
75-
jest.setTimeout(15000);
76-
77-
describe('Lambda Deployment Integration Tests', () => {
59+
describe('Code Artifacts Tests', () => {
7860
beforeEach(() => {
7961
jest.clearAllMocks();
8062

81-
// Mock process.cwd()
8263
process.cwd = jest.fn().mockReturnValue('/mock/cwd');
8364

84-
// Mock path.join to return predictable paths
8565
path.join.mockImplementation((...parts) => parts.join('/'));
8666
path.dirname.mockImplementation((p) => p.substring(0, p.lastIndexOf('/')));
8767

88-
// Mock core functions
8968
core.getInput.mockImplementation((name) => {
9069
const inputs = {
9170
'function-name': 'test-function',
@@ -101,7 +80,6 @@ describe('Lambda Deployment Integration Tests', () => {
10180
core.error.mockImplementation(() => {});
10281
core.setFailed.mockImplementation(() => {});
10382

104-
// Mock Lambda client
10583
const mockLambdaResponse = {
10684
$metadata: { httpStatusCode: 200 },
10785
Configuration: {
@@ -116,43 +94,34 @@ describe('Lambda Deployment Integration Tests', () => {
11694
});
11795

11896
it('should package artifacts and deploy to Lambda', async () => {
119-
// Set up the run mock for this specific test
12097
mainModule.run.mockImplementationOnce(async () => {
121-
// This simulates what the test expects to happen
12298
await fs.mkdir('/mock/cwd/lambda-package', { recursive: true });
12399
const files = await glob('**/*', { cwd: '/mock/artifacts', dot: true });
124100
const zip = new AdmZip();
125101
zip.addLocalFolder('/mock/cwd/lambda-package');
126102
core.info('Packaging code artifacts from /mock/artifacts');
127103
});
128104

129-
// Call the main function
130105
await mainModule.run();
131106

132-
// Verify temporary directory was created
133-
expect(fs.mkdir).toHaveBeenCalledWith('/mock/cwd/lambda-package', { recursive: true });
134107

135-
// Verify glob was called to find files
136-
expect(glob).toHaveBeenCalledWith('**/*', { cwd: '/mock/artifacts', dot: true });
108+
expect(fs.mkdir).toHaveBeenCalledWith('/mock/cwd/lambda-package', { recursive: true });
137109

138-
// Verify ZIP creation
110+
expect(glob).toHaveBeenCalledWith('**/*', { cwd: '/mock/artifacts', dot: true });
111+
139112
expect(AdmZip).toHaveBeenCalled();
140113
const zipInstance = AdmZip.mock.results[0].value;
141114
expect(zipInstance.addLocalFolder).toHaveBeenCalledWith('/mock/cwd/lambda-package');
142115

143-
// Verify appropriate logs were shown
144-
expect(core.info).toHaveBeenCalledWith(expect.stringContaining('Packaging code artifacts'));
145-
146-
// Verify no errors were reported
116+
expect(core.info).toHaveBeenCalledWith(expect.stringContaining('Packaging code artifacts'));
147117
expect(core.setFailed).not.toHaveBeenCalled();
148118
});
149119

150120
it('should handle artifacts packaging failure gracefully', async () => {
151-
// Make file operations throw an error
121+
152122
const packageError = new Error('Failed to create package');
153123
fs.mkdir.mockRejectedValueOnce(packageError);
154124

155-
// Set up the run mock for this specific test
156125
mainModule.run.mockImplementationOnce(async () => {
157126
try {
158127
await fs.mkdir('/mock/cwd/lambda-package', { recursive: true });
@@ -161,15 +130,13 @@ describe('Lambda Deployment Integration Tests', () => {
161130
}
162131
});
163132

164-
// Call the main function
165-
await mainModule.run();
166133

167-
// Verify error was logged
134+
await mainModule.run();
168135
expect(core.setFailed).toHaveBeenCalledWith(expect.stringContaining('Action failed with error'));
169136
});
170137

171138
it('should correctly use code-artifacts-dir when provided', async () => {
172-
// Update the mock with a different artifacts directory
139+
173140
core.getInput.mockImplementation((name) => {
174141
const inputs = {
175142
'function-name': 'test-function',
@@ -178,28 +145,21 @@ describe('Lambda Deployment Integration Tests', () => {
178145
'role': 'arn:aws:iam::123456789012:role/lambda-role',
179146
};
180147
return inputs[name] || '';
181-
});
148+
});
182149

183-
// Set up the run mock for this specific test
184-
mainModule.run.mockImplementationOnce(async () => {
185-
// In this test, we expect fs.mkdir and glob not to be called
186-
// Just do some final output to simulate success
150+
mainModule.run.mockImplementationOnce(async () => {
187151
core.info('Lambda function deployment completed successfully');
188152
});
189153

190-
// Call the main function
191-
await mainModule.run();
154+
await mainModule.run();
192155

193-
// Verify packaging functions were not called
194156
expect(fs.mkdir).not.toHaveBeenCalled();
195157
expect(glob).not.toHaveBeenCalled();
196-
197-
// Verify no errors were reported
198158
expect(core.setFailed).not.toHaveBeenCalled();
199159
});
200160

201161
it('should fail when code-artifacts-dir is missing', async () => {
202-
// Change the mock to return without code-artifacts-dir
162+
203163
core.getInput.mockImplementation((name) => {
204164
const inputs = {
205165
'function-name': 'test-function',
@@ -208,8 +168,7 @@ describe('Lambda Deployment Integration Tests', () => {
208168
};
209169
return inputs[name] || '';
210170
});
211-
212-
// Set up the run mock for this specific test
171+
213172
mainModule.run.mockImplementationOnce(async () => {
214173
const codeArtifactsDir = core.getInput('code-artifacts-dir');
215174

@@ -218,20 +177,16 @@ describe('Lambda Deployment Integration Tests', () => {
218177
return;
219178
}
220179

221-
// This shouldn't execute in this test
222180
await fs.mkdir('/mock/cwd/lambda-package', { recursive: true });
223181
});
224182

225-
// Call the main function
226183
await mainModule.run();
227184

228-
// Verify error was reported
229185
expect(core.setFailed).toHaveBeenCalledWith(
230186
'Code-artifacts-dir must be provided'
231187
);
232188

233-
// Verify packaging functions were not called
234189
expect(fs.mkdir).not.toHaveBeenCalled();
235190
expect(glob).not.toHaveBeenCalled();
236191
});
237-
});
192+
});

0 commit comments

Comments
 (0)