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

Commit 331bc30

Browse files
authored
1 parent d8f290c commit 331bc30

15 files changed

+91
-30
lines changed

src/commands/hld/reconcile.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ describe("reconcile tests", () => {
431431
k8sBackend: "cool-service",
432432
k8sBackendPort: 1337
433433
}
434-
}
434+
},
435+
version: "1.0"
435436
};
436437
});
437438

@@ -498,7 +499,8 @@ describe("reconcile tests", () => {
498499
},
499500
k8sBackendPort: 1337
500501
}
501-
}
502+
},
503+
version: "1.0"
502504
};
503505

504506
await reconcileHld(

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ describe("setVariableGroupInBedrockFile", () => {
224224
const bedrockFileData: BedrockFile = {
225225
rings: {}, // rings is optional but necessary to create a bedrock file in config.write method
226226
services: {}, // service property is not optional so set it to null
227-
variableGroups: [prevariableGroupName]
227+
variableGroups: [prevariableGroupName],
228+
version: ""
228229
};
229230

230231
const randomTmpDir = createBedrockYaml("", bedrockFileData);

src/commands/project/init.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fs from "fs";
2+
import path from "path";
13
import uuid from "uuid/v4";
24
import { Bedrock, Maintainers, write } from "../../config";
35
import {
@@ -8,6 +10,7 @@ import { createTempDir, getMissingFilenames } from "../../lib/ioUtil";
810
import { BedrockFile, MaintainersFile } from "../../types";
911
import { execute, initialize } from "./init";
1012
import * as init from "./init";
13+
import { getVersionMessage } from "../../lib/fileutils";
1114

1215
const CREATED_FILES = [
1316
".gitignore",
@@ -17,10 +20,18 @@ const CREATED_FILES = [
1720
];
1821

1922
describe("Initializing a blank/new bedrock repository", () => {
23+
const writeSpy = jest.spyOn(fs, "writeFileSync");
2024
test("all standard files get generated in the project root on init", async () => {
2125
const randomTmpDir = createTempDir();
2226
await initialize(randomTmpDir);
2327

28+
const expectedFilePath = path.join(randomTmpDir, "bedrock.yaml");
29+
expect(writeSpy).toBeCalledWith(
30+
expectedFilePath,
31+
`${getVersionMessage()}\n`,
32+
"utf8"
33+
);
34+
2435
// bedrock.yaml, maintainers.yaml should be in a the root for a 'standard' project
2536
const missing = getMissingFilenames(randomTmpDir, CREATED_FILES);
2637
expect(missing.length).toBe(0); // no files are missing hence length 0
@@ -58,7 +69,8 @@ describe("initializing an existing file does not modify it", () => {
5869
},
5970
k8sBackendPort: 1337
6071
}
61-
}
72+
},
73+
version: "1.0"
6274
};
6375
write(bedrockFile, randomDir);
6476
await initialize(randomDir);

src/commands/project/init.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { Bedrock, write } from "../../config";
55
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
66
import {
77
generateGitIgnoreFile,
8-
generateHldLifecyclePipelineYaml
8+
generateHldLifecyclePipelineYaml,
9+
getVersion
910
} from "../../lib/fileutils";
1011
import { exec } from "../../lib/shell";
1112
import { logger } from "../../logger";
@@ -37,7 +38,8 @@ const generateBedrockFile = (
3738
},
3839
{}
3940
),
40-
services: {}
41+
services: {},
42+
version: getVersion()
4143
};
4244

4345
// Check if a bedrock.yaml already exists; skip write if present

src/commands/project/pipeline.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ describe("installLifecyclePipeline and execute tests", () => {
115115
createBedrockYaml(tmpDir, {
116116
rings: {},
117117
services: {},
118-
variableGroups: ["test"]
118+
variableGroups: ["test"],
119+
version: "1.0"
119120
});
120121
await execute(mockValues, tmpDir, exitFn);
121122

src/commands/ring/create.test.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ describe("checkDependencies", () => {
3333
}
3434
},
3535
services: {},
36-
variableGroups: ["testvg"]
36+
variableGroups: ["testvg"],
37+
version: "1.0"
3738
});
3839
checkDependencies(tmpDir, "not-master");
3940
// No errors thrown, this is a pass for the function.
@@ -46,7 +47,8 @@ describe("checkDependencies", () => {
4647
}
4748
},
4849
services: {},
49-
variableGroups: ["testvg"]
50+
variableGroups: ["testvg"],
51+
version: "1.0"
5052
});
5153
expect(() => {
5254
checkDependencies(tmpDir, "master");
@@ -104,7 +106,8 @@ describe("test execute function and logic", () => {
104106
k8sBackendPort: 80
105107
}
106108
},
107-
variableGroups: ["testvg"]
109+
variableGroups: ["testvg"],
110+
version: "1.0"
108111
});
109112

110113
const newRingName = "my-new-ring";

src/commands/ring/set-default.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ describe("test execute function and logic", () => {
4242
prod: {}
4343
},
4444
services: {},
45-
variableGroups: ["testvg"]
45+
variableGroups: ["testvg"],
46+
version: "1.0"
4647
});
4748
await execute("prod", tmpDir, exitFn);
4849

src/commands/service/create-revision.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ describe("Default rings", () => {
6565
pathPrefix: "servicepath",
6666
pathPrefixMajorVersion: "v1"
6767
}
68-
}
68+
},
69+
version: "1.0"
6970
};
7071

