Skip to content

Commit bbdb604

Browse files
committed
refactor: move runCodeServerCommand into sep. file
When Jest runs a test, it loads all the files and imports for that test. This means you might be "requiring" code that's unrelated to your tests. This leads to unexpected errors depending on where the code runs. Moved this file to avoid GLIBC and other errors relaed to argon2 when running integration tests in CI.
1 parent dd86c42 commit bbdb604

File tree

3 files changed

+87
-86
lines changed

3 files changed

+87
-86
lines changed

test/integration/installExtension.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { clean, tmpdir } from "../utils/helpers"
2-
import { runCodeServerCommand } from "../utils/integration"
2+
import { runCodeServerCommand } from "../utils/runCodeServerCommand"
33

44
describe("--install-extension", () => {
55
const testName = "installExtension"

test/utils/integration.ts

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { spawn } from "child_process"
21
import { promises as fs } from "fs"
32
import * as path from "path"
43
import { parse, parseConfigFile, setDefaults } from "../../src/node/cli"
@@ -30,87 +29,3 @@ export async function setup(argv: string[], configFile?: string): Promise<httpse
3029

3130
return new httpserver.HttpServer(server)
3231
}
33-
34-
type RunCodeServerCommandOptions = {
35-
stderr?: "log"
36-
stdout?: "log"
37-
ignoreFail?: boolean
38-
}
39-
interface ErrorWithMoreInfo extends Error {
40-
stderr: string
41-
stdout: string
42-
}
43-
44-
type CSCmd =
45-
| {
46-
code: number | null
47-
signal: NodeJS.Signals | null
48-
stdout: string
49-
stderr: string
50-
}
51-
| ErrorWithMoreInfo
52-
53-
/**
54-
*
55-
* A helper function for integration tests to run code-server commands.
56-
*/
57-
export async function runCodeServerCommand(argv: string[], options: RunCodeServerCommandOptions = {}): Promise<CSCmd> {
58-
const CODE_SERVER_COMMAND = process.env.CODE_SERVER_PATH || "/var/tmp/coder/code-server/bin/code-server"
59-
// Credit: https://github.com/vercel/next.js/blob/canary/test/lib/next-test-utils.js#L139
60-
return new Promise((resolve, reject) => {
61-
console.log(`Running command "${CODE_SERVER_COMMAND} ${argv.join(" ")}"`)
62-
const instance = spawn(CODE_SERVER_COMMAND, [...argv])
63-
let mergedStdio = ""
64-
65-
let stderrOutput = ""
66-
if (options.stderr) {
67-
instance.stderr.on("data", function (chunk) {
68-
mergedStdio += chunk
69-
stderrOutput += chunk
70-
71-
if (options.stderr === "log") {
72-
console.log(chunk.toString())
73-
}
74-
})
75-
} else {
76-
instance.stderr.on("data", function (chunk) {
77-
mergedStdio += chunk
78-
})
79-
}
80-
81-
let stdoutOutput = ""
82-
if (options.stdout) {
83-
instance.stdout.on("data", function (chunk) {
84-
mergedStdio += chunk
85-
stdoutOutput += chunk
86-
87-
if (options.stdout === "log") {
88-
console.log(chunk.toString())
89-
}
90-
})
91-
} else {
92-
instance.stdout.on("data", function (chunk) {
93-
mergedStdio += chunk
94-
})
95-
}
96-
97-
instance.on("close", (code, signal) => {
98-
if (!options.stderr && !options.stdout && !options.ignoreFail && code !== 0) {
99-
return reject(new Error(`command failed with code ${code}\n${mergedStdio}`))
100-
}
101-
102-
resolve({
103-
code,
104-
signal,
105-
stdout: stdoutOutput,
106-
stderr: stderrOutput,
107-
})
108-
})
109-
110-
instance.on("error", (err: ErrorWithMoreInfo) => {
111-
err.stdout = stdoutOutput
112-
err.stderr = stderrOutput
113-
reject(err)
114-
})
115-
})
116-
}

test/utils/runCodeServerCommand.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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
22+
23+
/**
24+
*
25+
* A helper function for integration tests to run code-server commands.
26+
*/
27+
export async function runCodeServerCommand(argv: string[], options: RunCodeServerCommandOptions = {}): Promise<CSCmd> {
28+
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+
})
79+
80+
instance.on("error", (err: ErrorWithMoreInfo) => {
81+
err.stdout = stdoutOutput
82+
err.stderr = stderrOutput
83+
reject(err)
84+
})
85+
})
86+
}

0 commit comments

Comments
 (0)