Skip to content

Commit

Permalink
Added AWSError error messages in output (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
akazakou committed Jul 20, 2022
1 parent 467acd6 commit de98746
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 25 deletions.
18 changes: 18 additions & 0 deletions __tests__/src/index/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const mocks: Record<string, jest.Mock> = {
actionsCoreInfo: jest.fn().mockName('Mock: "@actions/core".info()'),
actionsCoreGetInput: jest.fn().mockName('Mock: "@actions/core".getInput()'),
actionsCoreSetFailed: jest.fn().mockName('Mock: "@actions/core".setFailed()'),
actionsCoreError: jest.fn().mockName('Mock: "@actions/core".error()'),
startBuild: jest.fn().mockName('Mock: "src/codebuildjob".CodeBuildJob.startBuild()'),
cancelBuild: jest.fn().mockName('Mock: "src/codebuildjob".CodeBuildJob.cancelBuild()'),
};
Expand All @@ -15,6 +16,7 @@ jest.mock('@actions/core', () => ({
info: mocks.actionsCoreInfo,
getInput: mocks.actionsCoreGetInput,
setFailed: mocks.actionsCoreSetFailed,
error: mocks.actionsCoreError,
}));

jest.mock('../../../src/codebuildjob', () => ({
Expand All @@ -35,6 +37,7 @@ describe('CodeBuildJob class functionality', () => {

afterEach(() => {
Object.values(mocks).forEach(mock => mock.mockReset());
CodeBuildJobMock.mockReset();
});

it('should trigger job successfully', async () => {
Expand Down Expand Up @@ -71,4 +74,19 @@ describe('CodeBuildJob class functionality', () => {
process.emit('SIGINT');
expect(cancelBuild).toBeCalled();
});

it('should try to cancel job on SIGINT signal with exception', async () => {
const { startBuild, actionsCoreGetInput, cancelBuild, actionsCoreError } = mocks;
startBuild.mockReturnValue({ catch: jest.fn() });
actionsCoreGetInput.mockReturnValue('test');
const error = new Error('test error');
cancelBuild.mockImplementation(() => {throw error;});
jest.spyOn(process, 'exit').mockImplementation(jest.fn().mockName('Mock process.exit()') as never);

jest.requireMock('../../../src/index');
process.emit('SIGINT');

expect(cancelBuild).toBeCalled();
expect(actionsCoreError).toHaveBeenLastCalledWith(error);
});
});
23 changes: 23 additions & 0 deletions __tests__/src/logger/CloudWatchLogger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { AWSError } from 'aws-sdk';

const mocks = {
getLogEvents: jest.fn().mockName('Mock: "aws-sdk".CloudWatchLogs.prototype.getLogEvents()'),
actionsCoreInfo: jest.fn().mockName('Mock: "@actions/core".info()'),
actionsCoreError: jest.fn().mockName('Mock: "@actions/core".error()'),
}

jest.mock("aws-sdk", () => ({
Expand All @@ -11,13 +14,15 @@ jest.mock("aws-sdk", () => ({

jest.mock("@actions/core", () => ({
info: mocks.actionsCoreInfo,
error: mocks.actionsCoreError,
}));

import { CloudWatchLogger } from '../../../src/logger';


describe('CloudWatchLogs Logger getEvents() method', () => {
const createAWSResponse = (resolves: unknown) => ({ promise: () => Promise.resolve(resolves) });
const createAWSReject = (reject: unknown) => ({ promise: () => Promise.reject(reject) });

beforeAll(() => {
jest.useFakeTimers();
Expand Down Expand Up @@ -60,6 +65,24 @@ describe('CloudWatchLogs Logger getEvents() method', () => {
expect(getLogEvents).toBeCalledTimes(6);
expect(actionsCoreInfo).toBeCalledTimes(8);
});

it('should cancel logs listening if have no access', async () => {
const { getLogEvents, actionsCoreInfo, actionsCoreError } = mocks;
const error = {
message: 'test error',
code: 'AccessDeniedException',
} as AWSError;

getLogEvents.mockReturnValue(createAWSReject(error));
const logger = new CloudWatchLogger({ logGroupName: 'log_group_name', logStreamName: 'log_stream_name' });
const stopListenSpy = jest.spyOn(logger, 'stopListen');

await expect(logger['getEvents']()).resolves.toBeUndefined();
expect(getLogEvents).toBeCalledTimes(1);
expect(actionsCoreInfo).not.toBeCalled();
expect(actionsCoreError).toBeCalledTimes(1);
expect(stopListenSpy).toBeCalledTimes(1);
});
});

describe('CloudWatchLogs Logger Timers', () => {
Expand Down

0 comments on commit de98746

Please sign in to comment.