Skip to content

Commit 61329eb

Browse files
committed
Ensure Wrangler outputs TOML when wrangler.toml is used & JSON when wrangler.json is used
1 parent a99f30e commit 61329eb

File tree

13 files changed

+390
-290
lines changed

13 files changed

+390
-290
lines changed

packages/wrangler/src/__tests__/configuration.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1030,11 +1030,12 @@ describe("normalizeAndValidateConfig()", () => {
10301030
- \\"unsafe\\" fields are experimental and may change or break at any time.
10311031
- 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:
10321032
1033-
\`\`\`
1034-
[[migrations]]
1035-
tag = \\"v1\\" # Should be unique for each entry
1036-
new_classes = [\\"CLASS1\\"]
1037-
\`\`\`
1033+
\`\`\`
1034+
[[migrations]]
1035+
tag = \\"v1\\"
1036+
new_classes = [ \\"CLASS1\\" ]
1037+
1038+
\`\`\`
10381039
10391040
Refer to https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more details."
10401041
`);

packages/wrangler/src/__tests__/deploy.test.ts

+29-28
Original file line numberDiff line numberDiff line change
@@ -5317,13 +5317,13 @@ addEventListener('fetch', event => {});`
53175317
}
53185318

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

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

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

53525352
it("should enable the workers.dev domain if workers_dev is undefined and subdomain is not already available", async () => {
@@ -6090,24 +6090,25 @@ addEventListener('fetch', event => {});`
60906090
`);
60916091
expect(std.err).toMatchInlineSnapshot(`""`);
60926092
expect(std.warn).toMatchInlineSnapshot(`
6093-
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mProcessing wrangler.toml configuration:[0m
6093+
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mProcessing wrangler.toml configuration:[0m
60946094
6095-
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (SomeClass),
6096-
but no [migrations] for them. This may not work as expected until you add a [migrations] section
6097-
to your wrangler.toml. Add this configuration to your wrangler.toml:
6095+
- In wrangler.toml, you have configured [durable_objects] exported by this Worker (SomeClass),
6096+
but no [migrations] for them. This may not work as expected until you add a [migrations] section
6097+
to your wrangler.toml. Add this configuration to your wrangler.toml:
60986098
6099-
\`\`\`
6100-
[[migrations]]
6101-
tag = \\"v1\\" # Should be unique for each entry
6102-
new_classes = [\\"SomeClass\\"]
6103-
\`\`\`
6099+
\`\`\`
6100+
[[migrations]]
6101+
tag = \\"v1\\"
6102+
new_classes = [ \\"SomeClass\\" ]
61046103
6105-
Refer to
6106-
https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more
6107-
details.
6104+
\`\`\`
61086105
6109-
"
6110-
`);
6106+
Refer to
6107+
https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more
6108+
details.
6109+
6110+
"
6111+
`);
61116112
});
61126113

61136114
it("does not warn if all the durable object bindings are to external classes", async () => {

packages/wrangler/src/__tests__/dev.test.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1241,11 +1241,12 @@ describe.sequential("wrangler dev", () => {
12411241
CLASS_3), but no [migrations] for them. This may not work as expected until you add a [migrations]
12421242
section to your wrangler.toml. Add this configuration to your wrangler.toml:
12431243
1244-
\`\`\`
1245-
[[migrations]]
1246-
tag = \\"v1\\" # Should be unique for each entry
1247-
new_classes = [\\"CLASS_1\\", \\"CLASS_3\\"]
1248-
\`\`\`
1244+
\`\`\`
1245+
[[migrations]]
1246+
tag = \\"v1\\"
1247+
new_classes = [ \\"CLASS_1\\", \\"CLASS_3\\" ]
1248+
1249+
\`\`\`
12491250
12501251
Refer to
12511252
https://developers.cloudflare.com/durable-objects/reference/durable-objects-migrations/ for more

packages/wrangler/src/__tests__/kv.test.ts

+106-52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { writeFileSync } from "node:fs";
22
import { http, HttpResponse } from "msw";
3+
import dedent from "ts-dedent";
34
import { endEventLoop } from "./helpers/end-event-loop";
45
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
56
import { mockConsoleMethods } from "./helpers/mock-console";
@@ -9,6 +10,10 @@ import { mockProcess } from "./helpers/mock-process";
910
import { msw } from "./helpers/msw";
1011
import { runInTempDir } from "./helpers/run-in-tmp";
1112
import { runWrangler } from "./helpers/run-wrangler";
13+
import {
14+
writeWranglerJson,
15+
writeWranglerToml,
16+
} from "./helpers/write-wrangler-toml";
1217
import type {
1318
KeyValue,
1419
KVNamespaceInfo,
@@ -190,60 +195,109 @@ describe("wrangler", () => {
190195
`);
191196
});
192197

