Archived
1
0

Move onLine to utilities

This way it can be used by the tests when spawning code-server on a
random port to look for the address.
This commit is contained in:
Asher
2021-06-23 15:57:16 -05:00
parent add55ecd62
commit 49c44818d9
3 changed files with 70 additions and 32 deletions

View File

@ -1,3 +1,5 @@
import * as cp from "child_process"
import { generateUuid } from "../../../src/common/util"
import * as util from "../../../src/node/util"
describe("getEnvPaths", () => {
@ -397,3 +399,38 @@ describe("sanitizeString", () => {
expect(util.sanitizeString(" ")).toBe("")
})
})
describe("onLine", () => {
// Spawn a process that outputs anything given on stdin.
let proc: cp.ChildProcess | undefined
beforeAll(() => {
proc = cp.spawn("node", ["-e", 'process.stdin.setEncoding("utf8");process.stdin.on("data", console.log)'])
})
afterAll(() => {
proc?.kill()
})
it("should call with individual lines", async () => {
const size = 100
const received = new Promise<string[]>((resolve) => {
const lines: string[] = []
util.onLine(proc!, (line) => {
lines.push(line)
if (lines.length === size) {
resolve(lines)
}
})
})
const expected: string[] = []
for (let i = 0; i < size; ++i) {
expected.push(generateUuid(i))
}
proc?.stdin?.write(expected.join("\n"))
expect(await received).toEqual(expected)
})
})