Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nodejs compat v2 upgrade #6824

Merged
merged 15 commits into from
Sep 25, 2024
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
87cd2c0
fix: tidy up error messaging for unexpected use of Node.js APIs
petebacondarwin Sep 25, 2024
7b8f838
fix: teach Wrangler about node_compat version date switch
petebacondarwin Sep 25, 2024
dcce811
refactor: move the node-compat parsing from Wrangler into Miniflare
petebacondarwin Sep 25, 2024
141e5f9
fix: teach Miniflare about node_compat version date switch
petebacondarwin Sep 25, 2024
7c97b9e
test: update Node.js hybrid fixture to use nodejs_compat + compat date
petebacondarwin Sep 25, 2024
2727d1e
fixup! fix: teach Miniflare about node_compat version date switch
petebacondarwin Sep 25, 2024
2222dd0
fixup! refactor: move the node-compat parsing from Wrangler into Mini…
petebacondarwin Sep 25, 2024
bb848ca
fixup! fix: tidy up error messaging for unexpected use of Node.js APIs
petebacondarwin Sep 25, 2024
75be39c
fixup! fix: tidy up error messaging for unexpected use of Node.js APIs
petebacondarwin Sep 25, 2024
77c7af4
fixup! refactor: move the node-compat parsing from Wrangler into Mini…
petebacondarwin Sep 25, 2024
ccde13a
fixup! fix: tidy up error messaging for unexpected use of Node.js APIs
petebacondarwin Sep 25, 2024
8445cbc
fixup! refactor: move the node-compat parsing from Wrangler into Mini…
petebacondarwin Sep 25, 2024
3e62c2a
fix: also handle the case with `no_nodejs_compat_v2`
petebacondarwin Sep 25, 2024
8ae707f
test: normalize output strings for tests
petebacondarwin Sep 25, 2024
7db433a
refactor: rename `getNodejsCompatMode()` to `getNodejsCompat()`
petebacondarwin Sep 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: normalize output strings for tests
  • Loading branch information
