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

Commit 113ac6e

Browse files
authored
Removed duplicate call to check for bedrock file that enforces project commands order (#235)
* Closes #927 * Fixed test case * Incrporated feedback. Moved test cases to apporpriate file. * Incrporated feedback * Fixed test case
1 parent eb6c58d commit 113ac6e

File tree

11 files changed

+132
-216
lines changed

11 files changed

+132
-216
lines changed

src/commands/project/create-variable-group.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@ import { VariableGroup } from "azure-devops-node-api/interfaces/ReleaseInterface
22
import commander from "commander";
33
import path from "path";
44
import { echo } from "shelljs";
5-
import {
6-
Bedrock,
7-
bedrockFileInfo,
8-
Config,
9-
readYaml,
10-
write
11-
} from "../../config";
12-
import { projectInitDependencyErrorMessage } from "../../constants";
5+
import { Bedrock, Config, readYaml, write } from "../../config";
6+
import { fileInfo as bedrockFileInfo } from "../../lib/bedrockYaml";
137
import {
148
build as buildCmd,
159
exit as exitCmd,
1610
validateForRequiredValues
1711
} from "../../lib/commandBuilder";
12+
import { PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE } from "../../lib/constants";
1813
import { IAzureDevOpsOpts } from "../../lib/git";
1914
import { addVariableGroup } from "../../lib/pipelines/variableGroup";
2015
import { hasValue } from "../../lib/validator";
2116
import { logger } from "../../logger";
2217
import {
2318
IAzurePipelinesYaml,
2419
IBedrockFile,
20+
IBedrockFileInfo,
2521
IVariableGroupData,
2622
IVariableGroupDataVariable
2723
} from "../../types";
@@ -39,6 +35,13 @@ interface ICommandOptions {
3935
project: string | undefined;
4036
}
4137

38+
export const checkDependencies = (projectPath: string) => {
39+
const fileInfo: IBedrockFileInfo = bedrockFileInfo(projectPath);
40+
if (fileInfo.exist === false) {
41+
throw new Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE);
42+
}
43+
};
44+
4245
/**
4346
* Executes the command.
4447
*
@@ -59,12 +62,7 @@ export const execute = async (
5962
const projectPath = process.cwd();
6063
logger.verbose(`project path: ${projectPath}`);
6164

62-
const fileInfo = await bedrockFileInfo(projectPath);
63-
if (fileInfo.exist === false) {
64-
logger.error(projectInitDependencyErrorMessage);
65-
await exitFn(1);
66-
return;
67-
}
65+
checkDependencies(projectPath);
6866

6967
const { azure_devops } = Config();
7068

src/commands/project/pipeline.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { create as createBedrockYaml } from "../../lib/bedrockYaml";
22
import { createTempDir } from "../../lib/ioUtil";
3-
import { disableVerboseLogging, enableVerboseLogging } from "../../logger";
3+
import {
4+
disableVerboseLogging,
5+
enableVerboseLogging,
6+
logger
7+
} from "../../logger";
48
jest.mock("../../lib/pipelines/pipelines");
59

610
import {
@@ -10,11 +14,11 @@ import {
1014
} from "../../lib/pipelines/pipelines";
1115

1216
import {
17+
checkDependencies,
1318
execute,
1419
fetchValidateValues,
1520
ICommandOptions,
16-
installLifecyclePipeline,
17-
validate
21+
installLifecyclePipeline
1822
} from "./pipeline";
1923

2024
beforeAll(() => {
@@ -48,10 +52,10 @@ const mockMissingValues: ICommandOptions = {
4852
const gitUrl = "https://github.com/CatalystCode/spk.git";
4953

5054
describe("test valid function", () => {
51-
it("negative test", () => {
55+
it("negative test", async () => {
5256
try {
5357
const tmpDir = createBedrockYaml();
54-
validate(tmpDir);
58+
await checkDependencies(tmpDir);
5559
expect(true).toBe(false);
5660
} catch (e) {
5761
expect(e).not.toBeNull();

src/commands/project/pipeline.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import {
44
BuildDefinitionVariable
55
} from "azure-devops-node-api/interfaces/BuildInterfaces";
66
import commander from "commander";
7-
import { bedrockFileInfo, Config } from "../../config";
8-
import {
9-
projectCvgDependencyErrorMessage,
10-
projectInitCvgDependencyErrorMessage
11-
} from "../../constants";
12-
import { isExists as isBedrockFileExists } from "../../lib/bedrockYaml";
7+
import { Config } from "../../config";
8+
import { fileInfo as bedrockFileInfo } from "../../lib/bedrockYaml";
139
import {
1410
build as buildCmd,
1511
exit as exitCmd,
1612
validateForRequiredValues
1713
} from "../../lib/commandBuilder";
14+
import {
15+
PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE,
16+
PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE
17+
} from "../../lib/constants";
1818
import { BUILD_SCRIPT_URL } from "../../lib/constants";
1919
import {
2020
getOriginUrl,
@@ -41,12 +41,12 @@ export interface ICommandOptions {
4141
buildScriptUrl: string | undefined;
4242
}
4343

44-
export const validate = async (projectPath: string) => {
45-
const exist = isBedrockFileExists(projectPath);
46-
if (exist === false) {
47-
throw new Error(
48-
"Please run `spk project init` command before running this command to initialize the project."
49-
);
44+
export const checkDependencies = (projectPath: string) => {
45+
const file: IBedrockFileInfo = bedrockFileInfo(projectPath);
46+
if (file.exist === false) {
47+
throw new Error(PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE);
48+
} else if (file.hasVariableGroups === false) {
49+
throw new Error(PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE);
5050
}
5151
};
5252

@@ -113,22 +113,10 @@ export const execute = async (
113113
return;
114114
}
115115

116-
const fileInfo: IBedrockFileInfo = await bedrockFileInfo(projectPath);
117-
if (fileInfo.exist === false) {
118-
logger.error(projectInitCvgDependencyErrorMessage);
119-
await exitFn(1);
120-
return;
121-
}
122-
if (fileInfo.hasVariableGroups === false) {
123-
logger.error(projectCvgDependencyErrorMessage);
124-
await exitFn(1);
125-
return;
126-
}
127-
128116
logger.verbose(`project path: ${projectPath}`);
129117

130118
try {
131-
await validate(projectPath);
119+
checkDependencies(projectPath);
132120
const gitOriginUrl = await getOriginUrl();
133121
const values = fetchValidateValues(opts, gitOriginUrl, Config());
134122

src/commands/service/create.test.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { promisify } from "util";
44
import uuid from "uuid/v4";
55
import { Bedrock } from "../../config";
66
import * as config from "../../config";
7+
import * as bedrockYaml from "../../lib/bedrockYaml";
78
import { DEFAULT_CONTENT as BedrockMockedContent } from "../../lib/bedrockYaml";
89
import { checkoutCommitPushCreatePRLink } from "../../lib/gitutils";
910
import { createTempDir, removeDir } from "../../lib/ioUtil";
@@ -115,12 +116,10 @@ describe("Test execute function", () => {
115116
it("Negative test: missing bedrock file", async () => {
116117
const testServiceName = uuid();
117118
const exitFn = jest.fn();
118-
jest.spyOn(config, "bedrockFileInfo").mockImplementation(() =>
119-
Promise.resolve({
120-
exist: false,
121-
hasVariableGroups: false
122-
})
123-
);
119+
jest.spyOn(bedrockYaml, "fileInfo").mockImplementation(() => ({
120+
exist: false,
121+
hasVariableGroups: false
122+
}));
124123
try {
125124
await execute(testServiceName, getMockValues(), exitFn);
126125
expect(exitFn).toBeCalledTimes(1);
@@ -132,26 +131,10 @@ describe("Test execute function", () => {
132131
it("Negative test: missing bedrock variable groups", async () => {
133132
const testServiceName = uuid();
134133
const exitFn = jest.fn();
135-
jest.spyOn(config, "bedrockFileInfo").mockImplementation(() =>
136-
Promise.resolve({
137-
exist: true,
138-
hasVariableGroups: false
139-
})
140-
);
141-
try {
142-
await execute(testServiceName, getMockValues(), exitFn);
143-
expect(exitFn).toBeCalledTimes(1);
144-
expect(exitFn.mock.calls).toEqual([[1]]);
145-
} finally {
146-
removeDir(testServiceName); // housekeeping
147-
}
148-
});
149-
it("Negative test: simulated exception thrown", async () => {
150-
const testServiceName = uuid();
151-
const exitFn = jest.fn();
152-
jest
153-
.spyOn(config, "bedrockFileInfo")
154-
.mockImplementation(() => Promise.reject());
134+
jest.spyOn(bedrockYaml, "fileInfo").mockImplementation(() => ({
135+
exist: true,
136+
hasVariableGroups: false
137+
}));
155138
try {
156139
await execute(testServiceName, getMockValues(), exitFn);
157140
expect(exitFn).toBeCalledTimes(1);

src/commands/service/create.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import commander from "commander";
22
import path from "path";
33
import shelljs from "shelljs";
4-
import { Bedrock, bedrockFileInfo } from "../../config";
5-
import {
6-
projectCvgDependencyErrorMessage,
7-
projectInitCvgDependencyErrorMessage
8-
} from "../../constants";
4+
import { Bedrock } from "../../config";
95
import {
106
addNewService as addNewServiceToBedrockFile,
7+
fileInfo as bedrockFileInfo,
118
YAML_NAME as BedrockFileName
129
} from "../../lib/bedrockYaml";
1310
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
11+
import {
12+
PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE,
13+
PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE
14+
} from "../../lib/constants";
1415
import {
1516
addNewServiceToMaintainersFile,
1617
generateDockerfile,
@@ -88,6 +89,15 @@ export const fetchValues = (opts: ICommandOptions) => {
8889
return values;
8990
};
9091

92+
export const checkDependencies = (projectPath: string) => {
93+
const fileInfo: IBedrockFileInfo = bedrockFileInfo(projectPath);
94+
if (fileInfo.exist === false) {
95+
throw new Error(PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE);
96+
} else if (fileInfo.hasVariableGroups === false) {
97+
throw new Error(PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE);
98+
}
99+
};
100+
91101
export const execute = async (
92102
serviceName: string,
93103
opts: ICommandOptions,
@@ -103,19 +113,7 @@ export const execute = async (
103113
logger.verbose(`project path: ${projectPath}`);
104114

105115
try {
106-
const fileInfo: IBedrockFileInfo = await bedrockFileInfo(projectPath);
107-
if (fileInfo.exist === false) {
108-
logger.error(projectInitCvgDependencyErrorMessage());
109-
await exitFn(1);
110-
return;
111-
}
112-
113-
if (fileInfo.hasVariableGroups === false) {
114-
logger.error(projectCvgDependencyErrorMessage());
115-
await exitFn(1);
116-
return;
117-
}
118-
116+
checkDependencies(projectPath);
119117
const values = fetchValues(opts);
120118
await createService(projectPath, serviceName, values);
121119
await exitFn(0);

src/config.test.ts

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import fs from "fs";
2-
import yaml from "js-yaml";
31
import os from "os";
42
import path from "path";
53
import shell from "shelljs";
64
import uuid from "uuid/v4";
7-
import { Bedrock, bedrockFileInfo, write } from "./config";
5+
import { Bedrock, write } from "./config";
86
import { disableVerboseLogging, enableVerboseLogging, logger } from "./logger";
97
import { IBedrockFile, IBedrockFileInfo } from "./types";
108

@@ -103,85 +101,3 @@ describe("Bedrock", () => {
103101
expect(error).toBeDefined();
104102
});
105103
});
106-
107-
describe("isBedrockFileValid", () => {
108-
test("Should fail when empty file directory is passed", async () => {
109-
let invalidDirError: Error | undefined;
110-
111-
try {
112-
logger.info("calling create");
113-
await bedrockFileInfo("");
114-
} catch (err) {
115-
invalidDirError = err;
116-
}
117-
expect(invalidDirError).toBeDefined();
118-
});
119-
120-
test("Should return false when bedrock file does not exist", async () => {
121-
// Create random directory to initialize
122-
const randomTmpDir = path.join(os.tmpdir(), uuid());
123-
fs.mkdirSync(randomTmpDir);
124-
125-
const fileInfo: IBedrockFileInfo = await bedrockFileInfo(randomTmpDir);
126-
127-
logger.info(`bedrock.yaml file exists: ${fileInfo.exist}`);
128-
129-
expect(fileInfo.exist).toBe(false);
130-
});
131-
132-
test("Should pass when bedrock file exists with variable groups length 0", async () => {
133-
// Create random directory to initialize
134-
const randomTmpDir = path.join(os.tmpdir(), uuid());
135-
fs.mkdirSync(randomTmpDir);
136-
137-
logger.info(`random temp dir: ${randomTmpDir}`);
138-
139-
// create bedrock file to simulate the the use case that `spk project init` ran before
140-
const bedrockFileData: IBedrockFile = {
141-
rings: {},
142-
services: {},
143-
variableGroups: []
144-
};
145-
146-
const asYaml = yaml.safeDump(bedrockFileData, {
147-
lineWidth: Number.MAX_SAFE_INTEGER
148-
});
149-
fs.writeFileSync(path.join(randomTmpDir, "bedrock.yaml"), asYaml);
150-
151-
const fileInfo: IBedrockFileInfo = await bedrockFileInfo(randomTmpDir);
152-
logger.verbose(
153-
`bedrock.yaml file exists: ${fileInfo.exist} in ${randomTmpDir}`
154-
);
155-
156-
expect(fileInfo.exist).toBe(true);
157-
expect(fileInfo.hasVariableGroups).toBe(false);
158-
});
159-
160-
test("Should pass when bedrock file exists with one variable group", async () => {
161-
// Create random directory to initialize
162-
const randomTmpDir = path.join(os.tmpdir(), uuid());
163-
fs.mkdirSync(randomTmpDir);
164-
165-
logger.info(`random temp dir: ${randomTmpDir}`);
166-
167-
// create bedrock file to simulate the the use case that `spk project init` ran before
168-
const bedrockFileData: IBedrockFile = {
169-
rings: {},
170-
services: {},
171-
variableGroups: [variableGroupName]
172-
};
173-
174-
const asYaml = yaml.safeDump(bedrockFileData, {
175-
lineWidth: Number.MAX_SAFE_INTEGER
176-
});
177-
fs.writeFileSync(path.join(randomTmpDir, "bedrock.yaml"), asYaml);
178-
179-
const fileInfo: IBedrockFileInfo = await bedrockFileInfo(randomTmpDir);
180-
logger.info(
181-
`bedrock.yaml file exists: ${fileInfo.exist} in ${randomTmpDir}`
182-
);
183-
184-
expect(fileInfo.exist).toBe(true);
185-
expect(fileInfo.hasVariableGroups).toBe(true);
186-
});
187-
});

0 commit comments

Comments
 (0)