Skip to content

Commit

Permalink
Ensure Wrangler outputs TOML when wrangler.toml is used & JSON when w…
Browse files Browse the repository at this point in the history
…rangler.json is used
  • Loading branch information
penalosa committed Nov 16, 2024
1 parent 7b233ad commit aeab1b9
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 290 deletions.
11 changes: 6 additions & 5 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,11 +1030,12 @@ describe("normalizeAndValidateConfig()", () => {
- \\"unsafe\\" fields are experimental and may change or break at any time.
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (CLASS1), but no [migrations] for them. This may not work as expected until you add a [migrations] section to your wrangler.toml. Add this configuration to your wrangler.toml:
\`\`\`
[[migrations]]
tag = \\"v1\\" # Should be unique for each entry
new_classes = [\\"CLASS1\\"]
\`\`\`
\`\`\`
[[migrations]]
tag = \\"v1\\"
new_classes = [ \\"CLASS1\\" ]
\`\`\`
Refer to https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more details."
`);
Expand Down
57 changes: 29 additions & 28 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5324,13 +5324,13 @@ addEventListener('fetch', event => {});`
}

expect(err?.message.replaceAll(/\d/g, "X")).toMatchInlineSnapshot(`
"A compatibility_date is required when publishing. Add the following to your wrangler.toml file:.
\`\`\`
compatibility_date = \\"XXXX-XX-XX\\"
\`\`\`
Or you could pass it in your terminal as \`--compatibility-date XXXX-XX-XX\`
See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
`);
"A compatibility_date is required when publishing. Add the following to your wrangler.toml file:.
\`\`\`
{\\"compatibility_date\\":\\"XXXX-XX-XX\\"}
\`\`\`
Or you could pass it in your terminal as \`--compatibility-date XXXX-XX-XX\`
See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
`);
});

it("should error if a compatibility_date is missing and suggest the correct month", async () => {
Expand All @@ -5347,13 +5347,13 @@ addEventListener('fetch', event => {});`
}

expect(err?.message).toMatchInlineSnapshot(`
"A compatibility_date is required when publishing. Add the following to your wrangler.toml file:.
\`\`\`
compatibility_date = \\"2020-12-01\\"
\`\`\`
Or you could pass it in your terminal as \`--compatibility-date 2020-12-01\`
See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
`);
"A compatibility_date is required when publishing. Add the following to your wrangler.toml file:.
\`\`\`
{\\"compatibility_date\\":\\"2020-12-01\\"}
\`\`\`
Or you could pass it in your terminal as \`--compatibility-date 2020-12-01\`
See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information."
`);
});

it("should enable the workers.dev domain if workers_dev is undefined and subdomain is not already available", async () => {
Expand Down Expand Up @@ -6073,24 +6073,25 @@ addEventListener('fetch', event => {});`
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mProcessing wrangler.toml configuration:[0m
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mProcessing wrangler.toml configuration:[0m
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (SomeClass),
but no [migrations] for them. This may not work as expected until you add a [migrations] section
to your wrangler.toml. Add this configuration to your wrangler.toml:
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (SomeClass),
but no [migrations] for them. This may not work as expected until you add a [migrations] section
to your wrangler.toml. Add this configuration to your wrangler.toml:
\`\`\`
[[migrations]]
tag = \\"v1\\" # Should be unique for each entry
new_classes = [\\"SomeClass\\"]
\`\`\`
\`\`\`
[[migrations]]
tag = \\"v1\\"
new_classes = [ \\"SomeClass\\" ]
Refer to
https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more
details.
\`\`\`
"
`);
Refer to
https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more
details.
"
`);
});

it("does not warn if all the durable object bindings are to external classes", async () => {
Expand Down
11 changes: 6 additions & 5 deletions packages/wrangler/src/__tests__/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1241,11 +1241,12 @@ describe.sequential("wrangler dev", () => {
CLASS_3), but no [migrations] for them. This may not work as expected until you add a [migrations]
section to your wrangler.toml. Add this configuration to your wrangler.toml:
\`\`\`
[[migrations]]
tag = \\"v1\\" # Should be unique for each entry
new_classes = [\\"CLASS_1\\", \\"CLASS_3\\"]
\`\`\`
\`\`\`
[[migrations]]
tag = \\"v1\\"
new_classes = [ \\"CLASS_1\\", \\"CLASS_3\\" ]
\`\`\`
Refer to
https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more
Expand Down
158 changes: 106 additions & 52 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { writeFileSync } from "node:fs";
import { http, HttpResponse } from "msw";
import dedent from "ts-dedent";
import { endEventLoop } from "./helpers/end-event-loop";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
Expand All @@ -9,6 +10,10 @@ import { mockProcess } from "./helpers/mock-process";
import { msw } from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import {
writeWranglerJson,
writeWranglerToml,
} from "./helpers/write-wrangler-toml";
import type {
KeyValue,
KVNamespaceInfo,
Expand Down Expand Up @@ -190,60 +195,109 @@ describe("wrangler", () => {
`);
});

