2021-12-17 20:06:52 +01:00
|
|
|
import { logger } from "@coder/logger"
|
2022-03-12 00:54:59 +01:00
|
|
|
import path from "path"
|
2022-03-15 03:37:29 +01:00
|
|
|
import * as semver from "semver"
|
|
|
|
import { mockLogger } from "../../utils/helpers"
|
2021-02-23 23:59:17 +01:00
|
|
|
|
2021-02-09 00:21:37 +01:00
|
|
|
describe("constants", () => {
|
2021-07-28 01:52:57 +02:00
|
|
|
let constants: typeof import("../../../src/node/constants")
|
2021-05-06 18:25:29 +02:00
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
describe("with package.json defined", () => {
|
2021-05-06 18:25:29 +02:00
|
|
|
const mockPackageJson = {
|
2021-05-06 01:38:54 +02:00
|
|
|
name: "mock-code-server",
|
|
|
|
description: "Run VS Code on a remote server.",
|
2022-02-01 17:45:19 +01:00
|
|
|
repository: "https://github.com/coder/code-server",
|
2021-05-06 01:38:54 +02:00
|
|
|
version: "1.0.0",
|
|
|
|
commit: "f6b2be2838f4afb217c2fd8f03eafedd8d55ef9b",
|
|
|
|
}
|
|
|
|
|
2022-02-28 22:55:47 +01:00
|
|
|
const mockCodePackageJson = {
|
2022-03-15 03:37:29 +01:00
|
|
|
name: "mock-vscode",
|
2022-02-28 22:55:47 +01:00
|
|
|
version: "1.2.3",
|
|
|
|
}
|
|
|
|
|
2021-05-06 18:25:29 +02:00
|
|
|
beforeAll(() => {
|
2022-03-12 00:54:59 +01:00
|
|
|
jest.clearAllMocks()
|
2021-12-17 20:06:52 +01:00
|
|
|
mockLogger()
|
2022-03-12 00:54:59 +01:00
|
|
|
jest.mock(path.resolve(__dirname, "../../../package.json"), () => mockPackageJson, { virtual: true })
|
2022-03-15 03:37:29 +01:00
|
|
|
jest.mock(path.resolve(__dirname, "../../../lib/vscode/package.json"), () => mockCodePackageJson, {
|
|
|
|
virtual: true,
|
|
|
|
})
|
2021-07-28 01:52:57 +02:00
|
|
|
constants = require("../../../src/node/constants")
|
2021-02-09 00:21:37 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(() => {
|
2021-05-05 01:55:09 +02:00
|
|
|
jest.resetModules()
|
2021-02-09 00:21:37 +01:00
|
|
|
})
|
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
it("should provide the commit", () => {
|
2021-05-06 18:25:29 +02:00
|
|
|
expect(constants.commit).toBe(mockPackageJson.commit)
|
2021-02-09 00:21:37 +01:00
|
|
|
})
|
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
it("should return the package.json version", () => {
|
2021-05-06 18:25:29 +02:00
|
|
|
expect(constants.version).toBe(mockPackageJson.version)
|
2022-02-28 22:55:47 +01:00
|
|
|
|
|
|
|
// 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", () => {
|
2022-03-01 21:20:43 +01:00
|
|
|
expect(constants.getVersionString()).toStrictEqual(
|
|
|
|
`${mockPackageJson.version} ${mockPackageJson.commit} with Code ${mockCodePackageJson.version}`,
|
|
|
|
)
|
2022-02-28 22:55:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should return a machine-readable version string", () => {
|
|
|
|
expect(constants.getVersionJsonString()).toStrictEqual(
|
|
|
|
JSON.stringify({
|
|
|
|
codeServer: mockPackageJson.version,
|
|
|
|
commit: mockPackageJson.commit,
|
|
|
|
vscode: mockCodePackageJson.version,
|
|
|
|
}),
|
|
|
|
)
|
2021-02-09 00:21:37 +01:00
|
|
|
})
|
2021-05-05 01:55:09 +02:00
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
describe("getPackageJson", () => {
|
|
|
|
it("should log a warning if package.json not found", () => {
|
|
|
|
const expectedErrorMessage = "Cannot find module './package.json' from 'src/node/constants.ts'"
|
2021-05-05 01:55:09 +02:00
|
|
|
|
2021-05-06 18:25:29 +02:00
|
|
|
constants.getPackageJson("./package.json")
|
2021-05-05 01:55:09 +02:00
|
|
|
|
2021-12-17 20:06:52 +01:00
|
|
|
expect(logger.warn).toHaveBeenCalled()
|
|
|
|
expect(logger.warn).toHaveBeenCalledWith(expectedErrorMessage)
|
2021-05-05 01:55:09 +02:00
|
|
|
})
|
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
it("should find the package.json", () => {
|
|
|
|
// the function calls require from src/node/constants
|
|
|
|
// so to get the root package.json we need to use ../../
|
2021-05-06 18:25:29 +02:00
|
|
|
const packageJson = constants.getPackageJson("../../package.json")
|
|
|
|
expect(packageJson).toStrictEqual(mockPackageJson)
|
2022-02-28 22:55:47 +01:00
|
|
|
|
2022-03-15 03:37:29 +01:00
|
|
|
const codePackageJson = constants.getPackageJson("../../lib/vscode/package.json")
|
2022-02-28 22:55:47 +01:00
|
|
|
expect(codePackageJson).toStrictEqual(mockCodePackageJson)
|
2021-05-05 01:55:09 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
describe("with incomplete package.json", () => {
|
2021-05-06 18:25:29 +02:00
|
|
|
const mockPackageJson = {
|
2021-05-06 01:38:54 +02:00
|
|
|
name: "mock-code-server",
|
|
|
|
}
|
2022-02-28 22:55:47 +01:00
|
|
|
const mockCodePackageJson = {
|
2022-03-15 03:37:29 +01:00
|
|
|
name: "mock-vscode",
|
2022-02-28 22:55:47 +01:00
|
|
|
}
|
2021-05-06 01:38:54 +02:00
|
|
|
|
2021-05-06 18:25:29 +02:00
|
|
|
beforeAll(() => {
|
2022-03-12 00:54:59 +01:00
|
|
|
jest.clearAllMocks()
|
|
|
|
jest.mock(path.resolve(__dirname, "../../../package.json"), () => mockPackageJson, { virtual: true })
|
2022-03-15 03:37:29 +01:00
|
|
|
jest.mock(path.resolve(__dirname, "../../../lib/vscode/package.json"), () => mockCodePackageJson, {
|
|
|
|
virtual: true,
|
|
|
|
})
|
2021-07-28 01:52:57 +02:00
|
|
|
constants = require("../../../src/node/constants")
|
2021-05-05 01:55:09 +02:00
|
|
|
})
|
|
|
|
|
2021-05-06 18:25:29 +02:00
|
|
|
afterAll(() => {
|
2021-05-06 01:38:54 +02:00
|
|
|
jest.resetModules()
|
|
|
|
})
|
2021-05-05 01:55:09 +02:00
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
it("version should return 'development'", () => {
|
2021-05-06 18:25:29 +02:00
|
|
|
expect(constants.version).toBe("development")
|
2021-05-06 01:38:54 +02:00
|
|
|
})
|
2022-02-28 22:55:47 +01:00
|
|
|
|
2021-05-06 01:38:54 +02:00
|
|
|
it("commit should return 'development'", () => {
|
2021-05-06 18:25:29 +02:00
|
|
|
expect(constants.commit).toBe("development")
|
2021-02-09 00:21:37 +01:00
|
|
|
})
|
2022-02-28 22:55:47 +01:00
|
|
|
|
|
|
|
it("should return a human-readable version string", () => {
|
|
|
|
// this string is not super useful
|
2022-03-01 21:20:43 +01:00
|
|
|
expect(constants.getVersionString()).toStrictEqual("development development with Code development")
|
2022-02-28 22:55:47 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it("should return a machine-readable version string", () => {
|
|
|
|
expect(constants.getVersionJsonString()).toStrictEqual(
|
|
|
|
JSON.stringify({
|
|
|
|
codeServer: "development",
|
|
|
|
commit: "development",
|
|
|
|
vscode: "development",
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
2021-02-09 00:21:37 +01:00
|
|
|
})
|
|
|
|
})
|