Skip to content

Commit

Permalink
feat: add multi-platform end-to-end tests (#130)
Browse files Browse the repository at this point in the history
* chore: add multi-platform end-to-end tests

* refactor: rename integ to integration
  • Loading branch information
misterjoshua authored Oct 25, 2021
1 parent 8d09367 commit 0833d60
Show file tree
Hide file tree
Showing 25 changed files with 1,148 additions and 5 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/pullRequest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,27 @@ jobs:
- name: Test
run: |
yarn test --ignore-cache
- uses: actions/upload-artifact@v2
with:
name: plugin-build
path: packages/plugins/plugin-build/bundles

integration-test:
name: Integration Test
needs: [build]
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Download all artifacts
uses: actions/download-artifact@v2
with:
name: plugin-build
path: packages/plugins/plugin-build/bundles
- name: Install packages
run: yarn install
- name: Run integration tests
run: yarn test:integration
75 changes: 75 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
85 changes: 85 additions & 0 deletions e2e/lambda-project.integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as fs from 'fs-extra';
import * as os from 'os';
import * as crypto from 'crypto';
import * as path from 'path';
import * as execa from 'execa';

test('bundling lambda-project to a zip', async () => {
const workDir = getTempDirName();
const bundleOutput = getTempDirName();

// Stage the test project far from the yarn.build project
fs.copySync(path.join(__dirname, 'lambda-project'), workDir, {
recursive: true,
errorOnExist: true,
});

// WHEN
yarnCmd(workDir, 'plugin', 'import', BUILD_PLUGIN_PATH);
yarnCmd(workDir, 'install');
yarnCmd(workDir, 'workspace', 'lambda', 'build');
yarnCmd(workDir, 'workspace', 'lambda', 'bundle', '--output-directory', bundleOutput);

// THEN
const zipPath = path.join(bundleOutput, 'bundle.zip');

expect(fs.existsSync(zipPath)).toEqual(true);
expect(fs.statSync(zipPath).isFile()).toEqual(true);
});

test('run lambda-project after bundling without compression', async () => {
const workDir = getTempDirName();
const bundleOutput = getTempDirName();

// Stage the test project far from the yarn.build project
fs.copySync(path.join(__dirname, 'lambda-project'), workDir, {
recursive: true,
errorOnExist: true,
});

// WHEN
yarnCmd(workDir, 'plugin', 'import', BUILD_PLUGIN_PATH);
yarnCmd(workDir, 'install');
yarnCmd(workDir, 'workspace', 'lambda', 'build');
yarnCmd(workDir, 'workspace', 'lambda', 'bundle', '--output-directory', bundleOutput, '--no-compress');

// THEN
expect(fs.existsSync(path.join(bundleOutput, 'package.json'))).toEqual(true);
expect(fs.existsSync(path.join(bundleOutput, '.pnp.cjs'))).toEqual(true);
expect(fs.existsSync(path.join(bundleOutput, '.yarn'))).toEqual(true);
expect(fs.readdirSync(path.join(bundleOutput, '.yarn', 'cache'))).toEqual([
".gitignore",
expect.stringMatching(/^uglify-js.*\.zip$/),
// Notably, the cache excludes the dev deps.
]);

// Now run the bundled code to see that it works!
// lambda-project's dependencies look like this: lambda -> lib -> uglify-js
// Calling the lambda's api handler tests the uglify-js transitive dependency
const execResult = execa.sync('node', ['--require', './.pnp.cjs', 'packages/lambda/dist/api.js'], {
cwd: bundleOutput,
});

const responsePayload = JSON.parse(execResult.stdout);

expect(responsePayload).toEqual({
statusCode: 200,
// The following shows that the lambda package called lib, and lib used
// uglify-js.
body: "\"function foobar(){} // MUGLIFIED\"",
});
});

function yarnCmd(workDir: string, ...args: string[]) {
execa.sync('yarn', args, {
cwd: workDir,
stdout: process.stdout,
stderr: process.stderr,
});
}

function getTempDirName() {
return path.join(os.tmpdir(), 'integ' + crypto.randomBytes(10).toString('hex'));
}

const BUILD_PLUGIN_PATH = path.join(__dirname, '..', 'packages', 'plugins', 'plugin-build', 'bundles', '@yarnpkg', 'plugin-build.js');
9 changes: 9 additions & 0 deletions e2e/lambda-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dist

.pnp.cjs
.yarn/*
!.yarn/patches
#!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
631 changes: 631 additions & 0 deletions e2e/lambda-project/.yarn/releases/yarn-berry.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions e2e/lambda-project/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodeLinker: pnp
yarnPath: .yarn/releases/yarn-berry.cjs
6 changes: 6 additions & 0 deletions e2e/lambda-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "lambda-project",
"workspaces": [
"packages/*"
]
}
16 changes: 16 additions & 0 deletions e2e/lambda-project/packages/lambda/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "lambda",
"version": "1.0.0",
"packageManager": "[email protected]",
"scripts": {
"build": "tsc --project tsconfig.dev.json"
},
"dependencies": {
"lib": "1.0.0"
},
"devDependencies": {
"@types/aws-lambda": "^8.10.84",
"@types/node": "14.17.6",
"typescript": "^4.4.4"
}
}
32 changes: 32 additions & 0 deletions e2e/lambda-project/packages/lambda/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable */
// @ts-ignore
import type * as lambda from 'aws-lambda';
// @ts-ignore
import {muglify} from 'lib';

export async function handler(): Promise<lambda.APIGatewayProxyResult> {
try {
const value = muglify('function foobar() { /* WILL BE STRIPPED OUT */ }');

return {
statusCode: 200,
body: JSON.stringify(value),
};
} catch (e) {
return {
statusCode: 500,
body: JSON.stringify(e),
};
}
}

// When run, this file executes the handler and outputs the result.
if (require.main === module) {
handler()
.then(res => {
console.log(JSON.stringify(res));
})
.catch(e => {
console.error(e);
});
}
29 changes: 29 additions & 0 deletions e2e/lambda-project/packages/lambda/tsconfig.dev.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"compilerOptions": {
"alwaysStrict": true,
"declaration": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"inlineSourceMap": true,
"inlineSources": true,
"lib": [
"es2019"
],
"module": "CommonJS",
"noEmitOnError": false,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"strict": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"stripInternal": true,
"target": "ES2019",
"outDir": "./dist",
"rootDir": "./src"
}
}
16 changes: 16 additions & 0 deletions e2e/lambda-project/packages/lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "lib",
"version": "1.0.0",
"packageManager": "[email protected]",
"main": "dist/index.js",
"scripts": {
"build": "tsc --project tsconfig.dev.json"
},
"devDependencies": {
"@types/uglify-js": "^3.13.1",
"typescript": "^4.4.4"
},
"dependencies": {
"uglify-js": "^3.14.2"
}
}
13 changes: 13 additions & 0 deletions e2e/lambda-project/packages/lib/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* eslint-disable */
// @ts-ignore
import uglify from 'uglify-js';

export function muglify(code: string): string {
const minifyOutput = uglify.minify(code);

if (minifyOutput.error) {
throw minifyOutput.error;
}

return minifyOutput.code + ' // MUGLIFIED';
}
Loading

0 comments on commit 0833d60

Please sign in to comment.