From 0289882a15eba55d802650a591f999ef7b614fb6 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Wed, 19 Jan 2022 20:08:41 +0000 Subject: [PATCH] fix: ensure `kv:key list` matches the output from Wrangler 1 The previous output was passing an array of objects to console.log, which ended up showing something like ``` [Object object] [Object object] ... ``` Now the result is JSON stringified before being sent to the console. The tests have been fixed to check this too. --- .changeset/four-tips-yell.md | 16 +++ packages/wrangler/src/__tests__/kv.test.ts | 116 +++++++++++++++++---- packages/wrangler/src/index.tsx | 7 +- 3 files changed, 117 insertions(+), 22 deletions(-) create mode 100644 .changeset/four-tips-yell.md diff --git a/.changeset/four-tips-yell.md b/.changeset/four-tips-yell.md new file mode 100644 index 000000000000..eabd143c43dc --- /dev/null +++ b/.changeset/four-tips-yell.md @@ -0,0 +1,16 @@ +--- +"wrangler": patch +--- + +fix: ensure `kv:key list` matches the output from Wrangler 1 + +The previous output was passing an array of objects to console.log, which ended up showing something like + +``` +[Object object] +[Object object] +... +``` + +Now the result is JSON stringified before being sent to the console. +The tests have been fixed to check this too. diff --git a/packages/wrangler/src/__tests__/kv.test.ts b/packages/wrangler/src/__tests__/kv.test.ts index 16c2e92ce055..b52866204a92 100644 --- a/packages/wrangler/src/__tests__/kv.test.ts +++ b/packages/wrangler/src/__tests__/kv.test.ts @@ -687,20 +687,26 @@ describe("wrangler", () => { keysPerRequest = 1000 ) { const requests = { count: 0 }; + // See https://api.cloudflare.com/#workers-kv-namespace-list-a-namespace-s-keys + const expectedKeyObjects = expectedKeys.map((name) => ({ + name, + expiration: 123456789, + metadata: {}, + })); setMockRawResponse( "/accounts/:accountId/storage/kv/namespaces/:namespaceId/keys", ([_url, accountId, namespaceId], _init, query) => { requests.count++; expect(accountId).toEqual("some-account-id"); expect(namespaceId).toEqual(expectedNamespaceId); - if (expectedKeys.length <= keysPerRequest) { - return createFetchResult(expectedKeys); + if (expectedKeyObjects.length <= keysPerRequest) { + return createFetchResult(expectedKeyObjects); } else { const start = parseInt(query.get("cursor") ?? "0") || 0; const end = start + keysPerRequest; - const cursor = end < expectedKeys.length ? end : undefined; + const cursor = end < expectedKeyObjects.length ? end : undefined; return createFetchResult( - expectedKeys.slice(start, end), + expectedKeyObjects.slice(start, end), true, [], [], @@ -721,9 +727,23 @@ describe("wrangler", () => { expect(error).toMatchInlineSnapshot(`undefined`); expect(stderr).toMatchInlineSnapshot(`""`); expect(stdout).toMatchInlineSnapshot(` - "key-1 - key-2 - key-3" + "[ + { + \\"name\\": \\"key-1\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-2\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-3\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + } + ]" `); }); @@ -737,9 +757,23 @@ describe("wrangler", () => { expect(error).toMatchInlineSnapshot(`undefined`); expect(stderr).toMatchInlineSnapshot(`""`); expect(stdout).toMatchInlineSnapshot(` - "key-1 - key-2 - key-3" + "[ + { + \\"name\\": \\"key-1\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-2\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-3\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + } + ]" `); }); @@ -753,9 +787,23 @@ describe("wrangler", () => { expect(error).toMatchInlineSnapshot(`undefined`); expect(stderr).toMatchInlineSnapshot(`""`); expect(stdout).toMatchInlineSnapshot(` - "key-1 - key-2 - key-3" + "[ + { + \\"name\\": \\"key-1\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-2\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-3\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + } + ]" `); }); @@ -769,9 +817,23 @@ describe("wrangler", () => { expect(error).toMatchInlineSnapshot(`undefined`); expect(stderr).toMatchInlineSnapshot(`""`); expect(stdout).toMatchInlineSnapshot(` - "key-1 - key-2 - key-3" + "[ + { + \\"name\\": \\"key-1\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-2\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-3\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + } + ]" `); }); @@ -785,9 +847,23 @@ describe("wrangler", () => { expect(error).toMatchInlineSnapshot(`undefined`); expect(stderr).toMatchInlineSnapshot(`""`); expect(stdout).toMatchInlineSnapshot(` - "key-1 - key-2 - key-3" + "[ + { + \\"name\\": \\"key-1\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-2\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + }, + { + \\"name\\": \\"key-3\\", + \\"expiration\\": 123456789, + \\"metadata\\": {} + } + ]" `); }); @@ -804,7 +880,7 @@ describe("wrangler", () => { ); expect(error).toMatchInlineSnapshot(`undefined`); expect(stderr).toMatchInlineSnapshot(`""`); - expect(stdout).toEqual(keys.join("\n")); + expect(JSON.parse(stdout).map((k) => k.name)).toEqual(keys); expect(requests.count).toEqual(6); }); diff --git a/packages/wrangler/src/index.tsx b/packages/wrangler/src/index.tsx index 7b68ef780980..a6b60cdb81bf 100644 --- a/packages/wrangler/src/index.tsx +++ b/packages/wrangler/src/index.tsx @@ -1551,9 +1551,12 @@ export async function main(argv: string[]): Promise { } // -- snip, end -- - console.log( - await listNamespaceKeys(config.account_id, namespaceId, prefix) + const results = await listNamespaceKeys( + config.account_id, + namespaceId, + prefix ); + console.log(JSON.stringify(results, undefined, 2)); } } )