Skip to content

Commit

Permalink
fix: bug in yarn packager rebase logic; legacy logger bug; fix integr…
Browse files Browse the repository at this point in the history
…ation tests
  • Loading branch information
floydspace committed Sep 24, 2022
1 parent f60b9f5 commit d976e9f
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 115 deletions.
1 change: 1 addition & 0 deletions e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.test-artifacts/
26 changes: 13 additions & 13 deletions e2e/Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
test-e2e: test-e2e-minimal test-e2e-individually test-e2e-complete

build:
build:
npm run build
npm link

test-e2e-minimal: build
rm -fr ./e2e/minimal && mkdir ./e2e/minimal && rsync -r ./examples/minimal/ ./e2e/minimal/
cd ./e2e/minimal && npm i && npm link serverless-esbuild && npx sls package
cd ./e2e/minimal/.serverless && unzip minimal-example.zip
rm -fr ./e2e/.test-artifacts && mkdir -p ./e2e/.test-artifacts/minimal && rsync -r ./examples/minimal/ ./e2e/.test-artifacts/minimal/
cd ./e2e/.test-artifacts/minimal && npm i && npm link serverless-esbuild && npx sls package
cd ./e2e/.test-artifacts/minimal/.serverless && unzip minimal-example.zip
npx jest -c jest.config.e2e.js --ci ./e2e/minimal.test.ts
rm -fr ./e2e/minimal
rm -fr ./e2e/.test-artifacts

test-e2e-individually: build
rm -fr ./e2e/individually && mkdir ./e2e/individually && rsync -r ./examples/individually/ ./e2e/individually/
cd ./e2e/individually && npm i && npm link serverless-esbuild && npx sls package
cd ./e2e/individually/.serverless && unzip hello1.zip && unzip hello2.zip
rm -fr ./e2e/.test-artifacts && mkdir -p ./e2e/.test-artifacts/individually && rsync -r ./examples/individually/ ./e2e/.test-artifacts/individually/
cd ./e2e/.test-artifacts/individually && yarn install && npm link serverless-esbuild && npx sls package
cd ./e2e/.test-artifacts/individually/.serverless && unzip hello1.zip && unzip hello2.zip
npx jest -c jest.config.e2e.js --ci ./e2e/individually.test.ts
rm -fr ./e2e/individually
rm -fr ./e2e/.test-artifacts

