Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

[HOUSEKEEPING] some fixes on service get service name command #536

Merged
merged 3 commits into from
Apr 9, 2020
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
36 changes: 24 additions & 12 deletions src/commands/service/get-display-name.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,41 @@ describe("get display name", () => {
const defaultBedrockFileObject = createTestBedrockYaml(
false
) as BedrockFile;
jest.spyOn(fs, "existsSync").mockReturnValue(true);
jest.spyOn(bedrockYaml, "read").mockReturnValue(defaultBedrockFileObject);
jest.spyOn(process, "cwd").mockReturnValue("bedrock.yaml/");
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
jest
.spyOn(bedrockYaml, "read")
.mockReturnValueOnce(defaultBedrockFileObject);
jest.spyOn(process, "cwd").mockReturnValueOnce("bedrock.yaml/");
const consoleSpy = jest.spyOn(console, "log");
execute({ path: "./packages/service1" }, exitFn);
await execute({ path: "./packages/service1" }, exitFn);
expect(consoleSpy).toHaveBeenCalledWith("service1");
expect(exitFn).toBeCalledTimes(1);
expect(exitFn).toBeCalledWith(0);
});
it("negative test", async () => {
it("negative test: path missing", async () => {
const exitFn = jest.fn();
execute({ path: "" }, exitFn);
await execute({ path: "" }, exitFn);
expect(exitFn).toBeCalledTimes(1);
execute({ path: undefined }, exitFn);
expect(exitFn).toBeCalledWith(1);
});
it("negative test: path as undefined", async () => {
const exitFn = jest.fn();
await execute({ path: undefined }, exitFn);
expect(exitFn).toBeCalledTimes(1);
expect(exitFn).toBeCalledWith(1);
});
it("negative test: service not found", async () => {
const exitFn = jest.fn();
const defaultBedrockFileObject = createTestBedrockYaml(
false
) as BedrockFile;
jest.spyOn(fs, "existsSync").mockReturnValue(true);
jest.spyOn(bedrockYaml, "read").mockReturnValue(defaultBedrockFileObject);
jest.spyOn(process, "cwd").mockReturnValue("bedrock.yaml/");
execute({ path: "./packages/service" }, exitFn); // should not exist
expect(exitFn).toBeCalledTimes(3);
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
jest
.spyOn(bedrockYaml, "read")
.mockReturnValueOnce(defaultBedrockFileObject);
jest.spyOn(process, "cwd").mockReturnValueOnce("bedrock.yaml/");
await execute({ path: "./packages/service" }, exitFn); // should not exist
expect(exitFn).toBeCalledTimes(1);
expect(exitFn).toBeCalledWith(1);
});
});
32 changes: 15 additions & 17 deletions src/commands/service/get-display-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,33 @@ export const execute = async (
if (!opts.path) {
throw buildError(
errorStatusCode.VALIDATION_ERR,
"service-get-display-name-path-missing-param-err"
"service-get-display-name-cmd-path-missing-param-err"
);
}

// bedrockFile will always be return
// it cannot be null or undefined.
const bedrockFile = readBedrockYaml(process.cwd());
if (!bedrockFile) {
throw buildError(
errorStatusCode.FILE_IO_ERR,
"service-get-display-name-bedrock-yaml-missing-err"
);
}

const serviceIndex = Object.keys(bedrockFile.services).find(
(index) => opts.path === bedrockFile.services[+index].path
// bedrockFile.services is an array
const serviceConfig = bedrockFile.services.find(
(config) => opts.path === config.path
);

if (serviceIndex) {
console.log(bedrockFile.services[+serviceIndex].displayName);
if (serviceConfig) {
console.log(serviceConfig.displayName);
await exitFn(0);
} else {
throw buildError(errorStatusCode.ENV_SETTING_ERR, {
errorKey: "service-get-display-name-cmd-service-name-not-found-err",
values: [opts.path],
});
}

throw buildError(errorStatusCode.ENV_SETTING_ERR, {
errorKey: "service-get-display-name-err",
values: [opts.path],
});
} catch (err) {
logError(
buildError(
errorStatusCode.VALIDATION_ERR,
"service-get-display-name-generic-err",
"service-get-display-name-cmd-failed",
err
)
);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@
"introspect-dashboard-cmd-launch-pre-req-err": "Requirements to launch dashboard were not met.",
"introspect-dashboard-cmd-launch-err": "Could not launch dashboard docker container.",

"service-get-display-name-path-missing-param-err": "Value for path parameter was missing. This is required for running get-display-command. Provide it.",
"service-get-display-name-bedrock-yaml-missing-err": "Could not find bedrock.yaml in current directory. Make sure to run this from a directory that contains bedrock.yaml.",
"service-get-display-name-err": "Could not find a service for path {0}. Make sure that the specified path is valid.",
"service-get-display-name-generic-err": "Error occurred while getting display name.",
"service-get-display-name-cmd-failed": "Service get display name command was not successfully executed.",
"service-get-display-name-cmd-path-missing-param-err": "Value for path parameter was missing. This is required for running get-display-command. Provide it.",
"service-get-display-name-cmd-service-name-not-found-err": "Could not find a service for path {0}. Make sure that the specified path is valid.",

"az-cli-login-err": "Could not login through azure cli.",
"az-cli-create-sp-err": "Could not create service principal with azure cli",

Expand Down