Skip to content

Commit d3ccafc

Browse files
committed
feat: add version string functions to constants
Introduce helper functions for getting human- and machine-readable version strings from the constants package, and cover it in unit tests. This is a first step to resolving #4874.
1 parent a989e0c commit d3ccafc

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

src/node/constants.ts

+17
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,24 @@ export const version = pkg.version || "development"
2323
export const commit = pkg.commit || "development"
2424
export const rootPath = path.resolve(__dirname, "../..")
2525
export const vsRootPath = path.join(rootPath, "vendor/modules/code-oss-dev")
26+
export const codeVersion = require(path.join(vsRootPath, "package.json")).version
2627
export const tmpdir = path.join(os.tmpdir(), "code-server")
2728
export const isDevMode = commit === "development"
2829
export const httpProxyUri =
2930
process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy
31+
32+
// getVersionString returns a human-readable version string suitable
33+
// for outputting to the console.
34+
export function getVersionString(): string {
35+
return [ version, commit ].join(" ")
36+
}
37+
38+
// getVersionJsonString returns a machine-readable version string
39+
// suitable for outputting to the console.
40+
export function getVersionJsonString(): string {
41+
return JSON.stringify({
42+
codeServer: version,
43+
commit,
44+
vscode: codeVersion,
45+
})
46+
}

test/unit/node/constants.test.ts

+47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from "@coder/logger"
22
import { mockLogger } from "../../utils/helpers"
3+
import * as semver from "semver"
34

45
describe("constants", () => {
56
let constants: typeof import("../../../src/node/constants")
@@ -13,9 +14,15 @@ describe("constants", () => {
1314
commit: "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b",
1415
}
1516

17+
const mockCodePackageJson = {
18+
name: "mock-code-oss-dev",
19+
version: "1.2.3",
20+
}
21+
1622
beforeAll(() => {
1723
mockLogger()
1824
jest.mock("../../../package.json", () => mockPackageJson, { virtual: true })
25+
jest.mock("../../../vendor/modules/code-oss-dev/package.json", () => mockCodePackageJson, { virtual: true })
1926
constants = require("../../../src/node/constants")
2027
})
2128

@@ -24,12 +31,44 @@ describe("constants", () => {
2431
jest.resetModules()
2532
})
2633

34+
it("should provide the package name", () => {
35+
expect(constants.pkgName).toBe(mockPackageJson.name)
36+
})
37+
2738
it("should provide the commit", () => {
2839
expect(constants.commit).toBe(mockPackageJson.commit)
2940
})
3041

3142
it("should return the package.json version", () => {
3243
expect(constants.version).toBe(mockPackageJson.version)
44+
45+
// Ensure the version is parseable as semver and equal
46+
const actual = semver.parse(constants.version)
47+
const expected = semver.parse(mockPackageJson.version)
48+
expect(actual).toBeTruthy()
49+
expect(actual).toStrictEqual(expected)
50+
})
51+
52+
it("should include embedded Code version information", () => {
53+
expect(constants.codeVersion).toBe(mockCodePackageJson.version)
54+
55+
// Ensure the version is parseable as semver and equal
56+
const actual = semver.parse(constants.codeVersion)
57+
const expected = semver.parse(mockCodePackageJson.version)
58+
expect(actual).toBeTruthy()
59+
expect(actual).toStrictEqual(expected)
60+
})
61+
62+
it("should return a human-readable version string", () => {
63+
expect(constants.getVersionString()).toStrictEqual(`${mockPackageJson.version} ${mockPackageJson.commit}`)
64+
})
65+
66+
it("should return a machine-readable version string", () => {
67+
expect(constants.getVersionJsonString()).toStrictEqual(JSON.stringify({
68+
codeServer: mockPackageJson.version,
69+
commit: mockPackageJson.commit,
70+
vscode: mockCodePackageJson.version,
71+
}))
3372
})
3473

3574
describe("getPackageJson", () => {
@@ -47,6 +86,9 @@ describe("constants", () => {
4786
// so to get the root package.json we need to use ../../
4887
const packageJson = constants.getPackageJson("../../package.json")
4988
expect(packageJson).toStrictEqual(mockPackageJson)
89+
90+
const codePackageJson = constants.getPackageJson("../../vendor/modules/code-oss-dev/package.json")
91+
expect(codePackageJson).toStrictEqual(mockCodePackageJson)
5092
})
5193
})
5294
})
@@ -55,9 +97,13 @@ describe("constants", () => {
5597
const mockPackageJson = {
5698
name: "mock-code-server",
5799
}
100+
const mockCodePackageJson = {
101+
name: "mock-code-oss-dev",
102+
}
58103

59104
beforeAll(() => {
60105
jest.mock("../../../package.json", () => mockPackageJson, { virtual: true })
106+
jest.mock("../../../vendor/modules/code-oss-dev/package.json", () => mockCodePackageJson, { virtual: true })
61107
constants = require("../../../src/node/constants")
62108
})
63109

@@ -69,6 +115,7 @@ describe("constants", () => {
69115
it("version should return 'development'", () => {
70116
expect(constants.version).toBe("development")
71117
})
118+
72119
it("commit should return 'development'", () => {
73120
expect(constants.commit).toBe("development")
74121
})

0 commit comments

Comments
 (0)