Skip to content

Commit

Permalink
fix: changelog updates and codeql remarks on tests
Browse files Browse the repository at this point in the history
Signed-off-by: zFernand0 <[email protected]>
  • Loading branch information
zFernand0 committed Jun 6, 2024
1 parent 5f6ee26 commit 8b86ccf
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 48 deletions.
6 changes: 6 additions & 0 deletions packages/imperative/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to the Imperative package will be documented in this file.
## Recent Changes

- Enhancement: Add client-side custom-event handling capabilities. [#2136](https://github.com/zowe/zowe-cli/pull/2136)
- Next-Breaking: Refactored the Imperative Event Emitter class. [#2136](https://github.com/zowe/zowe-cli/pull/2136)
- Removed the `ImperativeEventEmitter` class.
- Added an `EventProcessor` class to handle event listening and emitting.
- Added an `EventOperator` class to handle creation and deletion of `EventProcessors`.
- Added an `EventUtils` class to contain all common utility methods for the Client Event Handling capabilities.
- Added `IEmitter`, `IWatcher`, and `IEmitterAndWatcher` interfaces to expose what application developers should see.

## `8.0.0-next.202405151329`

Expand Down
2 changes: 1 addition & 1 deletion packages/imperative/src/config/src/ConfigUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ConfigUtils {
* Called once in `readProfilesFromDisk` and cached to minimize I/O operations.
* @internal
*/
public static readExtendersJsonFromDisk(): IExtendersJsonOpts {
public static readExtendersJson(): IExtendersJsonOpts {
const extenderJsonPath = pathJoin(ConfigUtils.getZoweDir(), "extenders.json");
if (!fsExistsSync(extenderJsonPath)) {
jsonfile.writeFileSync(extenderJsonPath, {
Expand Down
6 changes: 3 additions & 3 deletions packages/imperative/src/config/src/ProfileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ export class ProfileInfo {
});
}

this.mExtendersJson = ProfileInfo.readExtendersJsonFromDisk();
this.mExtendersJson = ConfigUtils.readExtendersJson();

Check warning on line 831 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L831

Added line #L831 was not covered by tests
this.loadAllSchemas();
}

Expand Down Expand Up @@ -1007,10 +1007,10 @@ export class ProfileInfo {
* Reads the `extenders.json` file from the CLI home directory.
* Called once in `readProfilesFromDisk` and cached to minimize I/O operations.
* @internal
* @deprecated Please use `ConfigUtils.readExtendersJsonFromDisk` instead
* @deprecated Please use `ConfigUtils.readExtendersJson` instead
*/
public static readExtendersJsonFromDisk(): IExtendersJsonOpts {
return ConfigUtils.readExtendersJsonFromDisk();
return ConfigUtils.readExtendersJson();

Check warning on line 1013 in packages/imperative/src/config/src/ProfileInfo.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/config/src/ProfileInfo.ts#L1013

Added line #L1013 was not covered by tests
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
*/

import { IImperativeEventJson as EventJson, EventUtils, IEventJson, ImperativeEventEmitter, ImperativeSharedEvents } from "../../..";
import { EventOperator, EventTypes, EventUtils, IEventJson, ZoweSharedEvents } from "../../..";
import { ITestEnvironment } from "../../../../__tests__/__src__/environment/doc/response/ITestEnvironment";
import { TestLogger } from "../../../../__tests__/src/TestLogger";
import * as TestUtil from "../../../../__tests__/src/TestUtil";
Expand All @@ -18,9 +18,8 @@ import * as fs from "fs";
import * as path from "path";

let TEST_ENVIRONMENT: ITestEnvironment;
const iee = ImperativeEventEmitter;
const iee_s = ImperativeSharedEvents;
let cwd = '';
const appName = "Zowe";

describe("Event Emitter", () => {
const mainModule = process.mainModule;
Expand All @@ -38,50 +37,47 @@ describe("Event Emitter", () => {
cwd = TEST_ENVIRONMENT.workingDir;
});

beforeEach(() => {
iee.initialize("zowe", { logger: testLogger });
});

afterEach(() => {
iee.teardown();
});

afterAll(() => {
process.mainModule = mainModule;
TestUtil.rimraf(cwd);
});

const doesEventFileExists = (eventType: string) => {
const eventDir = iee.instance.getEventDir(eventType);
const doesEventFileExists = (eventName: string) => {
const eventType = EventUtils.isSharedEvent(eventName) ? EventTypes.ZoweSharedEvents :
EventUtils.isUserEvent(eventName) ? EventTypes.ZoweUserEvents : EventTypes.SharedEvents;

const eventDir = EventUtils.getEventDir(eventType, appName);
if (!fs.existsSync(eventDir)) return false;
if (fs.existsSync(path.join(eventDir, eventType))) return true;
if (fs.existsSync(path.join(eventDir, appName, eventName))) return true;
return false;
};

describe("Shared Events", () => {
it("should create an event file upon first subscription if the file does not exist", () => {
const theEvent = iee_s.ON_CREDENTIAL_MANAGER_CHANGED;
const theEvent = ZoweSharedEvents.ON_CREDENTIAL_MANAGER_CHANGED;
const theProc = EventOperator.getZoweProcessor()

Check warning on line 58 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Missing semicolon

expect(doesEventFileExists(theEvent)).toBeFalsy();
expect((theProc as any).subscribedEvents.get(theEvent)).toBeFalsy();

const subSpy = jest.fn();
iee.instance.subscribe(theEvent, subSpy);
theProc.subscribeShared(theEvent, subSpy);

expect(subSpy).not.toHaveBeenCalled();
expect(doesEventFileExists(theEvent)).toBeTruthy();

Check failure on line 67 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / test (18.x, macos-14)

Event Emitter › Shared Events › should create an event file upon first subscription if the file does not exist

expect(received).toBeTruthy() Received: false at Object.<anonymous> (packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts:67:51)

Check failure on line 67 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / test (18.x, ubuntu-latest)

Event Emitter › Shared Events › should create an event file upon first subscription if the file does not exist

expect(received).toBeTruthy() Received: false at Object.<anonymous> (packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts:67:51)

Check failure on line 67 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / test (18.x, windows-latest)

Event Emitter › Shared Events › should create an event file upon first subscription if the file does not exist

expect(received).toBeTruthy() Received: false at Object.<anonymous> (packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts:67:51)

Check failure on line 67 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x, macos-14)

Event Emitter › Shared Events › should create an event file upon first subscription if the file does not exist

expect(received).toBeTruthy() Received: false at Object.<anonymous> (packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts:67:51)

Check failure on line 67 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x, ubuntu-latest)

Event Emitter › Shared Events › should create an event file upon first subscription if the file does not exist

expect(received).toBeTruthy() Received: false at Object.<anonymous> (packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts:67:51)

Check failure on line 67 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / test (20.x, windows-latest)

Event Emitter › Shared Events › should create an event file upon first subscription if the file does not exist

expect(received).toBeTruthy() Received: false at Object.<anonymous> (packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts:67:51)

expect(iee.instance.getEventContents(theEvent)).toBeFalsy();

iee.instance.emitEvent(theEvent);
theProc.emitEvent(theEvent);

(iee.instance as any).subscriptions.get(theEvent)[1][0](); // simulate FSWatcher called
(theProc as any).subscribedEvents.get(theEvent).subscriptions[0](); // simulate FSWatcher called

expect(doesEventFileExists(theEvent)).toBeTruthy();
const eventDetails: IEventJson = JSON.parse(iee.instance.getEventContents(theEvent));
const eventDetails: IEventJson = (theProc as any).subscribedEvents.get(theEvent).toJson();
expect(eventDetails.eventName).toEqual(theEvent);
expect(EventUtils.isUserEvent(eventDetails.eventName)).toBeTruthy();
expect(EventUtils.isSharedEvent(eventDetails.eventName)).toBeTruthy();

expect(subSpy).toHaveBeenCalled();

EventOperator.deleteProcessor(appName);
});
it("should trigger subscriptions for all instances watching for onCredentialManagerChanged", () => { });

Check warning on line 82 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Test has no assertions
it("should not affect subscriptions from another instance when unsubscribing from events", () => { });

Check warning on line 83 in packages/imperative/src/events/__tests__/__integration__/EventEmitter.integration.test.ts

View workflow job for this annotation

GitHub Actions / lint

Test has no assertions
Expand Down
9 changes: 0 additions & 9 deletions packages/imperative/src/events/src/EventConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
*/


// TO DO - flesh out these enums to include all expected user and shared events
export enum ZoweUserEvents {
ON_VAULT_CHANGED = "onVaultChanged"
}
Expand All @@ -19,14 +18,6 @@ export enum ZoweSharedEvents {
ON_CREDENTIAL_MANAGER_CHANGED = "onCredentialManagerChanged"
}

export enum SharedEvents {
CUSTOM_SHARED_EVENT = "customSharedEvent"
}

export enum UserEvents {
CUSTOM_USER_EVENT = "customUserEvent",
}

export enum EventTypes { ZoweUserEvents, ZoweSharedEvents, SharedEvents, UserEvents }

export type EventCallback = () => void | PromiseLike<void>;
Expand Down
16 changes: 12 additions & 4 deletions packages/imperative/src/events/src/EventOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import { EventProcessor } from "./EventProcessor";
import { IEmitter, IEmitterAndWatcher, IProcessorTypes, IWatcher } from "./doc/IEventInstanceTypes";
import { Logger } from "../../logger";
import { EventUtils } from "./EventUtils";

/**
* @internal Interface for Zowe-specific event processing, combining emitter and watcher functionalities.
Expand Down Expand Up @@ -46,13 +45,22 @@ export class EventOperator {
const newInstance = new EventProcessor(appName, type, logger);
this.instances.set(appName, newInstance);

Check warning on line 46 in packages/imperative/src/events/src/EventOperator.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventOperator.ts#L45-L46

Added lines #L45 - L46 were not covered by tests
}
return this.instances.get(appName);
const procInstance = this.instances.get(appName);

Check warning on line 48 in packages/imperative/src/events/src/EventOperator.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventOperator.ts#L48

Added line #L48 was not covered by tests
if (procInstance.processorType !== type) {
procInstance.processorType = IProcessorTypes.BOTH;

Check warning on line 50 in packages/imperative/src/events/src/EventOperator.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventOperator.ts#L50

Added line #L50 was not covered by tests
// throw new ImperativeError({msg: "Not allowed to get the other hald"})
}
return procInstance;

Check warning on line 53 in packages/imperative/src/events/src/EventOperator.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventOperator.ts#L53

Added line #L53 was not covered by tests
}

/**
* Retrieves a Zowe-specific event processor.
* Retrieves a Zowe-specific event processor. The purpose of this method is for internal
* Imperative APIs to get a properly initialized processor. This processor will be used
* when applications (like Zowe Explorer) call Imperative APIs that trigger events. For
* example, when the user updates credentials from Zowe Explorer, this processor will be
* used to emit an `OnVaultChanged` event.
*
* @internal
* @internal Not meant to be called by application developers
* @static
* @returns {IZoweProcessor} The Zowe event processor instance.
*/
Expand Down
11 changes: 6 additions & 5 deletions packages/imperative/src/events/src/EventUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class EventUtils {
* @returns {string[]} List of application names.
*/
public static getListOfApps(): string[] {
const extendersJson = ConfigUtils.readExtendersJsonFromDisk();
const extendersJson = ConfigUtils.readExtendersJson();

Check warning on line 37 in packages/imperative/src/events/src/EventUtils.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventUtils.ts#L36-L37

Added lines #L36 - L37 were not covered by tests
// We should not need to keep a reference to their sources
return ["Zowe", ...Object.keys(extendersJson.profileTypes)];

Check warning on line 39 in packages/imperative/src/events/src/EventUtils.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventUtils.ts#L39

Added line #L39 was not covered by tests

Expand Down Expand Up @@ -147,11 +147,12 @@ export class EventUtils {
* @return {IEventDisposable} An interface for managing the subscription.
*/
public static createSubscription(eeInst: EventProcessor, eventName: string, eventType: EventTypes): IEventDisposable {
const dir = this.getEventDir(eventType, eeInst.appName);
this.ensureEventsDirExists(join(ConfigUtils.getZoweDir(), '.events'));
this.ensureEventsDirExists(join(ConfigUtils.getZoweDir(), dir));
const dir = EventUtils.getEventDir(eventType, eeInst.appName);
const zoweDir = ConfigUtils.getZoweDir();
this.ensureEventsDirExists(join(zoweDir, '.events'));
this.ensureEventsDirExists(join(zoweDir, dir));

Check warning on line 153 in packages/imperative/src/events/src/EventUtils.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventUtils.ts#L149-L153

Added lines #L149 - L153 were not covered by tests

const filePath = join(ConfigUtils.getZoweDir(), dir, eventName);
const filePath = join(zoweDir, dir, eventName);
this.ensureFileExists(filePath);

Check warning on line 156 in packages/imperative/src/events/src/EventUtils.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventUtils.ts#L155-L156

Added lines #L155 - L156 were not covered by tests

const newEvent = new Event({

Check warning on line 158 in packages/imperative/src/events/src/EventUtils.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/events/src/EventUtils.ts#L158

Added line #L158 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { UpdateImpConfig } from "../../../UpdateImpConfig";
import { CredentialManagerOverride, ICredentialManagerNameMap } from "../../../../../security";
import { IProfileTypeConfiguration } from "../../../../../profiles";
import * as semver from "semver";
import { ProfileInfo } from "../../../../../config";
import { ConfigUtils, ProfileInfo } from "../../../../../config";
import { IExtendersJsonOpts } from "../../../../../config/src/doc/IExtenderOpts";

// Helper function to update extenders.json object during plugin install.
Expand Down Expand Up @@ -188,7 +188,7 @@ export async function install(packageLocation: string, registry: string, install
// Only update global schema if we were able to load it from disk
if (loadedSchema != null) {
const existingTypes = loadedSchema.map((obj) => obj.type);
const extendersJson = ProfileInfo.readExtendersJsonFromDisk();
const extendersJson = ConfigUtils.readExtendersJson();

Check warning on line 191 in packages/imperative/src/imperative/src/plugins/utilities/npm-interface/install.ts

View check run for this annotation

Codecov / codecov/patch

packages/imperative/src/imperative/src/plugins/utilities/npm-interface/install.ts#L191

Added line #L191 was not covered by tests

// Determine new profile types to add to schema
let shouldUpdate = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ImperativeError } from "../../../../../error";
import { ExecUtils, TextUtils } from "../../../../../utilities";
import { StdioOptions } from "child_process";
import { findNpmOnPath } from "../NpmFunctions";
import { ConfigSchema, ProfileInfo } from "../../../../../config";
import { ConfigSchema, ConfigUtils, ProfileInfo } from "../../../../../config";
import { IProfileTypeConfiguration } from "../../../../../profiles";

const npmCmd = findNpmOnPath();
Expand All @@ -30,7 +30,7 @@ const npmCmd = findNpmOnPath();
* @returns A list of types to remove from the schema
*/
export const updateAndGetRemovedTypes = (npmPackage: string): string[] => {
const extendersJson = ProfileInfo.readExtendersJsonFromDisk();
const extendersJson = ConfigUtils.readExtendersJson();
const pluginTypes = Object.keys(extendersJson.profileTypes)
.filter((type) => extendersJson.profileTypes[type].from.includes(npmPackage));
const typesToRemove: string[] = [];
Expand Down

0 comments on commit 8b86ccf

Please sign in to comment.