Archived
1
0

refactor: move onLine to test helpers

This commit is contained in:
Joe Previte 2022-05-10 21:54:20 +00:00
parent cec0658e86
commit 32cc27b213
4 changed files with 39 additions and 37 deletions

View File

@ -17,39 +17,7 @@ export interface Paths {
runtime: string
}
// From https://github.com/chalk/ansi-regex
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
].join("|")
const re = new RegExp(pattern, "g")
export type OnLineCallback = (strippedLine: string, originalLine: string) => void
/**
* Split stdout on newlines and strip ANSI codes.
*/
export const onLine = (proc: cp.ChildProcess, callback: OnLineCallback): void => {
let buffer = ""
if (!proc.stdout) {
throw new Error("no stdout")
}
proc.stdout.setEncoding("utf8")
proc.stdout.on("data", (d) => {
const data = buffer + d
const split = data.split("\n")
const last = split.length - 1
for (let i = 0; i < last; ++i) {
callback(split[i].replace(re, ""), split[i])
}
// The last item will either be an empty string (the data ended with a
// newline) or a partial line (did not end with a newline) and we must
// wait to parse it until we get a full line.
buffer = split[last]
})
}
export const paths = getEnvPaths()
/**

View File

@ -5,9 +5,8 @@ import * as path from "path"
import { Page } from "playwright"
import * as util from "util"
import { logError, plural } from "../../../src/common/util"
import { onLine } from "../../../src/node/util"
import { PASSWORD, workspaceDir } from "../../utils/constants"
import { idleTimer, tmpdir } from "../../utils/helpers"
import { idleTimer, onLine, tmpdir } from "../../utils/helpers"
interface CodeServerProcess {
process: cp.ChildProcess
@ -147,7 +146,7 @@ export class CodeServer {
let resolved = false
proc.stdout.setEncoding("utf8")
onLine(proc, (line) => {
onLine(proc, (line: string) => {
// As long as we are actively getting input reset the timer. If we stop
// getting input and still have not found the address the timer will
// reject.

View File

@ -4,6 +4,7 @@ import * as path from "path"
import { generateUuid } from "../../../src/common/util"
import { tmpdir } from "../../../src/node/constants"
import * as util from "../../../src/node/util"
import { onLine } from "../../utils/helpers"
describe("getEnvPaths", () => {
describe("on darwin", () => {
@ -379,7 +380,7 @@ describe("onLine", () => {
const size = 100
const received = new Promise<string[]>((resolve) => {
const lines: string[] = []
util.onLine(proc!, (line) => {
onLine(proc!, (line: string) => {
lines.push(line)
if (lines.length === size) {
resolve(lines)
@ -412,7 +413,7 @@ describe("onLine", () => {
})
const mockCallback = jest.fn()
expect(() => util.onLine(proc, mockCallback)).toThrowError(/stdout/)
expect(() => onLine(proc, mockCallback)).toThrowError(/stdout/)
// Cleanup
proc?.kill()

View File

@ -1,5 +1,6 @@
import { logger } from "@coder/logger"
import { promises as fs } from "fs"
import * as cp from "child_process"
import * as net from "net"
import * as os from "os"
import * as path from "path"
@ -119,3 +120,36 @@ export function isAddressInfo(address: unknown): address is net.AddressInfo {
(address as net.AddressInfo).address !== undefined
)
}
// From https://github.com/chalk/ansi-regex
const pattern = [
"[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)",
"(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))",
].join("|")
const re = new RegExp(pattern, "g")
type OnLineCallback = (strippedLine: string, originalLine: string) => void
/**
* Split stdout on newlines and strip ANSI codes.
*/
export const onLine = (proc: cp.ChildProcess, callback: OnLineCallback): void => {
let buffer = ""
if (!proc.stdout) {
throw new Error("no stdout")
}
proc.stdout.setEncoding("utf8")
proc.stdout.on("data", (d) => {
const data = buffer + d
const split = data.split("\n")
const last = split.length - 1
for (let i = 0; i < last; ++i) {
callback(split[i].replace(re, ""), split[i])
}
// The last item will either be an empty string (the data ended with a
// newline) or a partial line (did not end with a newline) and we must
// wait to parse it until we get a full line.
buffer = split[last]
})
}