Skip to content

Commit

Permalink
normalise stdout bound local network addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Nov 10, 2023
1 parent d817598 commit fd9ee9a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 73 deletions.
143 changes: 70 additions & 73 deletions packages/wrangler/e2e/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,64 +479,58 @@ describe("writes debug logs to hidden file", () => {
});

it("writes to file when --log-level = debug", async () => {
await a.runDevSession("--log-level debug", async (session) => {
const normalize = (text: string) =>
normalizeOutput(text, { [session.port]: "<PORT>" });

await waitForPortToBeBound(session.port);
const finalA = await a.runDevSession(
"--log-level debug",
async (session) => {
await waitForPortToBeBound(session.port);

await waitUntil(() => session.stdout.includes("🐛"));

const filepath = session.stdout.match(
/🐛 Writing debug logs to "(.+\.log)"/
)?.[1];
assert(filepath);

expect(existsSync(filepath)).toBe(true);

expect(normalize(session.stdout)).toMatchInlineSnapshot(`
"🐛 Writing debug logs to \\"../../../../../../../../Users/sethi/code/workers-sdk/packages/wrangler/.wrangler/wrangler-debug-<TIMESTAMP>.log\\"
⎔ Starting local server...
[mf:inf] Ready on http://0.0.0.0:<PORT>
[mf:inf] - http://127.0.0.1:<PORT>
[mf:inf] - http://127.0.2.2:<PORT>
[mf:inf] - http://127.0.2.3:<PORT>
[mf:inf] - http://172.16.29.60:<PORT>
[mf:inf] GET / 200 OK (TIMINGS)
"
`);
expect(normalize(session.stderr)).toMatchInlineSnapshot(`
"X [ERROR] workerd/server/server.c++:2984: info: Inspector is listening
X [ERROR] workerd/server/server.c++:1174: info: Inspector client attaching [core:user:a]
"
`);
});
await waitUntil(() => session.stdout.includes("🐛"));
}
);

const filepath = finalA.stdout.match(
/🐛 Writing debug logs to "(.+\.log)"/
)?.[1];
assert(filepath);

expect(existsSync(filepath)).toBe(true);

expect(normalizeOutput(finalA.stdout)).toMatchInlineSnapshot(`
"🐛 Writing debug logs to \\"../../../../../../../../Users/sethi/code/workers-sdk/packages/wrangler/.wrangler/wrangler-debug-<TIMESTAMP>.log\\"
⎔ Starting local server...
[mf:inf] Ready on http://<LOCAL_IP>:<PORT>
[mf:inf] - http://<LOCAL_IP>:<PORT>
[mf:inf] GET / 200 OK (TIMINGS)
"
`);
expect(normalizeOutput(finalA.stderr)).toMatchInlineSnapshot(`
"X [ERROR] workerd/server/server.c++:2984: info: Inspector is listening
X [ERROR] workerd/server/server.c++:1174: info: Inspector client attaching [core:user:a]
"
`);
});

it("does NOT write to file when --log-level != debug", async () => {
await a.runDevSession("", async (session) => {
const finalA = await a.runDevSession("", async (session) => {
const normalize = (text: string) =>
normalizeOutput(text, { [session.port]: "<PORT>" });

await waitForPortToBeBound(session.port);
});

const filepath = session.stdout.match(
/🐛 Writing debug logs to "(.+\.log)"/
)?.[1];
const filepath = finalA.stdout.match(
/🐛 Writing debug logs to "(.+\.log)"/
)?.[1];

expect(filepath).toBeUndefined();
expect(filepath).toBeUndefined();

expect(normalize(session.stdout)).toMatchInlineSnapshot(`
"⎔ Starting local server...
[mf:inf] Ready on http://0.0.0.0:<PORT>
[mf:inf] - http://127.0.0.1:<PORT>
[mf:inf] - http://127.0.2.2:<PORT>
[mf:inf] - http://127.0.2.3:<PORT>
[mf:inf] - http://172.16.29.60:<PORT>
"
`);
expect(normalize(session.stderr)).toMatchInlineSnapshot('""');
});
expect(normalizeOutput(finalA.stdout)).toMatchInlineSnapshot(`
"⎔ Starting local server...
[mf:inf] Ready on http://<LOCAL_IP>:<PORT>
[mf:inf] - http://<LOCAL_IP>:<PORT>
"
`);
expect(normalizeOutput(finalA.stderr)).toMatchInlineSnapshot('""');
});