petebacondarwin committed Sep 25, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 8ae707f30f24bbd41a7c09b5acdd66f081a0a24e
12 changes: 6 additions & 6 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from "node:path";
import { readConfig } from "../config";
import { normalizeAndValidateConfig } from "../config/validation";
import { normalizeSlashes } from "./helpers/mock-console";
import { normalizeString } from "./helpers/normalize";
import { runInTempDir } from "./helpers/run-in-tmp";
import { writeWranglerToml } from "./helpers/write-wrangler-toml";
import type {
@@ -301,7 +301,7 @@ describe("normalizeAndValidateConfig()", () => {
expect(diagnostics.hasErrors()).toBe(false);
expect(diagnostics.hasWarnings()).toBe(true);

expect(normalizeSlashes(diagnostics.renderWarnings()))
expect(normalizeString(diagnostics.renderWarnings()))
.toMatchInlineSnapshot(`
"Processing wrangler configuration:
- Deprecation: \\"site.entry-point\\":
@@ -332,7 +332,7 @@ describe("normalizeAndValidateConfig()", () => {
expect(diagnostics.hasWarnings()).toBe(true);
expect(diagnostics.hasErrors()).toBe(true);

expect(normalizeSlashes(diagnostics.renderWarnings()))
expect(normalizeString(diagnostics.renderWarnings()))
.toMatchInlineSnapshot(`
"Processing wrangler configuration:
- Deprecation: \\"site.entry-point\\":
@@ -375,7 +375,7 @@ describe("normalizeAndValidateConfig()", () => {
- Expected \\"site.entry-point\\" to be of type string but got 111."
`);

expect(normalizeSlashes(diagnostics.renderWarnings()))
expect(normalizeString(diagnostics.renderWarnings()))
.toMatchInlineSnapshot(`
"Processing wrangler configuration:
- Deprecation: \\"site.entry-point\\":
@@ -409,7 +409,7 @@ describe("normalizeAndValidateConfig()", () => {
expect(diagnostics.hasWarnings()).toBe(true);
expect(diagnostics.hasErrors()).toBe(false);

expect(normalizeSlashes(diagnostics.renderWarnings()))
expect(normalizeString(diagnostics.renderWarnings()))
.toMatchInlineSnapshot(`
"Processing wrangler configuration:
- Deprecation: \\"site.entry-point\\":
@@ -881,7 +881,7 @@ describe("normalizeAndValidateConfig()", () => {
expect(diagnostics.hasErrors()).toBe(false);
expect(diagnostics.hasWarnings()).toBe(true);

expect(normalizeSlashes(diagnostics.renderWarnings()))
expect(normalizeString(diagnostics.renderWarnings()))
.toMatchInlineSnapshot(`
"Processing project/wrangler.toml configuration:
- \\"unsafe\\" fields are experimental and may change or break at any time.
79 changes: 43 additions & 36 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
@@ -18,11 +18,7 @@ import { logger } from "../logger";
import { writeAuthConfigFile } from "../user";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockAuthDomain } from "./helpers/mock-auth-domain";
import {
mockConsoleMethods,
normalizeSlashes,
normalizeTempDirs,
} from "./helpers/mock-console";
import { mockConsoleMethods } from "./helpers/mock-console";
import { clearDialogs, mockConfirm } from "./helpers/mock-dialogs";
import { mockGetZoneFromHostRequest } from "./helpers/mock-get-zone-from-host";
import { useMockIsTTY } from "./helpers/mock-istty";
@@ -45,6 +41,7 @@ import {
mswSuccessUserHandlers,
} from "./helpers/msw";
import { mswListNewDeploymentsLatestFull } from "./helpers/msw/handlers/versions";
import { normalizeString } from "./helpers/normalize";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import { writeWorkerSource } from "./helpers/write-worker-source";
@@ -2053,7 +2050,7 @@ addEventListener('fetch', event => {});`

"
`);
expect(normalizeSlashes(std.warn)).toMatchInlineSnapshot(`
expect(normalizeString(std.warn)).toMatchInlineSnapshot(`
"▲ [WARNING] Processing wrangler.toml configuration:

- Because you've defined a [site] configuration, we're defaulting to \\"workers-site\\" for the
@@ -2169,7 +2166,7 @@ addEventListener('fetch', event => {});`
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(normalizeSlashes(std.warn)).toMatchInlineSnapshot(`
expect(normalizeString(std.warn)).toMatchInlineSnapshot(`
"▲ [WARNING] Processing my-site/wrangler.toml configuration:

- Deprecation: \\"site.entry-point\\":
@@ -5679,8 +5676,8 @@ addEventListener('fetch', event => {});`
mockUploadWorkerRequest();
await runWrangler("build");

const outFile = normalizeSlashes(
normalizeTempDirs(fs.readFileSync("dist/index.js", "utf-8"))
const outFile = normalizeString(
fs.readFileSync("dist/index.js", "utf-8")
);

// We don't check against the whole file as there is middleware being injected
@@ -5715,8 +5712,8 @@ addEventListener('fetch', event => {});`
mockUploadWorkerRequest();
await runWrangler("build --env staging");

const outFile = normalizeSlashes(
normalizeTempDirs(fs.readFileSync("dist/index.js", "utf-8"))
const outFile = normalizeString(
fs.readFileSync("dist/index.js", "utf-8")
);

// We don't check against the whole file as there is middleware being injected
@@ -9262,13 +9259,15 @@ export default{

await expect(
runWrangler("deploy index.js --dry-run").catch((e) =>
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
normalizeString(
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
)
)
).resolves.toMatchInlineSnapshot(`
" [ERROR] Could not resolve \\"path\\"
"X [ERROR] Could not resolve \\"path\\"

index.js:1:17:
1 │ import path from 'path';
@@ -9287,13 +9286,15 @@ export default{

await expect(
runWrangler("deploy index.js --dry-run").catch((e) =>
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
normalizeString(
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
)
)
).resolves.toMatchInlineSnapshot(`
" [ERROR] Could not resolve \\"path\\"
"X [ERROR] Could not resolve \\"path\\"

index.js:1:17:
1 │ import path from 'path';
@@ -9312,13 +9313,15 @@ export default{

await expect(
runWrangler("deploy index.js --dry-run").catch((e) =>
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
normalizeString(
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
)
)
).resolves.toMatchInlineSnapshot(`
" [ERROR] Could not resolve \\"diagnostics_channel\\"
"X [ERROR] Could not resolve \\"diagnostics_channel\\"

index.js:1:15:
1 │ import fs from 'diagnostics_channel';
@@ -9338,13 +9341,15 @@ export default{

await expect(
runWrangler("deploy index.js --dry-run").catch((e) =>
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
normalizeString(
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
)
)
).resolves.toMatchInlineSnapshot(`
" [ERROR] Could not resolve \\"path\\"
"X [ERROR] Could not resolve \\"path\\"

index.js:1:15:
1 │ import fs from 'path';
@@ -9364,13 +9369,15 @@ export default{

await expect(
runWrangler("deploy index.js --dry-run").catch((e) =>
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
normalizeString(
esbuild
.formatMessagesSync(e?.errors ?? [], { kind: "error" })
.join()
.trim()
)
)
).resolves.toMatchInlineSnapshot(`
" [ERROR] Could not resolve \\"path\\"
"X [ERROR] Could not resolve \\"path\\"

index.js:1:15:
1 │ import fs from 'path';
55 changes: 2 additions & 53 deletions packages/wrangler/src/__tests__/helpers/mock-console.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as util from "node:util";
import { afterEach, beforeEach, vi } from "vitest";
import { logger } from "../../logger";
import { normalizeString } from "./normalize";
import type { MockInstance } from "vitest";

/**
@@ -33,13 +34,7 @@ const std = {
};

function normalizeOutput(spy: MockInstance): string {
return normalizeErrorMarkers(
replaceByte(
stripTrailingWhitespace(
normalizeSlashes(normalizeTempDirs(stripTimings(captureCalls(spy))))
)
)
);
return normalizeString(captureCalls(spy));
}

function captureCalls(spy: MockInstance): string {
@@ -66,49 +61,3 @@ export function mockConsoleMethods() {
});
return std;
}

/**
* Normalize error `X` markers.
*
* Windows gets a different character.
*/
function normalizeErrorMarkers(str: string): string {
return str.replaceAll("✘", "X");
}

/**
* Ensure slashes in the `str` are OS file-system agnostic.
*
* Use this in snapshot tests to be resilient to file-system differences.
*/
export function normalizeSlashes(str: string): string {
return str.replace(/\\/g, "/");
}

/**
* Strip "timing data" out of the `stdout` string, since this is not always deterministic.
*
* Use this in snapshot tests to be resilient to slight changes in timing of processing.
*/
export function stripTimings(stdout: string): string {
return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
}

export function stripTrailingWhitespace(str: string): string {
return str.replace(/[^\S\n]+\n/g, "\n");
}

/**
* Removing leading kilobit (tenth of a byte) from test output due to
* variation causing every few tests the value to change by ± .01
*/
function replaceByte(stdout: string): string {
return stdout.replaceAll(/\d+\.\d+ KiB/g, "xx KiB");
}

/**
* Temp directories are created with random names, so we replace all comments temp dirs in them
*/
export function normalizeTempDirs(stdout: string): string {
return stdout.replaceAll(/\/\/.+\/tmp.+/g, "//tmpdir");
}
58 changes: 58 additions & 0 deletions packages/wrangler/src/__tests__/helpers/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Normalize the input string, to make it reliable to use in tests.
*/
export function normalizeString(input: string): string {
return normalizeErrorMarkers(
replaceByte(
stripTrailingWhitespace(
normalizeSlashes(normalizeTempDirs(stripTimings(input)))
)
)
);
}

/**
* Normalize error `X` markers.
*
* Windows gets a different character.
*/
function normalizeErrorMarkers(str: string): string {
return str.replaceAll("✘", "X");
}

/**
* Ensure slashes in the `str` are OS file-system agnostic.
*
* Use this in snapshot tests to be resilient to file-system differences.
*/
function normalizeSlashes(str: string): string {
return str.replace(/\\/g, "/");
}

/**
* Strip "timing data" out of the `stdout` string, since this is not always deterministic.
*
* Use this in snapshot tests to be resilient to slight changes in timing of processing.
*/
function stripTimings(stdout: string): string {
return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
}

function stripTrailingWhitespace(str: string): string {
return str.replace(/[^\S\n]+\n/g, "\n");
}

/**
* Removing leading kilobit (tenth of a byte) from test output due to
* variation causing every few tests the value to change by ± .01
*/
function replaceByte(stdout: string): string {
return stdout.replaceAll(/\d+\.\d+ KiB/g, "xx KiB");
}

/**
* Temp directories are created with random names, so we replace all comments temp dirs in them
*/
function normalizeTempDirs(stdout: string): string {
return stdout.replaceAll(/\/\/.+\/tmp.+/g, "//tmpdir");
}
4 changes: 2 additions & 2 deletions packages/wrangler/src/__tests__/helpers/run-wrangler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { main } from "../../index";
import * as shellquote from "../../utils/shell-quote";
import { normalizeSlashes, stripTimings } from "./mock-console";
import { normalizeString } from "./normalize";

/**
* A helper to 'run' wrangler commands for tests.
@@ -16,7 +16,7 @@ export async function runWrangler(
await main(argv);
} catch (err) {
if (err instanceof Error) {
err.message = normalizeSlashes(stripTimings(err.message));
err.message = normalizeString(err.message);
}
throw err;
} finally {
Loading