193-
it("should create a namespace", async () => {
194-
mockCreateRequest("worker-UnitTestNamespace");
195-
await runWrangler("kv namespace create UnitTestNamespace");
196-
expect(std.out).toMatchInlineSnapshot(`
197-
"🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
198-
✨ Success!
199-
Add the following to your configuration file in your kv_namespaces array:
200-
[[kv_namespaces]]
201-
binding = \\"UnitTestNamespace\\"
202-
id = \\"some-namespace-id\\""
203-
`);
204-
});
205-
206-
it("should create a preview namespace if configured to do so", async () => {
207-
mockCreateRequest("worker-UnitTestNamespace_preview");
208-
await runWrangler("kv namespace create UnitTestNamespace --preview");
209-
expect(std.out).toMatchInlineSnapshot(`
210-
"🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
211-
✨ Success!
212-
Add the following to your configuration file in your kv_namespaces array:
213-
[[kv_namespaces]]
214-
binding = \\"UnitTestNamespace\\"
215-
preview_id = \\"some-namespace-id\\""
216-
`);
217-
});
198+
it.each(["toml", "jsonc"])(
199+
"should create a namespace",
200+
async (format) => {
201+
format === "toml"
202+
? writeWranglerToml({ name: "worker" })
203+
: writeWranglerJson({ name: "worker" });
218204

219-
it("should create a namespace using configured worker name", async () => {
220-
writeFileSync("./wrangler.toml", 'name = "other-worker"', "utf-8");
221-
mockCreateRequest("other-worker-UnitTestNamespace");
222-
await runWrangler("kv namespace create UnitTestNamespace");
223-
expect(std.out).toMatchInlineSnapshot(`
224-
"🌀 Creating namespace with title \\"other-worker-UnitTestNamespace\\"
225-
✨ Success!
226-
Add the following to your configuration file in your kv_namespaces array:
227-
[[kv_namespaces]]
228-
binding = \\"UnitTestNamespace\\"
229-
id = \\"some-namespace-id\\""
230-
`);
231-
});
205+
mockCreateRequest("worker-UnitTestNamespace");
206+
await runWrangler("kv namespace create UnitTestNamespace");
207+
expect(std.out).toContain(dedent`
208+
🌀 Creating namespace with title "worker-UnitTestNamespace"
209+
✨ Success!
210+
Add the following to your configuration file in your kv_namespaces array:
211+
`);
212+
if (format === "toml") {
213+
expect(std.out).toContain("[[kv_namespaces]]");
214+
} else {
215+
expect(std.out).toContain(`"kv_namespaces": [`);
216+
}
217+
}
218+
);
219+
220+
it.each(["toml", "jsonc"])(
221+
"should create a preview namespace if configured to do so",
222+
async (format) => {
223+
format === "toml"
224+
? writeWranglerToml({ name: "worker" })
225+
: writeWranglerJson({ name: "worker" });
226+
227+
mockCreateRequest("worker-UnitTestNamespace_preview");
228+
await runWrangler("kv namespace create UnitTestNamespace --preview");
229+
expect(std.out).toContain(dedent`
230+
🌀 Creating namespace with title "worker-UnitTestNamespace_preview"
231+
✨ Success!
232+
Add the following to your configuration file in your kv_namespaces array:
233+
`);
234+
if (format === "toml") {
235+
expect(std.out).toContain("[[kv_namespaces]]");
236+
} else {
237+
expect(std.out).toContain(`"kv_namespaces": [`);
238+
}
239+
}
240+
);
241+
242+
it.each(["toml", "jsonc"])(
243+
"should create a namespace using configured worker name",
244+
async (format) => {
245+
format === "toml"
246+
? writeWranglerToml({ name: "other-worker" })
247+
: writeWranglerJson({ name: "other-worker" });
248+
249+
mockCreateRequest("other-worker-UnitTestNamespace");
250+
await runWrangler("kv namespace create UnitTestNamespace");
251+
expect(std.out).toContain(dedent`
252+
🌀 Creating namespace with title "other-worker-UnitTestNamespace"
253+
✨ Success!
254+
Add the following to your configuration file in your kv_namespaces array:
255+
`);
256+
if (format === "toml") {
257+
expect(std.out).toContain("[[kv_namespaces]]");
258+
} else {
259+
expect(std.out).toContain(`"kv_namespaces": [`);
260+
}
261+
}
262+
);
263+
264+
it.each(["jsonc", "toml"])(
265+
"should create a namespace in an environment if configured to do so",
266+
async (format) => {
267+
format === "toml"
268+
? writeWranglerToml({
269+
name: "worker",
270+
env: {
271+
customEnv: {
272+
name: "worker",
273+
},
274+
},
275+
})
276+
: writeWranglerJson({
277+
name: "worker",
278+
env: {
279+
customEnv: {
280+
name: "worker",
281+
},
282+
},
283+
});
232284

233-
it("should create a namespace in an environment if configured to do so", async () => {
234-
mockCreateRequest("worker-customEnv-UnitTestNamespace");
235-
await runWrangler(
236-
"kv namespace create UnitTestNamespace --env customEnv"
237-
);
238-
expect(std.out).toMatchInlineSnapshot(`
239-
"🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
240-
✨ Success!
241-
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
242-
[[kv_namespaces]]
243-
binding = \\"UnitTestNamespace\\"
244-
id = \\"some-namespace-id\\""
245-
`);
246-
});
285+
mockCreateRequest("worker-customEnv-UnitTestNamespace");
286+
await runWrangler(
287+
"kv namespace create UnitTestNamespace --env customEnv"
288+
);
289+
expect(std.out).toContain(dedent`
290+
🌀 Creating namespace with title "worker-customEnv-UnitTestNamespace"
291+
✨ Success!
292+
Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
293+
`);
294+
if (format === "toml") {
295+
expect(std.out).toContain("[[kv_namespaces]]");
296+
} else {
297+
expect(std.out).toContain(`"kv_namespaces": [`);
298+
}
299+
}
300+
);
247301
});
248302

