feat: migrate state to new database name (#4938)
* Merge setup and navigate functions Whenever we navigate we probably want to make sure the editor is ready so might as well just have one function. * Add customizable entry and workspace directory * Add test for state db migration * Update Code This contains the state migrations.
This commit is contained in:
@ -1,8 +1,45 @@
|
||||
import * as cp from "child_process"
|
||||
import { promises as fs } from "fs"
|
||||
import * as os from "os"
|
||||
import * as path from "path"
|
||||
import * as util from "util"
|
||||
import { describe, test, expect } from "./baseFixture"
|
||||
import { CodeServer } from "./models/CodeServer"
|
||||
|
||||
describe("code-server", true, [], {}, () => {
|
||||
// TODO@asher: Generalize this? Could be nice if we were to ever need
|
||||
// multiple migration tests in other suites.
|
||||
const instances = new Map<string, CodeServer>()
|
||||
test.afterAll(async () => {
|
||||
const procs = Array.from(instances.values())
|
||||
instances.clear()
|
||||
await Promise.all(procs.map((cs) => cs.close()))
|
||||
})
|
||||
|
||||
/**
|
||||
* Spawn a specific version of code-server using the install script.
|
||||
*/
|
||||
const spawn = async (version: string, dir?: string): Promise<CodeServer> => {
|
||||
let instance = instances.get(version)
|
||||
if (!instance) {
|
||||
await util.promisify(cp.exec)(`./install.sh --method standalone --version ${version}`, {
|
||||
cwd: path.join(__dirname, "../.."),
|
||||
})
|
||||
|
||||
instance = new CodeServer(
|
||||
"code-server@" + version,
|
||||
["--auth=none"],
|
||||
{ VSCODE_DEV: "" },
|
||||
dir,
|
||||
`${os.homedir()}/.local/lib/code-server-${version}`,
|
||||
)
|
||||
|
||||
instances.set(version, instance)
|
||||
}
|
||||
|
||||
return instance
|
||||
}
|
||||
|
||||
describe("CodeServer", true, [], {}, () => {
|
||||
test("should navigate to home page", async ({ codeServerPage }) => {
|
||||
// We navigate codeServer before each test
|
||||
// and we start the test with a storage state
|
||||
@ -34,24 +71,50 @@ describe("CodeServer", true, [], {}, () => {
|
||||
await codeServerPage.openFile(file)
|
||||
})
|
||||
|
||||
test("should not share state with other paths", async ({ codeServerPage }) => {
|
||||
test("should migrate state to avoid collisions", async ({ codeServerPage }) => {
|
||||
// This can take a very long time in development because of how long pages
|
||||
// take to load and we are doing a lot of that here.
|
||||
test.slow()
|
||||
|
||||
const dir = await codeServerPage.workspaceDir
|
||||
const file = path.join(dir, "foo")
|
||||
await fs.writeFile(file, "bar")
|
||||
const files = [path.join(dir, "foo"), path.join(dir, "bar")]
|
||||
await Promise.all(
|
||||
files.map((file) => {
|
||||
return fs.writeFile(file, path.basename(file))
|
||||
}),
|
||||
)
|
||||
|
||||
await codeServerPage.openFile(file)
|
||||
// Open a file in the latest instance.
|
||||
await codeServerPage.openFile(files[0])
|
||||
await codeServerPage.stateFlush()
|
||||
|
||||
// If we reload now VS Code will be unable to save the state changes so wait
|
||||
// until those have been written to the database. It flushes every five
|
||||
// seconds so we need to wait at least that long.
|
||||
await codeServerPage.page.waitForTimeout(5500)
|
||||
// Open a file in an older version of code-server. It should not see the
|
||||
// file opened in the new instance since the database has a different
|
||||
// name. This must be accessed through the proxy so it shares the same
|
||||
// domain and can write to the same database.
|
||||
const cs = await spawn("4.0.2", dir)
|
||||
const address = new URL(await cs.address())
|
||||
await codeServerPage.navigate("/proxy/" + address.port + "/")
|
||||
await codeServerPage.openFile(files[1])
|
||||
expect(await codeServerPage.tabIsVisible(files[0])).toBe(false)
|
||||
await codeServerPage.stateFlush()
|
||||
|
||||
// The tab should re-open on refresh.
|
||||
await codeServerPage.page.reload()
|
||||
await codeServerPage.waitForTab(file)
|
||||
// Move back to latest code-server. We should see the file we previously
|
||||
// opened with it but not the old code-server file because the new instance
|
||||
// already created its own database on this path and will avoid migrating.
|
||||
await codeServerPage.navigate()
|
||||
await codeServerPage.waitForTab(files[0])
|
||||
expect(await codeServerPage.tabIsVisible(files[1])).toBe(false)
|
||||
|
||||
// The tab should not re-open on a different path.
|
||||
await codeServerPage.setup(true, "/vscode")
|
||||
expect(await codeServerPage.tabIsVisible(file)).toBe(false)
|
||||
// Open a new path in latest code-server. This one should migrate the
|
||||
// database from old code-server but see nothing from the new database
|
||||
// created on the root.
|
||||
await codeServerPage.navigate("/vscode")
|
||||
await codeServerPage.waitForTab(files[1])
|
||||
expect(await codeServerPage.tabIsVisible(files[0])).toBe(false)
|
||||
// Should still be open after a reload.
|
||||
await codeServerPage.navigate("/vscode")
|
||||
await codeServerPage.waitForTab(files[1])
|
||||
expect(await codeServerPage.tabIsVisible(files[0])).toBe(false)
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user