Skip to content

Commit f8528fa

Browse files
committed
Update regex used by e2e to extract address
The address was recently changed to use URL which seems to add a trailing slash when using toString, causing the regex match to fail.
1 parent a343efe commit f8528fa

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

test/e2e/models/CodeServer.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Logger, logger } from "@coder/logger"
1+
import { field, Logger, logger } from "@coder/logger"
22
import * as cp from "child_process"
33
import { promises as fs } from "fs"
44
import * as path from "path"
55
import { Page } from "playwright"
66
import { onLine } from "../../../src/node/util"
77
import { PASSWORD, workspaceDir } from "../../utils/constants"
8-
import { tmpdir } from "../../utils/helpers"
8+
import { idleTimer, tmpdir } from "../../utils/helpers"
99

1010
interface CodeServerProcess {
1111
process: cp.ChildProcess
@@ -99,34 +99,44 @@ export class CodeServer {
9999
},
100100
)
101101

102+
const timer = idleTimer("Failed to extract address; did the format change?", reject)
103+
102104
proc.on("error", (error) => {
103105
this.logger.error(error.message)
106+
timer.dispose()
104107
reject(error)
105108
})
106109

107-
proc.on("close", () => {
110+
proc.on("close", (code) => {
108111
const error = new Error("closed unexpectedly")
109112
if (!this.closed) {
110-
this.logger.error(error.message)
113+
this.logger.error(error.message, field("code", code))
111114
}
115+
timer.dispose()
112116
reject(error)
113117
})
114118

115119
let resolved = false
116120
proc.stdout.setEncoding("utf8")
117121
onLine(proc, (line) => {
122+
// As long as we are actively getting input reset the timer. If we stop
123+
// getting input and still have not found the address the timer will
124+
// reject.
125+
timer.reset()
126+
118127
// Log the line without the timestamp.
119128
this.logger.trace(line.replace(/\[.+\]/, ""))
120129
if (resolved) {
121130
return
122131
}
123-
const match = line.trim().match(/HTTP server listening on (https?:\/\/[.:\d]+)$/)
132+
const match = line.trim().match(/HTTP server listening on (https?:\/\/[.:\d]+)\/?$/)
124133
if (match) {
125134
// Cookies don't seem to work on IP address so swap to localhost.
126135
// TODO: Investigate whether this is a bug with code-server.
127136
const address = match[1].replace("127.0.0.1", "localhost")
128137
this.logger.debug(`spawned on ${address}`)
129138
resolved = true
139+
timer.dispose()
130140
resolve({ process: proc, address })
131141
}
132142
})

test/utils/helpers.ts

+18
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,21 @@ export const getAvailablePort = (options?: net.ListenOptions): Promise<number> =
8282
})
8383
})
8484
})
85+
86+
/**
87+
* Return a timer that will not reject as long as it is disposed or continually
88+
* reset before the delay elapses.
89+
*/
90+
export function idleTimer(message: string, reject: (error: Error) => void, delay = 1000) {
91+
const start = () => setTimeout(() => reject(new Error(message)), delay)
92+
let timeout = start()
93+
return {
94+
reset: () => {
95+
clearTimeout(timeout)
96+
timeout = start()
97+
},
98+
dispose: () => {
99+
clearTimeout(timeout)
100+
},
101+
}
102+
}

0 commit comments

Comments
 (0)