it("should create a namespace", async () => {
mockCreateRequest("worker-UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
binding = \\"UnitTestNamespace\\"
id = \\"some-namespace-id\\""
`);
});

it("should create a preview namespace if configured to do so", async () => {
mockCreateRequest("worker-UnitTestNamespace_preview");
await runWrangler("kv namespace create UnitTestNamespace --preview");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
binding = \\"UnitTestNamespace\\"
preview_id = \\"some-namespace-id\\""
`);
});
it.each(["toml", "jsonc"])(
"should create a namespace",
async (format) => {
format === "toml"
? writeWranglerToml({ name: "worker" })
: writeWranglerJson({ name: "worker" });

it("should create a namespace using configured worker name", async () => {
writeFileSync("./wrangler.toml", 'name = "other-worker"', "utf-8");
mockCreateRequest("other-worker-UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
[[kv_namespaces]]
binding = \\"UnitTestNamespace\\"
id = \\"some-namespace-id\\""
`);
});
mockCreateRequest("worker-UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toContain(dedent`
🌀 Creating namespace with title "worker-UnitTestNamespace"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
`);
if (format === "toml") {
expect(std.out).toContain("[[kv_namespaces]]");
} else {
expect(std.out).toContain(`"kv_namespaces": [`);
}
}
);

it.each(["toml", "jsonc"])(
"should create a preview namespace if configured to do so",
async (format) => {
format === "toml"
? writeWranglerToml({ name: "worker" })
: writeWranglerJson({ name: "worker" });

mockCreateRequest("worker-UnitTestNamespace_preview");
await runWrangler("kv namespace create UnitTestNamespace --preview");
expect(std.out).toContain(dedent`
🌀 Creating namespace with title "worker-UnitTestNamespace_preview"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
`);
if (format === "toml") {
expect(std.out).toContain("[[kv_namespaces]]");
} else {
expect(std.out).toContain(`"kv_namespaces": [`);
}
}
);

it.each(["toml", "jsonc"])(
"should create a namespace using configured worker name",
async (format) => {
format === "toml"
? writeWranglerToml({ name: "other-worker" })
: writeWranglerJson({ name: "other-worker" });

mockCreateRequest("other-worker-UnitTestNamespace");
await runWrangler("kv namespace create UnitTestNamespace");
expect(std.out).toContain(dedent`
🌀 Creating namespace with title "other-worker-UnitTestNamespace"
✨ Success!
Add the following to your configuration file in your kv_namespaces array:
`);
if (format === "toml") {
expect(std.out).toContain("[[kv_namespaces]]");
} else {
expect(std.out).toContain(`"kv_namespaces": [`);
}
}
);

it.each(["jsonc", "toml"])(
"should create a namespace in an environment if configured to do so",
async (format) => {
format === "toml"
? writeWranglerToml({
name: "worker",
env: {
customEnv: {
name: "worker",
},
},
})
: writeWranglerJson({
name: "worker",
env: {
customEnv: {
name: "worker",
},
},
});

it("should create a namespace in an environment if configured to do so", async () => {
mockCreateRequest("worker-customEnv-UnitTestNamespace");
await runWrangler(
"kv namespace create UnitTestNamespace --env customEnv"
);
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
✨ Success!
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
[[kv_namespaces]]
binding = \\"UnitTestNamespace\\"
id = \\"some-namespace-id\\""
`);
});
mockCreateRequest("worker-customEnv-UnitTestNamespace");
await runWrangler(
"kv namespace create UnitTestNamespace --env customEnv"
);
expect(std.out).toContain(dedent`
🌀 Creating namespace with title "worker-customEnv-UnitTestNamespace"
✨ Success!
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
`);
if (format === "toml") {
expect(std.out).toContain("[[kv_namespaces]]");
} else {
expect(std.out).toContain(`"kv_namespaces": [`);
}
}
);
});

describe("list", () => {
Expand Down
60 changes: 27 additions & 33 deletions packages/wrangler/src/__tests__/queues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { mockConsoleMethods } from "./helpers/mock-console";
import { msw } from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import {
writeWranglerJson,
writeWranglerToml,
} from "./helpers/write-wrangler-toml";
import type { PostTypedConsumerBody, QueueResponse } from "../queues/client";

describe("wrangler", () => {
Expand Down Expand Up @@ -254,24 +258,15 @@ describe("wrangler", () => {
`);
});

it("should create a queue", async () => {
it.each(["toml", "jsonc"])("should create a queue", async (format) => {
format === "toml" ? writeWranglerToml() : writeWranglerJson();
const requests = mockCreateRequest("testQueue");
await runWrangler("queues create testQueue");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating queue 'testQueue'
✅ Created queue 'testQueue'
Configure your Worker to send messages to this queue:
[[queues.producers]]
queue = \\"testQueue\\"
binding = \\"testQueue\\"
Configure your Worker to consume messages from this queue:
[[queues.consumers]]
queue = \\"testQueue\\""
`);
if (format === "toml") {
expect(std.out).toContain("[[queues.producers]]");
} else {
expect(std.out).toContain(`"queues": {`);
}
expect(requests.count).toEqual(1);
});

Expand Down Expand Up @@ -315,26 +310,25 @@ describe("wrangler", () => {
`);
});

it("should send queue settings with delivery delay", async () => {
const requests = mockCreateRequest("testQueue", { delivery_delay: 10 });
await runWrangler("queues create testQueue --delivery-delay-secs=10");
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating queue 'testQueue'
✅ Created queue 'testQueue'
Configure your Worker to send messages to this queue:
it.each(["toml", "jsonc"])(
"should send queue settings with delivery delay",
async (format) => {
format === "toml" ? writeWranglerToml() : writeWranglerJson();

[[queues.producers]]
queue = \\"testQueue\\"
binding = \\"testQueue\\"
const requests = mockCreateRequest("testQueue", {
delivery_delay: 10,
});
await runWrangler("queues create testQueue --delivery-delay-secs=10");

Configure your Worker to consume messages from this queue:
if (format === "toml") {
expect(std.out).toContain("[[queues.producers]]");
} else {
expect(std.out).toContain(`"queues": {`);
}

[[queues.consumers]]
queue = \\"testQueue\\""
`);
expect(requests.count).toEqual(1);
});
expect(requests.count).toEqual(1);
}
);

it("should show an error when two delivery delays are set", async () => {
const requests = mockCreateRequest("testQueue", { delivery_delay: 0 });
Expand Down
Loading

0 comments on commit aeab1b9

Please sign in to comment.