Skip to content

Commit

Permalink
tests: addProfileTypeToSchema scenarios
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Jan 4, 2024
1 parent 2202b1a commit fba021a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { ImperativeConfig } from "../../utilities/src/ImperativeConfig";
import { ImperativeError } from "../../error";
import { IProfInfoUpdatePropOpts } from "../src/doc/IProfInfoUpdatePropOpts";
import { ConfigProfiles } from "../src/api";
import { IExtendersJsonOpts } from "../src/doc/IExtenderOpts";

const testAppNm = "ProfInfoApp";
const testEnvPrefix = testAppNm.toUpperCase();
Expand Down Expand Up @@ -1436,16 +1437,24 @@ describe("TeamConfig ProfileInfo tests", () => {
// case 1: no sources specified, returns profile types without filtering
it("returns the default set of profile types", async () => {
const profInfo = createNewProfInfo(teamProjDir);
jest.spyOn(jsonfile, "writeFileSync").mockImplementation();
await profInfo.readProfilesFromDisk({ homeDir: teamHomeProjDir });
const expectedTypes = [...profileTypes].concat(["ssh"]).sort();
expect(profInfo.getProfileTypes()).toEqual(expectedTypes);
});
// TODO: case 2: filtering by source
it("filters by source", async () => {
const profInfo = createNewProfInfo(teamProjDir);
jest.spyOn(jsonfile, "writeFileSync").mockImplementation();
await profInfo.readProfilesFromDisk({ homeDir: teamHomeProjDir });
profInfo.addProfileTypeToSchema("some-type", { sourceApp: "Zowe Client App", schema: {} as any });
expect(profInfo.getProfileTypes(["Zowe Client App"])).toEqual(["some-type"]);
});
});

describe("getSchemaForType", () => {
// case 1: returns the schema for a registered profile type
it("returns the schema for 'dummy' type", async () => {
it("returns the schema for a registered type", async () => {
const profInfo = createNewProfInfo(teamProjDir);
await profInfo.readProfilesFromDisk({ homeDir: teamHomeProjDir });
expect(profInfo.getSchemaForType("dummy")).toBeDefined();
Expand All @@ -1458,6 +1467,99 @@ describe("TeamConfig ProfileInfo tests", () => {
expect(profInfo.getSchemaForType("type-that-doesnt-exist")).toBeUndefined();
});
});
// TODO: getProfileTypes, buildSchema, addProfileTypeToSchema

describe("addProfileTypeToSchema", () => {
const expectAddToSchemaTester = async (testCase: { schema: any; previousVersion?: string; version?: string }, expected: {
extendersJson: IExtendersJsonOpts,
res: {
success: boolean;
info?: string;
},
version?: string,
}) => {
const profInfo = createNewProfInfo(teamProjDir);
await profInfo.readProfilesFromDisk({ homeDir: teamHomeProjDir });
if (testCase.previousVersion) {
(profInfo as any).mExtendersJson = {
profileTypes: {
"some-type": {
from: ["Zowe Client App"],
version: testCase.previousVersion === "none" ? undefined : testCase.previousVersion
}
}
};
} else {
(profInfo as any).mExtendersJson = {
profileTypes: {}
};
}
const updateSchemaAtLayerMock = jest.spyOn((ProfileInfo as any).prototype, "updateSchemaAtLayer").mockImplementation();
const writeExtendersJsonMock = jest.spyOn((ProfileInfo as any).prototype, "writeExtendersJson").mockImplementation();
const res = profInfo.addProfileTypeToSchema("some-type", { ...testCase, sourceApp: "Zowe Client App" });
if (expected.res.success) {
expect(updateSchemaAtLayerMock).toHaveBeenCalled();
expect(writeExtendersJsonMock).toHaveBeenCalled();
} else {
expect(updateSchemaAtLayerMock).not.toHaveBeenCalled();
expect(writeExtendersJsonMock).not.toHaveBeenCalled();
}
expect((profInfo as any).mExtendersJson).toEqual(expected.extendersJson);
expect(res.success).toBe(expected.res.success);
if (expected.res.info) {
expect(res.info).toBe(expected.res.info);
}
};
// case 1: Profile type did not exist
it("adds a new profile type to the schema", async () => {
expectAddToSchemaTester(
{ schema: { title: "Mock Schema" } as any },
{
extendersJson: { profileTypes: { "some-type": { from: ["Zowe Client App"] } } },
res: {
success: true
}
}
);
});

it("only updates a profile type in the schema if the version is newer", async () => {
expectAddToSchemaTester(
{ previousVersion: "1.0.0", schema: { title: "Mock Schema" } as any, version: "2.0.0" },
{
extendersJson: { profileTypes: { "some-type": { from: ["Zowe Client App"], version: "2.0.0" } } },
res: {
success: true
}
}
);
});

it("does not update a profile type in the schema if the version is older", async () => {
expectAddToSchemaTester(
{ previousVersion: "2.0.0", schema: { title: "Mock Schema" } as any, version: "1.0.0" },
{
extendersJson: { profileTypes: { "some-type": { from: ["Zowe Client App"], version: "2.0.0" } } },
res: {
success: false
}
}
);
});

it("updates a profile type in the schema - version provided, no previous schema version", async () => {
expectAddToSchemaTester(
{ previousVersion: "none", schema: { title: "Mock Schema" } as any, version: "1.0.0" },
{
extendersJson: { profileTypes: { "some-type": { from: ["Zowe Client App"], version: "1.0.0" } } },
res: {
success: true
}
}
);
});
});
describe("buildSchema", () => {
// TODO
});
// end schema management tests
});
22 changes: 10 additions & 12 deletions packages/imperative/src/config/src/ProfileInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,7 @@ export class ProfileInfo {
jsonfile.writeFileSync(extenderJsonPath, {
profileTypes: {}
}, { spaces: 4 });
this.mExtendersJson = { profileTypes: {} };
} else {
this.mExtendersJson = jsonfile.readFileSync(extenderJsonPath);
}
Expand Down Expand Up @@ -1478,22 +1479,19 @@ export class ProfileInfo {
continue;

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1479 was not covered by tests
}

if (filteredBySource && type in this.mExtendersJson.profileTypes) {
// Only consider types contributed by at least one of these sources
if (sources.some((val) => this.mExtendersJson.profileTypes[type].from.includes(val))) {
profileTypes.add(type);
}
} else {
profileTypes.add(type);
}
profileTypes.add(type);
}
}

// Include all profile types from extenders.json if we are not filtering by source
if (!filteredBySource) {
for (const type of Object.keys(this.mExtendersJson.profileTypes)) {
profileTypes.add(type);
}
if (filteredBySource) {
return [...profileTypes].filter((t) => {
if (!(t in this.mExtendersJson.profileTypes)) {
return false;
}

return this.mExtendersJson.profileTypes[t].from.some((src) => sources.includes(src));
}).sort((a, b) => a.localeCompare(b));

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L1494 was not covered by tests
}

return [...profileTypes].sort((a, b) => a.localeCompare(b));
Expand Down

0 comments on commit fba021a

Please sign in to comment.