7172
write(validBedrockYaml, randomTmpDir);
@@ -96,7 +97,8 @@ describe("Default rings", () => {
9697
pathPrefix: "servicepath",
9798
pathPrefixMajorVersion: "v1"
9899
}
99-
}
100+
},
101+
version: "1.0"
100102
};
101103

102104
write(validBedrockYaml, randomTmpDir);

src/config.test.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os from "os";
22
import path from "path";
3+
import fs from "fs";
34
import shell from "shelljs";
45
import uuid from "uuid/v4";
56
import {
@@ -14,6 +15,7 @@ import {
1415
import { createTempDir } from "./lib/ioUtil";
1516
import { disableVerboseLogging, enableVerboseLogging } from "./logger";
1617
import { BedrockFile } from "./types";
18+
import { getVersionMessage } from "./lib/fileutils";
1719

1820
beforeAll(() => {
1921
enableVerboseLogging();
@@ -51,6 +53,7 @@ describe("Test updateVariableWithLocalEnv function", () => {
5153
});
5254

5355
describe("Bedrock", () => {
56+
const writeSpy = jest.spyOn(fs, "writeFileSync");
5457
test("valid helm configuration passes", () => {
5558
const randomTmpDir = path.join(os.tmpdir(), uuid());
5659
shell.mkdir("-p", randomTmpDir);
@@ -82,9 +85,16 @@ describe("Bedrock", () => {
8285
pathPrefix: "servicepath",
8386
pathPrefixMajorVersion: "v1"
8487
}
85-
}
88+
},
89+
version: "1.0"
8690
};
8791
write(validBedrockYaml, randomTmpDir);
92+
const expectedFilePath = path.join(randomTmpDir, "bedrock.yaml");
93+
expect(writeSpy).toBeCalledWith(
94+
expectedFilePath,
95+
`${getVersionMessage()}\n`,
96+
"utf8"
97+
);
8898

8999
const bedrockConfig = Bedrock(randomTmpDir);
90100
expect(bedrockConfig).toBeTruthy();
@@ -113,10 +123,18 @@ describe("Bedrock", () => {
113123
}
114124
}
115125
}
116-
}
126+
},
127+
version: "1.0"
117128
// eslint-disable-next-line @typescript-eslint/no-explicit-any
118129
} as any;
119130
write(validBedrockYaml, randomTmpDir);
131+
const expectedFilePath = path.join(randomTmpDir, "bedrock.yaml");
132+
expect(writeSpy).toBeCalledWith(
133+
expectedFilePath,
134+
`${getVersionMessage()}\n`,
135+
"utf8"
136+
);
137+
120138
expect(() => {
121139
Bedrock(randomTmpDir);
122140
}).toThrow();

src/config.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,14 @@ export const write = (
237237
const asYaml = yaml.safeDump(file, { lineWidth: Number.MAX_SAFE_INTEGER });
238238
if ("rings" in file) {
239239
// Is bedrock.yaml
240-
return fs.writeFileSync(path.join(targetDirectory, "bedrock.yaml"), asYaml);
240+
fileName = "bedrock.yaml";
241+
writeVersion(path.join(targetDirectory, fileName));
242+
return fs.appendFileSync(path.join(targetDirectory, fileName), asYaml);
241243
} else if ("services" in file) {
242244
// Is maintainers file
243-
return fs.writeFileSync(
244-
path.join(targetDirectory, "maintainers.yaml"),
245-
asYaml
246-
);
245+
fileName = "maintainers.yaml";
246+
writeVersion(path.join(targetDirectory, fileName));
247+
return fs.appendFileSync(path.join(targetDirectory, fileName), asYaml);
247248
} else {
248249
// Is azure pipelines yaml file
249250
if (typeof fileName === "undefined") {

src/lib/bedrockYaml.test.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ describe("Creation and Existence test on bedrock.yaml", () => {
3030
const dir = createTempDir();
3131
const data = {
3232
rings: {},
33-
services: {}
33+
services: {},
34+
version: "1.0"
3435
};
3536
create(dir, data);
3637
expect(isExists(dir)).toBe(true);
@@ -90,7 +91,8 @@ describe("Adding a new service to a Bedrock file", () => {
9091
pathPrefixMajorVersion
9192
}
9293
},
93-
variableGroups: []
94+
variableGroups: [],
95+
version: defaultBedrockFileObject.version
9496
};
9597

9698
expect(read(dir)).toEqual(expected);
@@ -118,7 +120,8 @@ describe("Adding a new ring to an existing bedrock.yaml", () => {
118120
services: {
119121
...(defaultBedrockFileObject as BedrockFile).services
120122
},
121-
variableGroups: []
123+
variableGroups: [],
124+
version: defaultBedrockFileObject.version
122125
};
123126

124127
expect(read(dir)).toEqual(expected);
@@ -146,7 +149,8 @@ describe("Bedrock file info", () => {
146149
const data = {
147150
rings: {},
148151
services: {},
149-
variableGroups: [uuid()]
152+
variableGroups: [uuid()],
153+
version: "1.0"
150154
};
151155
create(dir, data);
152156
const file = fileInfo(dir);
@@ -164,7 +168,8 @@ describe("Set default ring", () => {
164168
prod: {}
165169
},
166170
services: {},
167-
variableGroups: [uuid()]
171+
variableGroups: [uuid()],
172+
version: "1.0"
168173
};
169174
create(dir, data);
170175
setDefaultRing(data, "master", dir);
@@ -180,7 +185,8 @@ describe("Set default ring", () => {
180185
prod: { isDefault: true }
181186
},
182187
services: {},
183-
variableGroups: [uuid()]
188+
variableGroups: [uuid()],
189+
version: "1.0"
184190
};
185191
create(dir, data);
186192
setDefaultRing(data, "master", dir);

src/lib/bedrockYaml.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import path from "path";
44
import { createTempDir } from "../lib/ioUtil";
55
import { logger } from "../logger";
66
import { BedrockFile, BedrockFileInfo, HelmConfig, Rings } from "../types";
7+
import { writeVersion, getVersion } from "./fileutils";
78

89
export const YAML_NAME = "bedrock.yaml";
910

1011
export const DEFAULT_CONTENT: BedrockFile = {
1112
rings: {},
1213
services: {},
13-
variableGroups: []
14+
variableGroups: [],
15+
version: getVersion()
1416
};
1517

1618
/**
@@ -30,7 +32,8 @@ export const create = (dir?: string, data?: BedrockFile): string => {
3032
const asYaml = yaml.safeDump(data, {
3133
lineWidth: Number.MAX_SAFE_INTEGER
3234
});
33-
fs.writeFileSync(path.join(absPath, YAML_NAME), asYaml);
35+
writeVersion(path.join(absPath, YAML_NAME));
36+
fs.appendFileSync(path.join(absPath, YAML_NAME), asYaml);
3437
return absPath;
3538
};
3639

src/lib/fileutils.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,18 @@ export const serviceBuildAndUpdatePipeline = (
351351
return pipelineYaml;
352352
};
353353

354+
/**
355+
* Gets the spk version
356+
*/
357+
export const getVersion = (): string => {
358+
return require("../../package.json").version;
359+
};
360+
354361
/**
355362
* Gets the spk version message
356363
*/
357364
export const getVersionMessage = (): string => {
358-
return VERSION_MESSAGE + require("../../package.json").version;
365+
return VERSION_MESSAGE + getVersion();
359366
};
360367

361368
/**

src/test/mockFactory.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,8 @@ export const createTestBedrockYaml = (
301301
k8sBackendPort: 80
302302
}
303303
},
304-
variableGroups: []
304+
variableGroups: [],
305+
version: "1.0"
305306
};
306307

307308
return asString ? yaml.dump(data) : data;

src/types.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface BedrockFile {
5757
[relativeDirectory: string]: BedrockServiceConfig;
5858
};
5959
variableGroups?: string[];
60+
version: string;
6061
}
6162

6263
export interface BedrockServiceConfig {

0 commit comments

Comments
 (0)