Skip to content

Commit 32cc27b

Browse files
committed
refactor: move onLine to test helpers
1 parent cec0658 commit 32cc27b

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

src/node/util.ts

-32
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,7 @@ export interface Paths {
1717
runtime: string
1818
}
1919

20-
// From https://github.com/chalk/ansi-regex
21-
const pattern = [
22-
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
23-
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
24-
].join("|")
25-
const re = new RegExp(pattern, "g")
26-
2720
export type OnLineCallback = (strippedLine: string, originalLine: string) => void
28-
/**
29-
* Split stdout on newlines and strip ANSI codes.
30-
*/
31-
export const onLine = (proc: cp.ChildProcess, callback: OnLineCallback): void => {
32-
let buffer = ""
33-
if (!proc.stdout) {
34-
throw new Error("no stdout")
35-
}
36-
proc.stdout.setEncoding("utf8")
37-
proc.stdout.on("data", (d) => {
38-
const data = buffer + d
39-
const split = data.split("\n")
40-
const last = split.length - 1
41-
42-
for (let i = 0; i < last; ++i) {
43-
callback(split[i].replace(re, ""), split[i])
44-
}
45-
46-
// The last item will either be an empty string (the data ended with a
47-
// newline) or a partial line (did not end with a newline) and we must
48-
// wait to parse it until we get a full line.
49-
buffer = split[last]
50-
})
51-
}
52-
5321
export const paths = getEnvPaths()
5422

5523
/**

test/e2e/models/CodeServer.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import * as path from "path"
55
import { Page } from "playwright"
66
import * as util from "util"
77
import { logError, plural } from "../../../src/common/util"
8-
import { onLine } from "../../../src/node/util"
98
import { PASSWORD, workspaceDir } from "../../utils/constants"
10-
import { idleTimer, tmpdir } from "../../utils/helpers"
9+
import { idleTimer, onLine, tmpdir } from "../../utils/helpers"
1110

1211
interface CodeServerProcess {
1312
process: cp.ChildProcess
@@ -147,7 +146,7 @@ export class CodeServer {
147146

148147
let resolved = false
149148
proc.stdout.setEncoding("utf8")
150-
onLine(proc, (line) => {
149+
onLine(proc, (line: string) => {
151150
// As long as we are actively getting input reset the timer. If we stop
152151
// getting input and still have not found the address the timer will
153152
// reject.

test/unit/node/util.test.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as path from "path"
44
import { generateUuid } from "../../../src/common/util"
55
import { tmpdir } from "../../../src/node/constants"
66
import * as util from "../../../src/node/util"
7+
import { onLine } from "../../utils/helpers"
78

89
describe("getEnvPaths", () => {
910
describe("on darwin", () => {
@@ -379,7 +380,7 @@ describe("onLine", () => {
379380
const size = 100
380381
const received = new Promise<string[]>((resolve) => {
381382
const lines: string[] = []
382-
util.onLine(proc!, (line) => {
383+
onLine(proc!, (line: string) => {
383384
lines.push(line)
384385
if (lines.length === size) {
385386
resolve(lines)
@@ -412,7 +413,7 @@ describe("onLine", () => {
412413
})
413414
const mockCallback = jest.fn()
414415

415-
expect(() => util.onLine(proc, mockCallback)).toThrowError(/stdout/)
416+
expect(() => onLine(proc, mockCallback)).toThrowError(/stdout/)
416417

417418
// Cleanup
418419
proc?.kill()

test/utils/helpers.ts

+34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from "@coder/logger"
22
import { promises as fs } from "fs"
3+
import * as cp from "child_process"
34
import * as net from "net"
45
import * as os from "os"
56
import * as path from "path"
@@ -119,3 +120,36 @@ export function isAddressInfo(address: unknown): address is net.AddressInfo {
119120
(address as net.AddressInfo).address !== undefined
120121
)
121122
}
123+
124+
// From https://github.com/chalk/ansi-regex
125+
const pattern = [
126+
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
127+
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
128+
].join("|")
129+
const re = new RegExp(pattern, "g")
130+
131+
type OnLineCallback = (strippedLine: string, originalLine: string) => void
132+
/**
133+
* Split stdout on newlines and strip ANSI codes.
134+
*/
135+
export const onLine = (proc: cp.ChildProcess, callback: OnLineCallback): void => {
136+
let buffer = ""
137+
if (!proc.stdout) {
138+
throw new Error("no stdout")
139+
}
140+
proc.stdout.setEncoding("utf8")
141+
proc.stdout.on("data", (d) => {
142+
const data = buffer + d
143+
const split = data.split("\n")
144+
const last = split.length - 1
145+
146+
for (let i = 0; i < last; ++i) {
147+
callback(split[i].replace(re, ""), split[i])
148+
}
149+
150+
// The last item will either be an empty string (the data ended with a
151+
// newline) or a partial line (did not end with a newline) and we must
152+
// wait to parse it until we get a full line.
153+
buffer = split[last]
154+
})
155+
}

0 commit comments

Comments
 (0)