Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise incorrect post event data #10954

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions packages/amplify-cli/src/__tests__/execution-manager.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import * as fs from 'fs-extra';
import path from 'path';
import {
AmplifyEvent,
AmplifyEventArgs,
AmplifyPostCodegenModelsEventData,
AmplifyPostInitEventData,
AmplifyPostPullEventData,
AmplifyPostPushEventData,
AmplifyPreCodegenModelsEventData,
AmplifyPreInitEventData,
AmplifyPrePullEventData,
AmplifyPrePushEventData,
} from '../domain/amplify-event';
import { Context } from '../domain/context';
import { Input } from '../domain/input';
import { PluginInfo } from '../domain/plugin-info';
import { PluginManifest } from '../domain/plugin-manifest';
import { PluginPlatform } from '../domain/plugin-platform';
import { executeCommand } from '../execution-manager';

const handleAmplifyEventMock = jest.fn();
jest.mock('../../__mocks__/faked-plugin', () => ({
executeAmplifyCommand: jest.fn(),
handleAmplifyEvent: handleAmplifyEventMock,
}));

describe('execution manager', () => {
const mockFs = (fs as jest.Mocked<typeof fs>);
const mockContext = jest.createMockFromModule<Context>('../domain/context');

mockContext.input = new Input([
'/Users/userName/.nvm/versions/node/v8.11.4/bin/node',
'/Users/userName/.nvm/versions/node/v8.11.4/bin/amplify',
'push',
]);
mockContext.input.plugin = 'core';
mockContext.pluginPlatform = new PluginPlatform();

mockContext.pluginPlatform.plugins.core = [
new PluginInfo('@aws-amplify/cli-internal',
'latestVersion',
path.join(__dirname, '../../__mocks__/faked-plugin.js'),
new PluginManifest('core', 'core', undefined, undefined, ['init', 'push', 'pull', 'models'])),
];

const eventPluginManifest = new PluginManifest('test-event-plugin', 'util');
eventPluginManifest.eventHandlers = [
AmplifyEvent.PrePush,
AmplifyEvent.PreInit,
AmplifyEvent.PrePull,
AmplifyEvent.PreCodegenModels,
AmplifyEvent.PostPush,
AmplifyEvent.PostInit,
AmplifyEvent.PostPull,
AmplifyEvent.PostCodegenModels,
];
mockContext.pluginPlatform.plugins.testEvent = [
new PluginInfo(
'',
'1.0.0',
path.join(__dirname, '../../__mocks__/faked-plugin.js'),
eventPluginManifest,
),
];
mockContext.usageData = {
init: jest.fn(),
setIsHeadless: jest.fn(),
emitError: jest.fn(),
emitAbort: jest.fn(),
emitSuccess: jest.fn(),
startCodePathTimer: jest.fn(),
stopCodePathTimer: jest.fn(),
pushHeadlessFlow: jest.fn(),
pushInteractiveFlow: jest.fn(),
getFlowReport: jest.fn(),
assignProjectIdentifier: jest.fn(),
getUsageDataPayload: jest.fn(),
calculatePushNormalizationFactor: jest.fn(),
};

beforeEach(() => {
jest.clearAllMocks();
});

it.each([
['init', new AmplifyEventArgs(AmplifyEvent.PreInit, new AmplifyPreInitEventData())],
['push', new AmplifyEventArgs(AmplifyEvent.PrePush, new AmplifyPrePushEventData())],
['pull', new AmplifyEventArgs(AmplifyEvent.PrePull, new AmplifyPrePullEventData())],
['models', new AmplifyEventArgs(AmplifyEvent.PreCodegenModels, new AmplifyPreCodegenModelsEventData())],
])('executeCommand raise pre %s event', async (command, args) => {
mockFs.existsSync.mockReturnValue(true);
mockContext.input.command = command;
await executeCommand(mockContext);
expect(handleAmplifyEventMock)
.toBeCalledWith(mockContext, args);
});

it.each([
['init', new AmplifyEventArgs(AmplifyEvent.PostInit, new AmplifyPostInitEventData())],
['push', new AmplifyEventArgs(AmplifyEvent.PostPush, new AmplifyPostPushEventData())],
['pull', new AmplifyEventArgs(AmplifyEvent.PostPull, new AmplifyPostPullEventData())],
['models', new AmplifyEventArgs(AmplifyEvent.PostCodegenModels, new AmplifyPostCodegenModelsEventData())],
])('executeCommand raise post %s event', async (command, args) => {
mockFs.existsSync.mockReturnValue(true);
mockContext.input.command = command;
await executeCommand(mockContext);
expect(handleAmplifyEventMock)
.toBeCalledWith(mockContext, args);
});
});
4 changes: 2 additions & 2 deletions packages/amplify-cli/src/execution-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ const raisePostEvent = async (context: Context): Promise<void> => {
};

const raisePostInitEvent = async (context: Context): Promise<void> => {
await raiseEvent(context, new AmplifyEventArgs(AmplifyEvent.PostInit, new AmplifyPostPushEventData()));
await raiseEvent(context, new AmplifyEventArgs(AmplifyEvent.PostInit, new AmplifyPostInitEventData()));
};

const raisePostPushEvent = async (context: Context): Promise<void> => {
await raiseEvent(context, new AmplifyEventArgs(AmplifyEvent.PostPush, new AmplifyPostInitEventData()));
await raiseEvent(context, new AmplifyEventArgs(AmplifyEvent.PostPush, new AmplifyPostPushEventData()));
};

const raisePostPullEvent = async (context: Context): Promise<void> => {
Expand Down