Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 01a4285

Browse files
committedMay 10, 2022
Revert "refactor: move onLine to test helpers"
This reverts commit 32cc27b.
1 parent 32cc27b commit 01a4285

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed
 

‎src/node/util.ts

+32
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,39 @@ 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+
2027
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+
2153
export const paths = getEnvPaths()
2254

2355
/**

‎test/e2e/models/CodeServer.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ 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"
89
import { PASSWORD, workspaceDir } from "../../utils/constants"
9-
import { idleTimer, onLine, tmpdir } from "../../utils/helpers"
10+
import { idleTimer, tmpdir } from "../../utils/helpers"
1011

1112
interface CodeServerProcess {
1213
process: cp.ChildProcess
@@ -146,7 +147,7 @@ export class CodeServer {
146147

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

‎test/unit/node/util.test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ 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"
87

98
describe("getEnvPaths", () => {
109
describe("on darwin", () => {
@@ -380,7 +379,7 @@ describe("onLine", () => {
380379
const size = 100
381380
const received = new Promise<string[]>((resolve) => {
382381
const lines: string[] = []
383-
onLine(proc!, (line: string) => {
382+
util.onLine(proc!, (line) => {
384383
lines.push(line)
385384
if (lines.length === size) {
386385
resolve(lines)
@@ -413,7 +412,7 @@ describe("onLine", () => {
413412
})
414413
const mockCallback = jest.fn()
415414

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

418417
// Cleanup
419418
proc?.kill()

‎test/utils/helpers.ts

-34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { logger } from "@coder/logger"
22
import { promises as fs } from "fs"
3-
import * as cp from "child_process"
43
import * as net from "net"
54
import * as os from "os"
65
import * as path from "path"
@@ -120,36 +119,3 @@ export function isAddressInfo(address: unknown): address is net.AddressInfo {
120119
(address as net.AddressInfo).address !== undefined
121120
)
122121
}
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)
Please sign in to comment.