Skip to content

Commit

Permalink
nice units
Browse files Browse the repository at this point in the history
  • Loading branch information
bergjaak committed May 15, 2024
1 parent a162e59 commit 8434309
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
20 changes: 8 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
"version": "0.2.0",
"configurations": [
{
// Has convenient settings for attaching to a NodeJS process for debugging purposes
// that are NOT the default and otherwise every developers has to configure for
// themselves again and again.
"type": "node",
"request": "attach",
"name": "Attach to NodeJS",
// If we don't do this, every step-into into an async function call will go into
// NodeJS internals which are hard to step out of.
"skipFiles": [
"<node_internals>/**"
],
// Saves some button-pressing latency on attaching
"stopOnEntry": false
"request": "launch",
"args": ["jest", "--runTestsByPath", "/Users/bergjak/workplace/CDK/aws-cdk/packages/aws-cdk-lib/aws-lambda/test/code.test.ts"],
"name": "debug unit test",
"program": "/opt/homebrew/bin/yarn",
"cwd": "/Users/bergjak/workplace/CDK/aws-cdk/packages/aws-cdk-lib",
"console": "integratedTerminal",
"sourceMaps": true,
"skipFiles": [ "<node_internals>/**/*" ],
}
]
}
2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export abstract class Code {
: spawnSync(cmd, commandArguments, commandOptions);

if (proc.error) {
throw proc.error;
throw new Error(`Failed to execute custom command: ${proc.error}`);
}
if (proc.status !== 0) {
throw new Error(`${command.join(' ')} exited with status: ${proc.status}\n\nstdout: ${proc.stdout?.toString().trim()}\n\nstderr: ${proc.stderr?.toString().trim()}`);
Expand Down
73 changes: 73 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/test/code.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as child_process from 'child_process';
import * as path from 'path';
import { Match, Template } from '../../assertions';
import * as ecr from '../../aws-ecr';
Expand All @@ -15,6 +16,78 @@ describe('code', () => {
});
});

describe('lambda.Code.fromCustomCommand', () => {
let spawnSyncMock: jest.SpyInstance;
beforeEach(() => {
spawnSyncMock = jest.spyOn(child_process, 'spawnSync').mockReturnValue({
status: 0,
stderr: Buffer.from('stderr'),
stdout: Buffer.from('stdout'),
pid: 123,
output: ['stdout', 'stderr'],
signal: null,
});
});
afterEach(() => {
jest.restoreAllMocks();
});

test('fails if command is empty', () => {
// GIVEN
const command = [];

// THEN
expect(() => lambda.Code.fromCustomCommand('', command)).toThrow('command must contain at least one argument. For example, ["node", "buildFile.js"].');
});
test('properly splices arguments', () => {
// GIVEN
const command = 'node is a great command, wow'.split(' ');
lambda.Code.fromCustomCommand('', command);

// THEN
expect(spawnSyncMock).toHaveBeenCalledWith(
'node',
['is', 'a', 'great', 'command,', 'wow'],
);
});
test('properly splices arguments when commandOptions are included', () => {
// GIVEN
const command = 'node is a great command, wow'.split(' ');
const commandOptions = { cwd: '/tmp', env: { SOME_KEY: 'SOME_VALUE' } };
lambda.Code.fromCustomCommand('', command, commandOptions);

// THEN
expect(spawnSyncMock).toHaveBeenCalledWith(
'node',
['is', 'a', 'great', 'command,', 'wow'],
commandOptions,
);
});
test('properly splices arguments when commandOptions are included', () => {
// GIVEN
jest.restoreAllMocks(); // use the real spawnSync, which doesn't work in unit tests.
const command = ['whatever'];

// THEN
expect(() => lambda.Code.fromCustomCommand('', command)).toThrow(/Failed to execute custom command: .*/);
});
test('properly splices arguments when commandOptions are included', () => {
// GIVEN
const command = ['whatever'];
spawnSyncMock = jest.spyOn(child_process, 'spawnSync').mockReturnValue({
status: 1,
stderr: Buffer.from('stderr'),
stdout: Buffer.from('stdout'),
pid: 123,
output: ['stdout', 'stderr'],
signal: null,
});

// THEN
expect(() => lambda.Code.fromCustomCommand('', command)).toThrow('whatever exited with status: 1\n\nstdout: stdout\n\nstderr: stderr');
});
});

describe('lambda.Code.fromAsset', () => {
test('fails if path is empty', () => {
// GIVEN
Expand Down

0 comments on commit 8434309

Please sign in to comment.