Skip to content

Commit 73f043a

Browse files
committed
refactor: use exec instead of spawn
1 parent 0ffc0e5 commit 73f043a

File tree

3 files changed

+7
-80
lines changed

3 files changed

+7
-80
lines changed

test/integration/installExtension.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("--install-extension", () => {
1717
const extName = `wesbos.theme-cobalt2-2.1.6`
1818
const vsixFileName = "wesbos.theme-cobalt2-2.1.6.vsix"
1919
const extensionFixture = path.resolve(`./test/integration/fixtures/${vsixFileName}`)
20-
await runCodeServerCommand([...setupFlags, "--install-extension", extensionFixture], {})
20+
await runCodeServerCommand([...setupFlags, "--install-extension", extensionFixture])
2121
const pathToExtFolder = path.join(tempDir, extName)
2222
const statInfo = await stat(pathToExtFolder)
2323
expect(statInfo.isDirectory()).toBe(true)

test/integration/listExtensions.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("--list-extensions", () => {
2525
await rename(`${tempPathToUnpackedExtension}/extension`, pathToUnpackedExtension)
2626
})
2727
it("should list installed extensions", async () => {
28-
const { stdout } = await runCodeServerCommand([...setupFlags, "--list-extensions"], { stdout: "log" })
28+
const { stdout } = await runCodeServerCommand([...setupFlags, "--list-extensions"])
2929
expect(stdout).toMatch(extName)
3030
}, 20000)
3131
})

test/utils/runCodeServerCommand.ts

+5-78
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,13 @@
1-
import { spawn } from "child_process"
2-
3-
type RunCodeServerCommandOptions = {
4-
stderr?: "log"
5-
stdout?: "log"
6-
ignoreFail?: boolean
7-
}
8-
9-
interface ErrorWithMoreInfo extends Error {
10-
stderr: string
11-
stdout: string
12-
}
13-
14-
type CSCmd =
15-
| {
16-
code: number | null
17-
signal: NodeJS.Signals | null
18-
stdout: string
19-
stderr: string
20-
}
21-
| ErrorWithMoreInfo
1+
import { exec } from "child_process"
2+
import { promisify } from "util"
223

234
/**
245
*
256
* A helper function for integration tests to run code-server commands.
267
*/
27-
export async function runCodeServerCommand(argv: string[], options: RunCodeServerCommandOptions = {}): Promise<CSCmd> {
8+
export async function runCodeServerCommand(argv: string[]): Promise<{ stdout: string; stderr: string }> {
289
const CODE_SERVER_COMMAND = process.env.CODE_SERVER_PATH || "/var/tmp/coder/code-server/bin/code-server"
29-
// Credit: https://github.com/vercel/next.js/blob/canary/test/lib/next-test-utils.js#L139
30-
return new Promise((resolve, reject) => {
31-
console.log(`Running command "${CODE_SERVER_COMMAND} ${argv.join(" ")}"`)
32-
const instance = spawn(CODE_SERVER_COMMAND, [...argv])
33-
let mergedStdio = ""
34-
35-
let stderrOutput = ""
36-
if (options.stderr) {
37-
instance.stderr.on("data", function (chunk) {
38-
mergedStdio += chunk
39-
stderrOutput += chunk
40-
41-
if (options.stderr === "log") {
42-
console.log(chunk.toString())
43-
}
44-
})
45-
} else {
46-
instance.stderr.on("data", function (chunk) {
47-
mergedStdio += chunk
48-
})
49-
}
50-
51-
let stdoutOutput = ""
52-
if (options.stdout) {
53-
instance.stdout.on("data", function (chunk) {
54-
mergedStdio += chunk
55-
stdoutOutput += chunk
56-
57-
if (options.stdout === "log") {
58-
console.log(chunk.toString())
59-
}
60-
})
61-
} else {
62-
instance.stdout.on("data", function (chunk) {
63-
mergedStdio += chunk
64-
})
65-
}
66-
67-
instance.on("close", (code, signal) => {
68-
if (!options.stderr && !options.stdout && !options.ignoreFail && code !== 0) {
69-
return reject(new Error(`command failed with code ${code}\n${mergedStdio}`))
70-
}
71-
72-
resolve({
73-
code,
74-
signal,
75-
stdout: stdoutOutput,
76-
stderr: stderrOutput,
77-
})
78-
})
10+
const { stdout, stderr } = await promisify(exec)(`${CODE_SERVER_COMMAND} ${argv.join(" ")}`)
7911

80-
instance.on("error", (err: ErrorWithMoreInfo) => {
81-
err.stdout = stdoutOutput
82-
err.stderr = stderrOutput
83-
reject(err)
84-
})
85-
})
12+
return { stdout, stderr }
8613
}

0 commit comments

Comments
 (0)