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

Commit 8d8852d

Browse files
authored
[HOUSEKEEPING] some fixes on service get service name command (#536)
1 parent 7882da8 commit 8d8852d

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

src/commands/service/get-display-name.test.ts

+24-12
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,41 @@ describe("get display name", () => {
1919
const defaultBedrockFileObject = createTestBedrockYaml(
2020
false
2121
) as BedrockFile;
22-
jest.spyOn(fs, "existsSync").mockReturnValue(true);
23-
jest.spyOn(bedrockYaml, "read").mockReturnValue(defaultBedrockFileObject);
24-
jest.spyOn(process, "cwd").mockReturnValue("bedrock.yaml/");
22+
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
23+
jest
24+
.spyOn(bedrockYaml, "read")
25+
.mockReturnValueOnce(defaultBedrockFileObject);
26+
jest.spyOn(process, "cwd").mockReturnValueOnce("bedrock.yaml/");
2527
const consoleSpy = jest.spyOn(console, "log");
26-
execute({ path: "./packages/service1" }, exitFn);
28+
await execute({ path: "./packages/service1" }, exitFn);
2729
expect(consoleSpy).toHaveBeenCalledWith("service1");
2830
expect(exitFn).toBeCalledTimes(1);
2931
expect(exitFn).toBeCalledWith(0);
3032
});
31-
it("negative test", async () => {
33+
it("negative test: path missing", async () => {
3234
const exitFn = jest.fn();
33-
execute({ path: "" }, exitFn);
35+
await execute({ path: "" }, exitFn);
3436
expect(exitFn).toBeCalledTimes(1);
35-
execute({ path: undefined }, exitFn);
3637
expect(exitFn).toBeCalledWith(1);
38+
});
39+
it("negative test: path as undefined", async () => {
40+
const exitFn = jest.fn();
41+
await execute({ path: undefined }, exitFn);
42+
expect(exitFn).toBeCalledTimes(1);
43+
expect(exitFn).toBeCalledWith(1);
44+
});
45+
it("negative test: service not found", async () => {
46+
const exitFn = jest.fn();
3747
const defaultBedrockFileObject = createTestBedrockYaml(
3848
false
3949
) as BedrockFile;
40-
jest.spyOn(fs, "existsSync").mockReturnValue(true);
41-
jest.spyOn(bedrockYaml, "read").mockReturnValue(defaultBedrockFileObject);
42-
jest.spyOn(process, "cwd").mockReturnValue("bedrock.yaml/");
43-
execute({ path: "./packages/service" }, exitFn); // should not exist
44-
expect(exitFn).toBeCalledTimes(3);
50+
jest.spyOn(fs, "existsSync").mockReturnValueOnce(true);
51+
jest
52+
.spyOn(bedrockYaml, "read")
53+
.mockReturnValueOnce(defaultBedrockFileObject);
54+
jest.spyOn(process, "cwd").mockReturnValueOnce("bedrock.yaml/");
55+
await execute({ path: "./packages/service" }, exitFn); // should not exist
56+
expect(exitFn).toBeCalledTimes(1);
4557
expect(exitFn).toBeCalledWith(1);
4658
});
4759
});

src/commands/service/get-display-name.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,33 @@ export const execute = async (
2626
if (!opts.path) {
2727
throw buildError(
2828
errorStatusCode.VALIDATION_ERR,
29-
"service-get-display-name-path-missing-param-err"
29+
"service-get-display-name-cmd-path-missing-param-err"
3030
);
3131
}
32+
33+
// bedrockFile will always be return
34+
// it cannot be null or undefined.
3235
const bedrockFile = readBedrockYaml(process.cwd());
33-
if (!bedrockFile) {
34-
throw buildError(
35-
errorStatusCode.FILE_IO_ERR,
36-
"service-get-display-name-bedrock-yaml-missing-err"
37-
);
38-
}
3936

40-
const serviceIndex = Object.keys(bedrockFile.services).find(
41-
(index) => opts.path === bedrockFile.services[+index].path
37+
// bedrockFile.services is an array
38+
const serviceConfig = bedrockFile.services.find(
39+
(config) => opts.path === config.path
4240
);
4341

44-
if (serviceIndex) {
45-
console.log(bedrockFile.services[+serviceIndex].displayName);
42+
if (serviceConfig) {
43+
console.log(serviceConfig.displayName);
4644
await exitFn(0);
45+
} else {
46+
throw buildError(errorStatusCode.ENV_SETTING_ERR, {
47+
errorKey: "service-get-display-name-cmd-service-name-not-found-err",
48+
values: [opts.path],
49+
});
4750
}
48-
49-
throw buildError(errorStatusCode.ENV_SETTING_ERR, {
50-
errorKey: "service-get-display-name-err",
51-
values: [opts.path],
52-
});
5351
} catch (err) {
5452
logError(
5553
buildError(
5654
errorStatusCode.VALIDATION_ERR,
57-
"service-get-display-name-generic-err",
55+
"service-get-display-name-cmd-failed",
5856
err
5957
)
6058
);

src/lib/i18n.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@
159159
"introspect-dashboard-cmd-launch-pre-req-err": "Requirements to launch dashboard were not met.",
160160
"introspect-dashboard-cmd-launch-err": "Could not launch dashboard docker container.",
161161

162-
"service-get-display-name-path-missing-param-err": "Value for path parameter was missing. This is required for running get-display-command. Provide it.",
163-
"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.",
164-
"service-get-display-name-err": "Could not find a service for path {0}. Make sure that the specified path is valid.",
165-
"service-get-display-name-generic-err": "Error occurred while getting display name.",
162+
"service-get-display-name-cmd-failed": "Service get display name command was not successfully executed.",
163+
"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.",
164+
"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.",
165+
166166
"az-cli-login-err": "Could not login through azure cli.",
167167
"az-cli-create-sp-err": "Could not create service principal with azure cli",
168168

0 commit comments

Comments
 (0)