it("rewrites address-in-use error logs", async () => {
Expand All @@ -549,34 +543,37 @@ describe("writes debug logs to hidden file", () => {
await waitForPortToBeBound(sessionA.port);

// 3. try to start worker B on the same port
await b.runDevSession(`--port ${sessionA.port}`, async (sessionB) => {
// 4. wait until session B emits an "Address in use" error log
await waitUntil(() =>
normalize(sessionB.stderr).includes(
"[ERROR] Address (0.0.0.0:<PORT>) already in use."
)
);
const finalB = await b.runDevSession(
`--port ${sessionA.port}`,
async (sessionB) => {
// 4. wait until session B emits an "Address in use" error log
await waitUntil(() =>
normalize(sessionB.stderr).includes(
"[ERROR] Address (0.0.0.0:<PORT>) already in use."
)
);
}
);

// ensure the workerd error message IS NOT present
expect(normalize(sessionB.stderr)).not.toContain(
"Address already in use; toString() = "
);
// ensure th wrangler (nicer) error message IS present
expect(normalize(sessionB.stderr)).toContain(
"[ERROR] Address (0.0.0.0:<PORT>) already in use."
);
// ensure the workerd error message IS NOT present
expect(normalize(finalB.stderr)).not.toContain(
"Address already in use; toString() = "
);
// ensure th wrangler (nicer) error message IS present
expect(normalize(finalB.stderr)).toContain(
"[ERROR] Address (0.0.0.0:<PORT>) already in use."
);

// snapshot stdout/stderr so we can see what the user sees
expect(normalize(sessionB.stdout)).toMatchInlineSnapshot(`
"⎔ Starting local server...
"
`);
expect(normalize(sessionB.stderr)).toMatchInlineSnapshot(`
"X [ERROR] Address (0.0.0.0:<PORT>) already in use. Please check that you are not already running a server on this address or specify a different port with --port.
X [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
"
`);
});
// snapshot stdout/stderr so we can see what the user sees
expect(normalize(finalB.stdout)).toMatchInlineSnapshot(`
"⎔ Starting local server...
"
`);
expect(normalize(finalB.stderr)).toMatchInlineSnapshot(`
"X [ERROR] Address (0.0.0.0:<PORT>) already in use. Please check that you are not already running a server on this address or specify a different port with --port.
X [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
"
`);
});
});
});
11 changes: 11 additions & 0 deletions packages/wrangler/e2e/helpers/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function normalizeOutput(
stripDevTimings,
stripEmptyNewlines,
normalizeDebugLogFilepath,
squashLocalNetworkBindings,
];
for (const f of functions) {
stdout = f(stdout);
Expand Down Expand Up @@ -142,3 +143,13 @@ export function normalizeDebugLogFilepath(stdout: string): string {
"$1-<TIMESTAMP>.log"
);
}

/**
* Squash the one or more local network bindings from `$ wrangler dev`
*/
export function squashLocalNetworkBindings(stdout: string): string {
return stdout.replace(
/(\[mf:inf\] Ready on http:\/\/.+:\d{4,5})(\n\[mf:inf\] - http:\/\/.+:\d{4,5})+/,
"[mf:inf] Ready on http://<LOCAL_IP>:<PORT>\n[mf:inf] - http://<LOCAL_IP>:<PORT>"
);
}

0 comments on commit fd9ee9a

Please sign in to comment.