Archived
1
0

Implement last opened functionality (#4633)

* Implement last opened functionality

Fixes https://github.com/cdr/code-server/issues/4619

* Fix test temp dirs not being cleaned up

* Mock logger everywhere

This suppresses all the error and debug output we generate which makes
it hard to actually find which test has failed.  It also gives us a
standard way to test logging for the few places we do that.

* Use separate data directories for unit test instances

Exactly as we do for the e2e tests.

* Add integration tests for vscode route

* Make settings use --user-data-dir

Without this test instances step on each other feet and they also
clobber your own non-test settings.

* Make redirects consistent

They will preserve the trailing slash if there is one.

* Remove compilation check

If you do a regular non-watch build there are no compilation stats so
this bricks VS Code in CI when running the unit tests.

I am not sure how best to fix this for the case where you have a build
that has not been packaged yet so I just removed it for now and added a
message to check if VS Code is compiling when in dev mode.

* Update code-server update endpoint name
This commit is contained in:
Asher
2021-12-17 13:06:52 -06:00
committed by GitHub
parent b990dabed1
commit c4c480a068
31 changed files with 406 additions and 241 deletions

View File

@ -1,9 +1,8 @@
import { promises as fs } from "fs"
import * as http from "http"
import * as path from "path"
import { tmpdir } from "../../../src/node/constants"
import { SettingsProvider, UpdateSettings } from "../../../src/node/settings"
import { LatestResponse, UpdateProvider } from "../../../src/node/update"
import { clean, mockLogger, tmpdir } from "../../utils/helpers"
describe("update", () => {
let version = "1.0.0"
@ -29,22 +28,31 @@ describe("update", () => {
response.end("not found")
})
const jsonPath = path.join(tmpdir, "tests/updates/update.json")
const settings = new SettingsProvider<UpdateSettings>(jsonPath)
let _settings: SettingsProvider<UpdateSettings> | undefined
const settings = (): SettingsProvider<UpdateSettings> => {
if (!_settings) {
throw new Error("Settings provider has not been created")
}
return _settings
}
let _provider: UpdateProvider | undefined
const provider = (): UpdateProvider => {
if (!_provider) {
const address = server.address()
if (!address || typeof address === "string" || !address.port) {
throw new Error("unexpected address")
}
_provider = new UpdateProvider(`http://${address.address}:${address.port}/latest`, settings)
throw new Error("Update provider has not been created")
}
return _provider
}
beforeAll(async () => {
mockLogger()
const testName = "update"
await clean(testName)
const testDir = await tmpdir(testName)
const jsonPath = path.join(testDir, "update.json")
_settings = new SettingsProvider<UpdateSettings>(jsonPath)
await new Promise((resolve, reject) => {
server.on("error", reject)
server.on("listening", resolve)
@ -53,8 +61,13 @@ describe("update", () => {
host: "localhost",
})
})
await fs.rmdir(path.join(tmpdir, "tests/updates"), { recursive: true })
await fs.mkdir(path.join(tmpdir, "tests/updates"), { recursive: true })
const address = server.address()
if (!address || typeof address === "string" || !address.port) {
throw new Error("unexpected address")
}
_provider = new UpdateProvider(`http://${address.address}:${address.port}/latest`, _settings)
})
afterAll(() => {
@ -72,7 +85,7 @@ describe("update", () => {
const now = Date.now()
const update = await p.getUpdate()
await expect(settings.read()).resolves.toEqual({ update })
await expect(settings().read()).resolves.toEqual({ update })
expect(isNaN(update.checked)).toEqual(false)
expect(update.checked < Date.now() && update.checked >= now).toEqual(true)
expect(update.version).toStrictEqual("2.1.0")
@ -86,7 +99,7 @@ describe("update", () => {
const now = Date.now()
const update = await p.getUpdate()
await expect(settings.read()).resolves.toEqual({ update })
await expect(settings().read()).resolves.toEqual({ update })
expect(isNaN(update.checked)).toStrictEqual(false)
expect(update.checked < now).toBe(true)
expect(update.version).toStrictEqual("2.1.0")
@ -100,7 +113,7 @@ describe("update", () => {
const now = Date.now()
const update = await p.getUpdate(true)
await expect(settings.read()).resolves.toEqual({ update })
await expect(settings().read()).resolves.toEqual({ update })
expect(isNaN(update.checked)).toStrictEqual(false)
expect(update.checked < Date.now() && update.checked >= now).toStrictEqual(true)
expect(update.version).toStrictEqual("4.1.1")
@ -113,12 +126,12 @@ describe("update", () => {
expect(spy).toEqual([])
let checked = Date.now() - 1000 * 60 * 60 * 23
await settings.write({ update: { checked, version } })
await settings().write({ update: { checked, version } })
await p.getUpdate()
expect(spy).toEqual([])
checked = Date.now() - 1000 * 60 * 60 * 25
await settings.write({ update: { checked, version } })
await settings().write({ update: { checked, version } })
const update = await p.getUpdate()
expect(update.checked).not.toStrictEqual(checked)
@ -143,14 +156,14 @@ describe("update", () => {
})
it("should not reject if unable to fetch", async () => {
let provider = new UpdateProvider("invalid", settings)
let provider = new UpdateProvider("invalid", settings())
let now = Date.now()
let update = await provider.getUpdate(true)
expect(isNaN(update.checked)).toStrictEqual(false)
expect(update.checked < Date.now() && update.checked >= now).toEqual(true)
expect(update.version).toStrictEqual("unknown")
provider = new UpdateProvider("http://probably.invalid.dev.localhost/latest", settings)
provider = new UpdateProvider("http://probably.invalid.dev.localhost/latest", settings())
now = Date.now()
update = await provider.getUpdate(true)
expect(isNaN(update.checked)).toStrictEqual(false)