test-e2e-complete: build
rm -fr ./e2e/complete && mkdir ./e2e/complete && rsync -r ./examples/complete/ ./e2e/complete/
cd ./e2e/complete && npm i && npm link serverless-esbuild && npx sls package
cd ./e2e/complete/.serverless && unzip complete-example.zip
rm -fr ./e2e/.test-artifacts && mkdir -p ./e2e/.test-artifacts/complete && rsync -r ./examples/complete/ ./e2e/.test-artifacts/complete/
cd ./e2e/.test-artifacts/complete && npm i && npm link serverless-esbuild && npx sls package
cd ./e2e/.test-artifacts/complete/.serverless && unzip complete-example.zip
npx jest -c jest.config.e2e.js --ci ./e2e/complete.test.ts
rm -fr ./e2e/complete
rm -fr ./e2e/.test-artifacts
27 changes: 6 additions & 21 deletions e2e/__snapshots__/individually.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,23 @@ exports[`individually 4`] = `
Object {
"Hello1LambdaFunctionQualifiedArn": Object {
"Description": "Current Lambda function version",
"Export": Object {
"Name": "sls-serverless-example-dev-Hello1LambdaFunctionQualifiedArn",
},
"Value": Object {
"Ref": "Hello1LambdaVersionRov7i9fIwcn3AXADkyUVGUVL7O5AeThTAlrxWsLjsEo",
"Ref": Any<String>,
},
},
"Hello2LambdaFunctionQualifiedArn": Object {
"Description": "Current Lambda function version",
"Export": Object {
"Name": "sls-serverless-example-dev-Hello2LambdaFunctionQualifiedArn",
},
"Value": Object {
"Ref": "Hello2LambdaVersion3Oj08lv37eh2o6tSuQ1RBHFHv928o5VNv2ag0rSE",
"Ref": Any<String>,
},
},
"ServerlessDeploymentBucketName": Object {
"Export": Object {
"Name": "sls-serverless-example-dev-ServerlessDeploymentBucketName",
},
"Value": Object {
"Ref": "ServerlessDeploymentBucket",
},
},
"ServiceEndpoint": Object {
"Description": "URL of the service endpoint",
"Export": Object {
"Name": "sls-serverless-example-dev-ServiceEndpoint",
},
"Value": Object {
"Fn::Join": Array [
"",
Expand Down Expand Up @@ -72,9 +60,6 @@ Object {
exports[`individually 5`] = `
Object {
"ApiGatewayMethodHelloGet": Object {
"DependsOn": Array [
"Hello2LambdaPermissionApiGateway",
],
"Properties": Object {
"ApiKeyRequired": false,
"AuthorizationType": "NONE",
Expand Down Expand Up @@ -206,10 +191,10 @@ Object {
},
"Type": "AWS::Lambda::Permission",
},
"Hello1LambdaVersionRov7i9fIwcn3AXADkyUVGUVL7O5AeThTAlrxWsLjsEo": Object {
"Hello1LambdaVersioncqcVSV8tNXHbeUvHuv4CZO7wqEZT94UBnDRbN5T8": Object {
"DeletionPolicy": "Retain",
"Properties": Object {
"CodeSha256": "zvn3/B4BtuYoEDUiWabnbK+IBitTWrDVHjhmVsdilTk=",
"CodeSha256": Any<String>,
"FunctionName": Object {
"Ref": "Hello1LambdaFunction",
},
Expand Down Expand Up @@ -284,10 +269,10 @@ Object {
},
"Type": "AWS::Lambda::Permission",
},
"Hello2LambdaVersion3Oj08lv37eh2o6tSuQ1RBHFHv928o5VNv2ag0rSE": Object {
"Hello2LambdaVersionZtozZcKHXOdGSOYyxQFNlTQvw0eS0u0v3utikJceH80": Object {
"DeletionPolicy": "Retain",
"Properties": Object {
"CodeSha256": "JATk1Y3OqG7tx8Vw+NlNrcgBqJZWAj6nJHJV5y4MarE=",
"CodeSha256": Any<String>,
"FunctionName": Object {
"Ref": "Hello2LambdaFunction",
},
Expand Down
6 changes: 3 additions & 3 deletions e2e/__snapshots__/minimal.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13963,7 +13963,7 @@ Object {
"Name": "sls-minimal-example-dev-ValidateIsinLambdaFunctionQualifiedArn",
},
"Value": Object {
"Ref": "ValidateIsinLambdaVersion6F1yFivwUErVb4oeRL4yIAVELyFwfsjK7XsJuVKpg",
"Ref": Any<String>,
},
},
}
Expand Down Expand Up @@ -14264,10 +14264,10 @@ Object {
},
"Type": "AWS::Lambda::Permission",
},
"ValidateIsinLambdaVersion6F1yFivwUErVb4oeRL4yIAVELyFwfsjK7XsJuVKpg": Object {
"ValidateIsinLambdaVersionB7CGimGwkzX4VvjfOLhBKNzyM0E3cvLweXKHVVWYpgg": Object {
"DeletionPolicy": "Retain",
"Properties": Object {
"CodeSha256": "e3rFVkcGNQQ/f5FlNP1UrgUuW453BrlyaenA0ZTb2Yk=",
"CodeSha256": Any<String>,
"FunctionName": Object {
"Ref": "ValidateIsinLambdaFunction",
},
Expand Down
10 changes: 5 additions & 5 deletions e2e/complete.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs';
import cloudformation from './complete/.serverless/cloudformation-template-update-stack.json';
import cloudformation from './.test-artifacts/complete/.serverless/cloudformation-template-update-stack.json';

test('complete', () => {
const indexContents = fs.readFileSync('e2e/complete/.serverless/src/index.js').toString();
const indexContents = fs.readFileSync('e2e/.test-artifacts/complete/.serverless/src/index.js').toString();
expect(indexContents).toMatchSnapshot();

const nodeModules = fs.readdirSync('e2e/complete/.serverless/node_modules').toString();
const nodeModules = fs.readdirSync('e2e/.test-artifacts/complete/.serverless/node_modules').toString();
expect(nodeModules).toEqual(expect.stringContaining('isin-validator'));

expect(cloudformation.AWSTemplateFormatVersion).toMatchSnapshot();
Expand All @@ -22,8 +22,8 @@ test('complete', () => {
s.startsWith('ApiGatewayDeployment')
) as keyof typeof cloudformation.Resources;

const validateIsinLambdaVersionPropertyKey = cloudformation.Outputs
.ValidateIsinLambdaFunctionQualifiedArn.Value.Ref as keyof typeof cloudformation.Resources;
const validateIsinLambdaVersionPropertyKey = cloudformation.Outputs.ValidateIsinLambdaFunctionQualifiedArn.Value
.Ref as keyof typeof cloudformation.Resources;

const {
[apiGatewayDeploymentPropertyKey]: apiGatewayDeployment,
Expand Down
23 changes: 19 additions & 4 deletions e2e/individually.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import fs from 'fs';
import cloudformation from './individually/.serverless/cloudformation-template-update-stack.json';
import cloudformation from './.test-artifacts/individually/.serverless/cloudformation-template-update-stack.json';

test('individually', () => {
const hello1indexContents = fs.readFileSync('e2e/individually/.serverless/hello1.js').toString();
const hello2indexContents = fs.readFileSync('e2e/individually/.serverless/hello2.js').toString();
const hello1indexContents = fs.readFileSync('e2e/.test-artifacts/individually/.serverless/hello1.js').toString();
const hello2indexContents = fs.readFileSync('e2e/.test-artifacts/individually/.serverless/hello2.js').toString();
expect(hello1indexContents).toMatchSnapshot();
expect(hello2indexContents).toMatchSnapshot();

expect(cloudformation.AWSTemplateFormatVersion).toMatchSnapshot();
expect(cloudformation.Description).toMatchSnapshot;
expect(cloudformation.Outputs).toMatchSnapshot();
expect(cloudformation.Outputs).toMatchSnapshot({
Hello1LambdaFunctionQualifiedArn: {
Value: { Ref: expect.any(String) },
},
Hello2LambdaFunctionQualifiedArn: {
Value: { Ref: expect.any(String) },
},
});
expect(cloudformation.Outputs.Hello1LambdaFunctionQualifiedArn.Value.Ref).toMatch(/^Hello1LambdaVersion/);
expect(cloudformation.Outputs.Hello2LambdaFunctionQualifiedArn.Value.Ref).toMatch(/^Hello2LambdaVersion/);

const apiGatewayDeploymentPropertyKey = Object.keys(cloudformation.Resources).find((s) =>
s.startsWith('ApiGatewayDeployment')
Expand All @@ -29,6 +38,12 @@ test('individually', () => {
Code: { S3Key: expect.stringContaining('hello2.zip') },
},
},
[Object.keys(deterministicResources).find((s) => s.startsWith('Hello1LambdaVersion')) as string]: {
Properties: { CodeSha256: expect.any(String) },
},
[Object.keys(deterministicResources).find((s) => s.startsWith('Hello2LambdaVersion')) as string]: {
Properties: { CodeSha256: expect.any(String) },
},
});

expect(apiGatewayDeployment).toMatchSnapshot();
Expand Down
14 changes: 11 additions & 3 deletions e2e/minimal.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import fs from 'fs';
import cloudformation from './minimal/.serverless/cloudformation-template-update-stack.json';
import cloudformation from './.test-artifacts/minimal/.serverless/cloudformation-template-update-stack.json';

test('minimal', () => {
const indexContents = fs.readFileSync('e2e/minimal/.serverless/index.js').toString();
const indexContents = fs.readFileSync('e2e/.test-artifacts/minimal/.serverless/index.js').toString();
expect(indexContents).toMatchSnapshot();

expect(cloudformation.AWSTemplateFormatVersion).toMatchSnapshot();
expect(cloudformation.Description).toMatchSnapshot;
expect(cloudformation.Outputs).toMatchSnapshot();
expect(cloudformation.Outputs).toMatchSnapshot({
ValidateIsinLambdaFunctionQualifiedArn: {
Value: { Ref: expect.any(String) },
},
});
expect(cloudformation.Outputs.ValidateIsinLambdaFunctionQualifiedArn.Value.Ref).toMatch(/^ValidateIsinLambdaVersion/);

const apiGatewayDeploymentPropertyKey = Object.keys(cloudformation.Resources).find((s) =>
s.startsWith('ApiGatewayDeployment')
Expand All @@ -22,6 +27,9 @@ test('minimal', () => {
Code: { S3Key: expect.stringContaining('minimal-example.zip') },
},
},
[Object.keys(deterministicResources).find((s) => s.startsWith('ValidateIsinLambdaVersion')) as string]: {
Properties: { CodeSha256: expect.any(String) },
},
});

expect(apiGatewayDeployment).toMatchSnapshot();
Expand Down
20 changes: 9 additions & 11 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ export const flatDep = (root: DependencyMap, rootDepsFilter: string[]): string[]
const getBaseDep = (path: string): string => /^@[^/]+\/[^/\n]+|^[^/\n]+/.exec(path)[0];

export const isESM = (buildOptions: Configuration): boolean => {
return (
buildOptions.format === 'esm' || (buildOptions.platform === 'neutral' && !buildOptions.format)
);
return buildOptions.format === 'esm' || (buildOptions.platform === 'neutral' && !buildOptions.format);
};

/**
Expand Down Expand Up @@ -177,14 +175,14 @@ export const providerRuntimeMatcher = Object.freeze({
});

export const buildServerlessV3LoggerFromLegacyLogger = (
legacyLogger: (text: string) => void,
legacyLogger: Serverless['cli'],
verbose?: boolean
): ServerlessPlugin.Logging['log'] => ({
error: legacyLogger,
warning: legacyLogger,
notice: legacyLogger,
info: legacyLogger,
debug: verbose ? legacyLogger : () => null,
verbose: legacyLogger,
success: legacyLogger,
error: legacyLogger.log.bind(legacyLogger),
warning: legacyLogger.log.bind(legacyLogger),
notice: legacyLogger.log.bind(legacyLogger),
info: legacyLogger.log.bind(legacyLogger),
debug: verbose ? legacyLogger.log.bind(legacyLogger) : () => null,
verbose: legacyLogger.log.bind(legacyLogger),
success: legacyLogger.log.bind(legacyLogger),
});
49 changes: 9 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,14 @@ import ServerlessPlugin from 'serverless/classes/Plugin';
import chokidar from 'chokidar';
import anymatch from 'anymatch';

import {
buildServerlessV3LoggerFromLegacyLogger,
extractFunctionEntries,
providerRuntimeMatcher,
} from './helper';
import { buildServerlessV3LoggerFromLegacyLogger, extractFunctionEntries, providerRuntimeMatcher } from './helper';
import { packExternalModules } from './pack-externals';
import { pack } from './pack';
import { preOffline } from './pre-offline';
import { preLocal } from './pre-local';
import { bundle } from './bundle';
import { BUILD_FOLDER, ONLY_PREFIX, SERVERLESS_FOLDER, WORK_FOLDER } from './constants';
import {
Configuration,
FileBuildResult,
FunctionBuildResult,
Plugins,
ReturnPluginsFn,
ConfigFn,
} from './types';
import { Configuration, FileBuildResult, FunctionBuildResult, Plugins, ReturnPluginsFn, ConfigFn } from './types';

function updateFile(op: string, src: string, dest: string) {
if (['add', 'change', 'addDir'].includes(op)) {
Expand Down Expand Up @@ -64,16 +53,10 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
preOffline: () => Promise<void>;
preLocal: () => void;

constructor(
serverless: Serverless,
options: Serverless.Options,
logging?: ServerlessPlugin.Logging
) {
constructor(serverless: Serverless, options: Serverless.Options, logging?: ServerlessPlugin.Logging) {
this.serverless = serverless;
this.options = options;
this.log =
logging?.log ||
buildServerlessV3LoggerFromLegacyLogger(this.serverless.cli.log, this.options.verbose);
this.log = logging?.log || buildServerlessV3LoggerFromLegacyLogger(this.serverless.cli, this.options.verbose);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore old versions use servicePath, new versions serviceDir. Types will use only one of them
this.serviceDirPath = this.serverless.config.serviceDir || this.serverless.config.servicePath;
Expand Down Expand Up @@ -269,11 +252,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
}

get functionEntries() {
return extractFunctionEntries(
this.serviceDirPath,
this.serverless.service.provider.name,
this.functions
);
return extractFunctionEntries(this.serviceDirPath, this.serverless.service.provider.name, this.functions);
}

async watch(): Promise<void> {
Expand Down Expand Up @@ -360,9 +339,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
filename
)
) {
const destFileName = path.resolve(
path.join(this.buildDirPath, `${ONLY_PREFIX}${functionAlias}`, filename)
);
const destFileName = path.resolve(path.join(this.buildDirPath, `${ONLY_PREFIX}${functionAlias}`, filename));
updateFile(op, path.resolve(filename), destFileName);
return;
}
Expand Down Expand Up @@ -390,9 +367,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
}
const files = await globby(fn.package.patterns);
for (const filename of files) {
const destFileName = path.resolve(
path.join(this.buildDirPath, `${ONLY_PREFIX}${functionAlias}`, filename)
);
const destFileName = path.resolve(path.join(this.buildDirPath, `${ONLY_PREFIX}${functionAlias}`, filename));
updateFile('add', path.resolve(filename), destFileName);
}
}
Expand All @@ -405,10 +380,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
async moveArtifacts(): Promise<void> {
const { service } = this.serverless;

await fs.copy(
path.join(this.workDirPath, SERVERLESS_FOLDER),
path.join(this.serviceDirPath, SERVERLESS_FOLDER)
);
await fs.copy(path.join(this.workDirPath, SERVERLESS_FOLDER), path.join(this.serviceDirPath, SERVERLESS_FOLDER));

if (service.package.individually || this.options.function) {
Object.values(this.functions).forEach((func) => {
Expand All @@ -417,10 +389,7 @@ class EsbuildServerlessPlugin implements ServerlessPlugin {
return;
}

service.package.artifact = path.join(
SERVERLESS_FOLDER,
path.basename(service.package.artifact)
);
service.package.artifact = path.join(SERVERLESS_FOLDER, path.basename(service.package.artifact));
}

async cleanup(): Promise<void> {
Expand Down
Loading

0 comments on commit d976e9f

Please sign in to comment.