Archived
1
0

feat: add version string functions to constants (#4920)

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.
This commit is contained in:
Jonathan Yu 2022-02-28 13:55:47 -08:00 committed by GitHub
parent a989e0c387
commit 44d74c170f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 0 deletions

View File

@ -17,13 +17,35 @@ export function getPackageJson(relativePath: string): JSONSchemaForNPMPackageJso
}
const pkg = getPackageJson("../../package.json")
const codePkg = getPackageJson("../../vendor/modules/code-oss-dev/package.json")
export const pkgName = pkg.name || "code-server"
export const version = pkg.version || "development"
export const commit = pkg.commit || "development"
export const rootPath = path.resolve(__dirname, "../..")
export const vsRootPath = path.join(rootPath, "vendor/modules/code-oss-dev")
export const codeVersion = codePkg.version || "development"
export const tmpdir = path.join(os.tmpdir(), "code-server")
export const isDevMode = commit === "development"
export const httpProxyUri =
process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy
/**
* getVersionString returns a human-readable version string suitable
* for outputting to the console.
*/
export function getVersionString(): string {
return [version, commit].join(" ")
}
/**
* getVersionJsonString returns a machine-readable version string
* suitable for outputting to the console.
*/
export function getVersionJsonString(): string {
return JSON.stringify({
codeServer: version,
commit,
vscode: codeVersion,
})
}

View File

@ -1,5 +1,6 @@
import { logger } from "@coder/logger"
import { mockLogger } from "../../utils/helpers"
import * as semver from "semver"
describe("constants", () => {
let constants: typeof import("../../../src/node/constants")
@ -13,9 +14,15 @@ describe("constants", () => {
commit: "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b",
}
const mockCodePackageJson = {
name: "mock-code-oss-dev",
version: "1.2.3",
}
beforeAll(() => {
mockLogger()
jest.mock("../../../package.json", () => mockPackageJson, { virtual: true })
jest.mock("../../../vendor/modules/code-oss-dev/package.json", () => mockCodePackageJson, { virtual: true })
constants = require("../../../src/node/constants")
})
@ -24,12 +31,46 @@ describe("constants", () => {
jest.resetModules()
})
it("should provide the package name", () => {
expect(constants.pkgName).toBe(mockPackageJson.name)
})
it("should provide the commit", () => {
expect(constants.commit).toBe(mockPackageJson.commit)
})
it("should return the package.json version", () => {
expect(constants.version).toBe(mockPackageJson.version)
// Ensure the version is parseable as semver and equal
const actual = semver.parse(constants.version)
const expected = semver.parse(mockPackageJson.version)
expect(actual).toBeTruthy()
expect(actual).toStrictEqual(expected)
})
it("should include embedded Code version information", () => {
expect(constants.codeVersion).toBe(mockCodePackageJson.version)
// Ensure the version is parseable as semver and equal
const actual = semver.parse(constants.codeVersion)
const expected = semver.parse(mockCodePackageJson.version)
expect(actual).toBeTruthy()
expect(actual).toStrictEqual(expected)
})
it("should return a human-readable version string", () => {
expect(constants.getVersionString()).toStrictEqual(`${mockPackageJson.version} ${mockPackageJson.commit}`)
})
it("should return a machine-readable version string", () => {
expect(constants.getVersionJsonString()).toStrictEqual(
JSON.stringify({
codeServer: mockPackageJson.version,
commit: mockPackageJson.commit,
vscode: mockCodePackageJson.version,
}),
)
})
describe("getPackageJson", () => {
@ -47,6 +88,9 @@ describe("constants", () => {
// so to get the root package.json we need to use ../../
const packageJson = constants.getPackageJson("../../package.json")
expect(packageJson).toStrictEqual(mockPackageJson)
const codePackageJson = constants.getPackageJson("../../vendor/modules/code-oss-dev/package.json")
expect(codePackageJson).toStrictEqual(mockCodePackageJson)
})
})
})
@ -55,9 +99,13 @@ describe("constants", () => {
const mockPackageJson = {
name: "mock-code-server",
}
const mockCodePackageJson = {
name: "mock-code-oss-dev",
}
beforeAll(() => {
jest.mock("../../../package.json", () => mockPackageJson, { virtual: true })
jest.mock("../../../vendor/modules/code-oss-dev/package.json", () => mockCodePackageJson, { virtual: true })
constants = require("../../../src/node/constants")
})
@ -69,8 +117,24 @@ describe("constants", () => {
it("version should return 'development'", () => {
expect(constants.version).toBe("development")
})
it("commit should return 'development'", () => {
expect(constants.commit).toBe("development")
})
it("should return a human-readable version string", () => {
// this string is not super useful
expect(constants.getVersionString()).toStrictEqual("development development")
})
it("should return a machine-readable version string", () => {
expect(constants.getVersionJsonString()).toStrictEqual(
JSON.stringify({
codeServer: "development",
commit: "development",
vscode: "development",
}),
)
})
})
})