From a882be574887367b378658535d2414e09e591ad5 Mon Sep 17 00:00:00 2001 From: Asher Date: Wed, 5 May 2021 12:20:38 -0500 Subject: [PATCH] Refactor integration tests to use main entry point --- src/node/entry.ts | 3 ++- src/node/main.ts | 4 +++- test/unit/health.test.ts | 4 ++-- test/unit/proxy.test.ts | 10 +++++----- test/utils/integration.ts | 16 +++++----------- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/node/entry.ts b/src/node/entry.ts index 24a557e1c..4b186fefd 100644 --- a/src/node/entry.ts +++ b/src/node/entry.ts @@ -24,7 +24,8 @@ async function entry(): Promise { if (isChild(wrapper)) { const args = await wrapper.handshake() wrapper.preventExit() - return runCodeServer(args) + await runCodeServer(args) + return } const cliArgs = parse(process.argv.slice(2)) diff --git a/src/node/main.ts b/src/node/main.ts index 1409a4409..e00364135 100644 --- a/src/node/main.ts +++ b/src/node/main.ts @@ -82,7 +82,7 @@ export const openInExistingInstance = async (args: DefaultedArgs, socketPath: st vscode.end() } -export const runCodeServer = async (args: DefaultedArgs): Promise => { +export const runCodeServer = async (args: DefaultedArgs): Promise => { logger.info(`code-server ${version} ${commit}`) logger.info(`Using user-data-dir ${humanPath(args["user-data-dir"])}`) @@ -154,4 +154,6 @@ export const runCodeServer = async (args: DefaultedArgs): Promise => { logger.error("Failed to open", field("address", openAddress), field("error", error)) } } + + return server } diff --git a/test/unit/health.test.ts b/test/unit/health.test.ts index a8bdfb195..2c5fc0b75 100644 --- a/test/unit/health.test.ts +++ b/test/unit/health.test.ts @@ -12,7 +12,7 @@ describe("health", () => { }) it("/healthz", async () => { - ;[, , codeServer] = await integration.setup(["--auth=none"], "") + codeServer = await integration.setup(["--auth=none"], "") const resp = await codeServer.fetch("/healthz") expect(resp.status).toBe(200) const json = await resp.json() @@ -20,7 +20,7 @@ describe("health", () => { }) it("/healthz (websocket)", async () => { - ;[, , codeServer] = await integration.setup(["--auth=none"], "") + codeServer = await integration.setup(["--auth=none"], "") const ws = codeServer.ws("/healthz") const message = await new Promise((resolve, reject) => { ws.on("error", console.error) diff --git a/test/unit/proxy.test.ts b/test/unit/proxy.test.ts index c5969ebfd..a9c1554e6 100644 --- a/test/unit/proxy.test.ts +++ b/test/unit/proxy.test.ts @@ -37,7 +37,7 @@ describe("proxy", () => { e.get("/wsup", (req, res) => { res.json("asher is the best") }) - ;[, , codeServer] = await integration.setup(["--auth=none"], "") + codeServer = await integration.setup(["--auth=none"], "") const resp = await codeServer.fetch(proxyPath) expect(resp.status).toBe(200) const json = await resp.json() @@ -48,7 +48,7 @@ describe("proxy", () => { e.get(absProxyPath, (req, res) => { res.json("joe is the best") }) - ;[, , codeServer] = await integration.setup(["--auth=none"], "") + codeServer = await integration.setup(["--auth=none"], "") const resp = await codeServer.fetch(absProxyPath) expect(resp.status).toBe(200) const json = await resp.json() @@ -62,7 +62,7 @@ describe("proxy", () => { e.post("/finale", (req, res) => { res.json("redirect success") }) - ;[, , codeServer] = await integration.setup(["--auth=none"], "") + codeServer = await integration.setup(["--auth=none"], "") const resp = await codeServer.fetch(proxyPath, { method: "POST", }) @@ -78,7 +78,7 @@ describe("proxy", () => { e.post(finalePath, (req, res) => { res.json("redirect success") }) - ;[, , codeServer] = await integration.setup(["--auth=none"], "") + codeServer = await integration.setup(["--auth=none"], "") const resp = await codeServer.fetch(absProxyPath, { method: "POST", }) @@ -91,7 +91,7 @@ describe("proxy", () => { e.post("/wsup", (req, res) => { res.json(req.body) }) - ;[, , codeServer] = await integration.setup(["--auth=none"], "") + codeServer = await integration.setup(["--auth=none"], "") const resp = await codeServer.fetch(proxyPath, { method: "post", body: JSON.stringify("coder is the best"), diff --git a/test/utils/integration.ts b/test/utils/integration.ts index c5101d0fb..1ad7b44d9 100644 --- a/test/utils/integration.ts +++ b/test/utils/integration.ts @@ -1,21 +1,15 @@ -import * as express from "express" -import { createApp } from "../../src/node/app" -import { parse, setDefaults, parseConfigFile, DefaultedArgs } from "../../src/node/cli" -import { register } from "../../src/node/routes" +import { parse, parseConfigFile, setDefaults } from "../../src/node/cli" +import { runCodeServer } from "../../src/node/main" import * as httpserver from "./httpserver" -export async function setup( - argv: string[], - configFile?: string, -): Promise<[express.Application, express.Application, httpserver.HttpServer, DefaultedArgs]> { +export async function setup(argv: string[], configFile?: string): Promise { argv = ["--bind-addr=localhost:0", ...argv] const cliArgs = parse(argv) const configArgs = parseConfigFile(configFile || "", "test/integration.ts") const args = await setDefaults(cliArgs, configArgs) - const [app, wsApp, server] = await createApp(args) - await register(app, wsApp, server, args) + const server = await runCodeServer(args) - return [app, wsApp, new httpserver.HttpServer(server), args] + return new httpserver.HttpServer(server) }