Skip to content

Commit 8f1c787

Browse files
committed
add test for redirected config
1 parent 4e23cab commit 8f1c787

File tree

1 file changed

+172
-8
lines changed

1 file changed

+172
-8
lines changed

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

Lines changed: 172 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { rmSync } from "node:fs";
12
import { readFile } from "node:fs/promises";
23
import { http, HttpResponse } from "msw";
34
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
@@ -19,7 +20,10 @@ import { mswListNewDeploymentsLatestFull } from "./helpers/msw/handlers/versions
1920
import { runInTempDir } from "./helpers/run-in-tmp";
2021
import { runWrangler } from "./helpers/run-wrangler";
2122
import { writeWorkerSource } from "./helpers/write-worker-source";
22-
import { writeWranglerConfig } from "./helpers/write-wrangler-config";
23+
import {
24+
writeRedirectedWranglerConfig,
25+
writeWranglerConfig,
26+
} from "./helpers/write-wrangler-config";
2327
import type { DatabaseInfo } from "../d1/types";
2428
import type { Settings } from "../deployment-bundle/bindings";
2529

@@ -198,7 +202,7 @@ describe("--x-provision", () => {
198202
Provisioning R2 (R2 Bucket)...
199203
✨ R2 provisioned 🎉
200204
201-
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard—either way future deploys will continue to work.
205+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
202206
🎉 All resources provisioned, continuing with deployment...
203207
204208
Worker Startup Time: 100 ms
@@ -319,7 +323,7 @@ describe("--x-provision", () => {
319323
Provisioning R2 (R2 Bucket)...
320324
✨ R2 provisioned 🎉
321325
322-
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard—either way future deploys will continue to work.
326+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
323327
🎉 All resources provisioned, continuing with deployment...
324328
325329
Worker Startup Time: 100 ms
@@ -453,7 +457,7 @@ describe("--x-provision", () => {
453457
🌀 Creating new R2 Bucket \\"new-r2\\"...
454458
✨ R2 provisioned 🎉
455459
456-
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard—either way future deploys will continue to work.
460+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
457461
🎉 All resources provisioned, continuing with deployment...
458462
459463
Worker Startup Time: 100 ms
@@ -492,6 +496,166 @@ describe("--x-provision", () => {
492496
`);
493497
});
494498

499+
it("can provision KV, R2 and D1 bindings with new resources w/ redirected config", async () => {
500+
writeRedirectedWranglerConfig({
501+
main: "../index.js",
502+
compatibility_flags: ["nodejs_compat"],
503+
kv_namespaces: [{ binding: "KV" }],
504+
r2_buckets: [{ binding: "R2" }],
505+
d1_databases: [{ binding: "D1" }],
506+
});
507+
mockGetSettings();
508+
mockListKVNamespacesRequest({
509+
title: "test-kv",
510+
id: "existing-kv-id",
511+
});
512+
msw.use(
513+
http.get("*/accounts/:accountId/d1/database", async () => {
514+
return HttpResponse.json(
515+
createFetchResult([
516+
{
517+
name: "db-name",
518+
uuid: "existing-d1-id",
519+
},
520+
])
521+
);
522+
}),
523+
http.get("*/accounts/:accountId/r2/buckets", async () => {
524+
return HttpResponse.json(
525+
createFetchResult({
526+
buckets: [
527+
{
528+
name: "existing-bucket-name",
529+
},
530+
],
531+
})
532+
);
533+
})
534+
);
535+
536+
mockSelect({
537+
text: "Would you like to connect an existing KV Namespace or create a new one?",
538+
result: "__WRANGLER_INTERNAL_NEW",
539+
});
540+
mockPrompt({
541+
text: "Enter a name for your new KV Namespace",
542+
result: "new-kv",
543+
});
544+
mockCreateKVNamespace({
545+
assertTitle: "new-kv",
546+
resultId: "new-kv-id",
547+
});
548+
549+
mockSelect({
550+
text: "Would you like to connect an existing D1 Database or create a new one?",
551+
result: "__WRANGLER_INTERNAL_NEW",
552+
});
553+
mockPrompt({
554+
text: "Enter a name for your new D1 Database",
555+
result: "new-d1",
556+
});
557+
mockCreateD1Database({
558+
assertName: "new-d1",
559+
resultId: "new-d1-id",
560+
});
561+
562+
mockSelect({
563+
text: "Would you like to connect an existing R2 Bucket or create a new one?",
564+
result: "__WRANGLER_INTERNAL_NEW",
565+
});
566+
mockPrompt({
567+
text: "Enter a name for your new R2 Bucket",
568+
result: "new-r2",
569+
});
570+
mockCreateR2Bucket({
571+
assertBucketName: "new-r2",
572+
});
573+
574+
mockUploadWorkerRequest({
575+
expectedBindings: [
576+
{
577+
name: "KV",
578+
type: "kv_namespace",
579+
namespace_id: "new-kv-id",
580+
},
581+
{
582+
name: "R2",
583+
type: "r2_bucket",
584+
bucket_name: "new-r2",
585+
},
586+
{
587+
name: "D1",
588+
type: "d1",
589+
id: "new-d1-id",
590+
},
591+
],
592+
});
593+
594+
await runWrangler("deploy --x-provision --x-auto-create=false");
595+
596+
expect(std.out).toMatchInlineSnapshot(`
597+
"Total Upload: xx KiB / gzip: xx KiB
598+
599+
The following bindings need to be provisioned:
600+
Binding Resource
601+
env.KV KV Namespace
602+
env.D1 D1 Database
603+
env.R2 R2 Bucket
604+
605+
606+
Provisioning KV (KV Namespace)...
607+
🌀 Creating new KV Namespace \\"new-kv\\"...
608+
✨ KV provisioned 🎉
609+
610+
Provisioning D1 (D1 Database)...
611+
🌀 Creating new D1 Database \\"new-d1\\"...
612+
✨ D1 provisioned 🎉
613+
614+
Provisioning R2 (R2 Bucket)...
615+
🌀 Creating new R2 Bucket \\"new-r2\\"...
616+
✨ R2 provisioned 🎉
617+
618+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
619+
🎉 All resources provisioned, continuing with deployment...
620+
621+
Worker Startup Time: 100 ms
622+
Your Worker has access to the following bindings:
623+
Binding Resource
624+
env.KV (new-kv-id) KV Namespace
625+
env.D1 (new-d1-id) D1 Database
626+
env.R2 (new-r2) R2 Bucket
627+
628+
Uploaded test-name (TIMINGS)
629+
Deployed test-name triggers (TIMINGS)
630+
https://test-name.test-sub-domain.workers.dev
631+
Current Version ID: Galaxy-Class"
632+
`);
633+
expect(std.err).toMatchInlineSnapshot(`""`);
634+
expect(std.warn).toMatchInlineSnapshot(`""`);
635+
636+
// IDs should be written back to the user config file
637+
expect(await readFile("wrangler.toml", "utf-8")).toMatchInlineSnapshot(`
638+
"compatibility_date = \\"2022-01-12\\"
639+
name = \\"test-name\\"
640+
main = \\"index.js\\"
641+
642+
[[kv_namespaces]]
643+
binding = \\"KV\\"
644+
id = \\"new-kv-id\\"
645+
646+
[[r2_buckets]]
647+
binding = \\"R2\\"
648+
bucket_name = \\"new-r2\\"
649+
650+
[[d1_databases]]
651+
binding = \\"D1\\"
652+
database_id = \\"new-d1-id\\"
653+
"
654+
`);
655+
656+
rmSync(".wrangler/deploy/config.json");
657+
});
658+
495659
it("can prefill d1 database name from config file if provided", async () => {
496660
writeWranglerConfig({
497661
main: "index.js",
@@ -546,7 +710,7 @@ describe("--x-provision", () => {
546710
🌀 Creating new D1 Database \\"prefilled-d1-name\\"...
547711
✨ D1 provisioned 🎉
548712
549-
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard—either way future deploys will continue to work.
713+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
550714
🎉 All resources provisioned, continuing with deployment...
551715
552716
Worker Startup Time: 100 ms
@@ -675,7 +839,7 @@ describe("--x-provision", () => {
675839
🌀 Creating new D1 Database \\"new-d1-name\\"...
676840
✨ D1 provisioned 🎉
677841
678-
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard—either way future deploys will continue to work.
842+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
679843
🎉 All resources provisioned, continuing with deployment...
680844
681845
Worker Startup Time: 100 ms
@@ -754,7 +918,7 @@ describe("--x-provision", () => {
754918
🌀 Creating new R2 Bucket \\"prefilled-r2-name\\"...
755919
✨ BUCKET provisioned 🎉
756920
757-
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard—either way future deploys will continue to work.
921+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
758922
🎉 All resources provisioned, continuing with deployment...
759923
760924
Worker Startup Time: 100 ms
@@ -950,7 +1114,7 @@ describe("--x-provision", () => {
9501114
🌀 Creating new R2 Bucket \\"existing-bucket-name\\"...
9511115
✨ BUCKET provisioned 🎉
9521116
953-
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard—either way future deploys will continue to work.
1117+
Your Worker was deployed with provisioned resources. We've written the IDs of these resources to your config file, which you can choose to save or discard. Either way future deploys will continue to work.
9541118
🎉 All resources provisioned, continuing with deployment...
9551119
9561120
Worker Startup Time: 100 ms

0 commit comments

Comments
 (0)