249303
describe("list", () => {

packages/wrangler/src/__tests__/queues.test.ts

+27-33
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import { mockConsoleMethods } from "./helpers/mock-console";
44
import { msw } from "./helpers/msw";
55
import { runInTempDir } from "./helpers/run-in-tmp";
66
import { runWrangler } from "./helpers/run-wrangler";
7+
import {
8+
writeWranglerJson,
9+
writeWranglerToml,
10+
} from "./helpers/write-wrangler-toml";
711
import type { PostTypedConsumerBody, QueueResponse } from "../queues/client";
812

913
describe("wrangler", () => {
@@ -254,24 +258,15 @@ describe("wrangler", () => {
254258
`);
255259
});
256260

257-
it("should create a queue", async () => {
261+
it.each(["toml", "jsonc"])("should create a queue", async (format) => {
262+
format === "toml" ? writeWranglerToml() : writeWranglerJson();
258263
const requests = mockCreateRequest("testQueue");
259264
await runWrangler("queues create testQueue");
260-
expect(std.out).toMatchInlineSnapshot(`
261-
"🌀 Creating queue 'testQueue'
262-
✅ Created queue 'testQueue'
263-
264-
Configure your Worker to send messages to this queue:
265-
266-
[[queues.producers]]
267-
queue = \\"testQueue\\"
268-
binding = \\"testQueue\\"
269-
270-
Configure your Worker to consume messages from this queue:
271-
272-
[[queues.consumers]]
273-
queue = \\"testQueue\\""
274-
`);
265+
if (format === "toml") {
266+
expect(std.out).toContain("[[queues.producers]]");
267+
} else {
268+
expect(std.out).toContain(`"queues": {`);
269+
}
275270
expect(requests.count).toEqual(1);
276271
});
277272

@@ -315,26 +310,25 @@ describe("wrangler", () => {
315310
`);
316311
});
317312

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

327-
[[queues.producers]]
328-
queue = \\"testQueue\\"
329-
binding = \\"testQueue\\"
318+
const requests = mockCreateRequest("testQueue", {
319+
delivery_delay: 10,
320+
});
321+
await runWrangler("queues create testQueue --delivery-delay-secs=10");
330322

331-
Configure your Worker to consume messages from this queue:
323+
if (format === "toml") {
324+
expect(std.out).toContain("[[queues.producers]]");
325+
} else {
326+
expect(std.out).toContain(`"queues": {`);
327+
}
332328

333-
[[queues.consumers]]
334-
queue = \\"testQueue\\""
335-
`);
336-
expect(requests.count).toEqual(1);
337-
});
329+
expect(requests.count).toEqual(1);
330+
}
331+
);
338332

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

0 commit comments

Comments
 (0)