diff --git a/packages/wrangler/src/__tests__/patch-config.test.ts b/packages/wrangler/src/__tests__/patch-config.test.ts index c1f43a60396c..91972e604f58 100644 --- a/packages/wrangler/src/__tests__/patch-config.test.ts +++ b/packages/wrangler/src/__tests__/patch-config.test.ts @@ -361,6 +361,73 @@ const replacingOnlyTestCases: Omit[] = [ } `, }, + { + name: "delete an existing binding so that none are left", + original: { + kv_namespaces: [ + { + binding: "KV", + }, + ], + }, + replacingPatch: { + kv_namespaces: undefined, + }, + expectedToml: dedent` + compatibility_date = "2022-01-12" + name = "test-name" + + `, + expectedJson: dedent` + { + "compatibility_date": "2022-01-12", + "name": "test-name" + } + `, + }, + // This one doesn't work because the jsonc-parser leaves behind a stray bracket when deleting + // there are possibly solutions that I am not inclined to solve right now + // e.g. passing in {binding: undefined} in the patch instead and cleaning up empty objects + // { + // name: "delete an existing binding (but some bindings of that type are still left)", + // original: { + // kv_namespaces: [ + // { + // binding: "KV", + // }, + // { + // binding: "KV2", + // }, + // ], + // }, + // replacingPatch: { + // kv_namespaces: [ + // { + // binding: "KV", + // }, + // undefined, + // ], + // }, + // expectedToml: dedent` + // compatibility_date = "2022-01-12" + // name = "test-name" + + // [[kv_namespaces]] + // binding = "KV" + + // `, + // expectedJson: dedent` + // { + // "compatibility_date": "2022-01-12", + // "name": "test-name", + // "kv_namespaces": [ + // { + // "binding": "KV" + // } + // ] + // } + // `, + // }, { name: "edit a compat flag", original: {}, @@ -410,7 +477,7 @@ const replacingOnlyTestCases: Omit[] = [ name: "delete a compat flag", original: {}, replacingPatch: { - compatibility_flags: [], + compatibility_flags: undefined, }, expectedToml: dedent` compatibility_date = "2022-01-12" @@ -698,5 +765,116 @@ describe("experimental_patchConfig()", () => { `); }); }); + + describe("edit existing bindings with patch array in a different order (will mess up comments)", () => { + it("isArrayInsertion = false", () => { + const jsonc = ` + { + // comment one + "compatibility_date": "2022-01-12", + // comment two + "name": "test-name", + "kv_namespaces": [ + { + // comment three + "binding": "KV" + // comment four + }, + { + // comment five + "binding": "KV2" + // comment six + } + ] + } + `; + writeFileSync("./wrangler.jsonc", jsonc); + const patch = { + compatibility_date: "2024-27-09", + kv_namespaces: [ + { + binding: "KV2", + }, + { + binding: "KV", + id: "hello-id", + }, + ], + }; + const result = experimental_patchConfig( + "./wrangler.jsonc", + patch, + false + ); + expect(result).not.toBeFalsy(); + // Note that the comments have stayed in place! + // However, I don't think we can reasonably expect to bring comments along when an array has been reordered + expect(result).toMatchInlineSnapshot(` + "{ + // comment one + \\"compatibility_date\\": \\"2024-27-09\\", + // comment two + \\"name\\": \\"test-name\\", + \\"kv_namespaces\\": [ + { + // comment three + \\"binding\\": \\"KV2\\" + // comment four + }, + { + // comment five + \\"binding\\": \\"KV\\", + \\"id\\": \\"hello-id\\" + // comment six + } + ] + }" + `); + }); + }); + + describe("delete existing bindings (cannot preserve comments)", () => { + it("isArrayInsertion = false", () => { + const jsonc = ` + { + // comment one + "compatibility_date": "2022-01-12", + // comment two + "name": "test-name", + "kv_namespaces": [ + { + // comment three + "binding": "KV" + // comment four + }, + { + // comment five + "binding": "KV2" + // comment six + } + ] + } + `; + writeFileSync("./wrangler.jsonc", jsonc); + const patch = { + compatibility_date: "2024-27-09", + kv_namespaces: undefined, + }; + const result = experimental_patchConfig( + "./wrangler.jsonc", + patch, + false + ); + expect(result).not.toBeFalsy(); + expect(result).toMatchInlineSnapshot(` + "{ + // comment one + \\"compatibility_date\\": \\"2024-27-09\\", + // comment two + \\"name\\": \\"test-name\\" + }" + `); + }); + }); }); });