Skip to content

Commit

Permalink
fix: ensure kv:key list matches the output from Wrangler 1
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
petebacondarwin committed Jan 24, 2022
1 parent b9aa6e3 commit 0289882
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 22 deletions.
16 changes: 16 additions & 0 deletions .changeset/four-tips-yell.md
Original file line number Diff line number Diff line change
@@ -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.
116 changes: 96 additions & 20 deletions packages/wrangler/src/__tests__/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
[],
[],
Expand All @@ -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\\": {}
}
]"
`);
});

Expand All @@ -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\\": {}
}
]"
`);
});

Expand All @@ -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\\": {}
}
]"
`);
});

Expand All @@ -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\\": {}
}
]"
`);
});

Expand All @@ -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\\": {}
}
]"
`);
});

Expand All @@ -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);
});

Expand Down
7 changes: 5 additions & 2 deletions packages/wrangler/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1551,9 +1551,12 @@ export async function main(argv: string[]): Promise<void> {
}
// -- 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));
}
}
)
Expand Down

0 comments on commit 0289882

Please sign in to comment.