Skip to content

Commit c11c0bc

Browse files
fix: fixing function build issue + e2e tests (#1750)
fix #1747
1 parent cc23bdf commit c11c0bc

File tree

6 files changed

+105
-4
lines changed

6 files changed

+105
-4
lines changed

packages/amplify-e2e-tests/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/lib
1+
/lib
2+
.env
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require('../src/aws-matchers/'); // custom matcher for assertion
2+
import { initProjectWithProfile, deleteProject, amplifyPushAuth } from '../src/init';
3+
import { addHelloWorldFunction, functionBuild } from '../src/categories/function';
4+
import { createNewProjectDir, deleteProjectDir, getProjectMeta, getFunction } from '../src/utils';
5+
6+
describe('amplify add function', () => {
7+
let projRoot: string;
8+
beforeEach(() => {
9+
projRoot = createNewProjectDir();
10+
jest.setTimeout(1000 * 60 * 60); // 1 hour
11+
});
12+
13+
afterEach(async () => {
14+
await deleteProject(projRoot);
15+
deleteProjectDir(projRoot);
16+
});
17+
18+
it('init a project and add simple function', async () => {
19+
await initProjectWithProfile(projRoot, {});
20+
await addHelloWorldFunction(projRoot, {});
21+
await functionBuild(projRoot, {});
22+
await amplifyPushAuth(projRoot);
23+
const meta = getProjectMeta(projRoot);
24+
const { Arn: functionArn, Name: functionName, Region: region } = Object.keys(
25+
meta.function
26+
).map(key => meta.function[key])[0].output;
27+
expect(functionArn).toBeDefined();
28+
expect(functionName).toBeDefined();
29+
expect(region).toBeDefined();
30+
const cloudFunction = await getFunction(functionName, region);
31+
expect(cloudFunction.Configuration.FunctionArn).toEqual(functionArn);
32+
});
33+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
import * as nexpect from 'nexpect';
3+
import { join } from 'path';
4+
import * as fs from 'fs';
5+
6+
import { getCLIPath, isCI, getEnvVars } from '../utils';
7+
const defaultSettings = {
8+
projectName: 'CLI Function test',
9+
};
10+
11+
12+
export function addHelloWorldFunction(
13+
cwd: string,
14+
settings: any,
15+
verbose: boolean = !isCI()
16+
) {
17+
return new Promise((resolve, reject) => {
18+
nexpect
19+
.spawn(getCLIPath(), ['add', 'function'], { cwd, stripColors: true, verbose })
20+
.wait('Provide a friendly name for your resource to be used as a label')
21+
.sendline('\r')
22+
.wait('Provide the AWS Lambda function name')
23+
.sendline('\r')
24+
.wait('Choose the function template that you want to use')
25+
.sendline('\r')
26+
.wait('Do you want to access other resources created in this project')
27+
.sendline('n')
28+
.wait('Do you want to edit the local lambda function now')
29+
.sendline('n')
30+
.sendEof()
31+
// tslint:disable-next-line
32+
.run(function(err: Error) {
33+
if (!err) {
34+
resolve();
35+
} else {
36+
reject(err);
37+
}
38+
})
39+
})
40+
}
41+
42+
export function functionBuild(
43+
cwd: string,
44+
settings: any,
45+
verbose: boolean = !isCI()
46+
) {
47+
return new Promise((resolve, reject) => {
48+
nexpect
49+
.spawn(getCLIPath(), ['function', 'build'], { cwd, stripColors: true, verbose })
50+
.wait('Are you sure you want to continue building the resources?')
51+
.sendline('Y')
52+
.sendEof()
53+
// tslint:disable-next-line
54+
.run(function(err: Error) {
55+
if (!err) {
56+
resolve();
57+
} else {
58+
reject(err);
59+
}
60+
})
61+
})
62+
}

packages/amplify-e2e-tests/src/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdirSync } from 'fs';
33
import * as rimraf from 'rimraf';
44
import { config } from 'dotenv';
55
export { default as getProjectMeta } from './projectMeta';
6-
export { getUserPool, getUserPoolClients, getBot } from './sdk-calls'
6+
export { getUserPool, getUserPoolClients, getBot, getFunction } from './sdk-calls'
77

88
// run dotenv config to update env variable
99
config();

packages/amplify-e2e-tests/src/utils/sdk-calls.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,9 @@ const getBot = async (botName: string, region: string) => {
4141
return await service.getBot({ name: botName, versionOrAlias: '$LATEST' }).promise();
4242
};
4343

44-
export { getUserPool, getUserPoolClients, getBot };
44+
const getFunction = async (functionName: string, region: string) => {
45+
const service = new AWS.Lambda({ region });
46+
return await service.getFunction({ FunctionName: functionName }).promise();
47+
};
48+
49+
export { getUserPool, getUserPoolClients, getBot, getFunction };

packages/amplify-provider-awscloudformation/lib/build-resources.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async function buildResource(context, resource) {
2222
const distDir = path.normalize(path.join(backEndDir, category, resourceName, 'dist'));
2323

2424
let zipFilename = resource.distZipFilename;
25-
let zipFilePath = zipFilename ? path.normalize(path.join(distDir, zipFilename)) : '';
25+
let zipFilePath = zipFilename ? path.normalize(path.join(distDir, 'latest-build.zip')) : '';
2626

2727
if (
2828
!resource.lastBuildTimeStamp ||

0 commit comments

Comments
 (0)