Skip to content

Commit f9e7145

Browse files
chore: include banner in output of tests for commands with json/pretty mode (#8109)
* tail * d1 * fixups * Update packages/wrangler/src/__tests__/d1/list.test.ts Co-authored-by: Carmen Popoviciu <[email protected]> * Update packages/wrangler/src/__tests__/d1/info.test.ts Co-authored-by: Carmen Popoviciu <[email protected]> * Update packages/wrangler/src/__tests__/d1/list.test.ts Co-authored-by: Carmen Popoviciu <[email protected]> * Apply suggestions from code review Co-authored-by: Carmen Popoviciu <[email protected]> --------- Co-authored-by: Carmen Popoviciu <[email protected]>
1 parent 86ab0ca commit f9e7145

File tree

6 files changed

+468
-85
lines changed

6 files changed

+468
-85
lines changed

packages/wrangler/src/__tests__/d1/execute.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import { runInTempDir } from "../helpers/run-in-tmp";
66
import { runWrangler } from "../helpers/run-wrangler";
77
import { writeWranglerConfig } from "../helpers/write-wrangler-config";
88

9+
// we want to include the banner to make sure it doesn't show up in the output
10+
// when --json=true
11+
vi.unmock("../../wrangler-banner");
12+
913
describe("execute", () => {
1014
const std = mockConsoleMethods();
1115
runInTempDir();
@@ -137,6 +141,29 @@ describe("execute", () => {
137141
)
138142
);
139143
});
144+
145+
it("should show banner by default", async () => {
146+
setIsTTY(false);
147+
writeWranglerConfig({
148+
d1_databases: [
149+
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
150+
],
151+
});
152+
153+
await runWrangler("d1 execute db --command 'select 1;'");
154+
expect(std.out).toContain("⛅️ wrangler x.x.x");
155+
});
156+
it("should not show banner if --json=true", async () => {
157+
setIsTTY(false);
158+
writeWranglerConfig({
159+
d1_databases: [
160+
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
161+
],
162+
});
163+
164+
await runWrangler("d1 execute db --command 'select 1;' --json");
165+
expect(std.out).not.toContain("⛅️ wrangler x.x.x");
166+
});
140167
});
141168

142169
function useSentry() {

packages/wrangler/src/__tests__/d1/info.test.ts

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import { runInTempDir } from "../helpers/run-in-tmp";
88
import { runWrangler } from "../helpers/run-wrangler";
99
import { writeWranglerConfig } from "../helpers/write-wrangler-config";
1010

11+
// we want to include the banner to make sure it doesn't show up in the output when
12+
// when --json=true
13+
vi.unmock("../../wrangler-banner");
1114
describe("info", () => {
1215
mockAccountId({ accountId: null });
1316
mockApiToken();
@@ -16,7 +19,7 @@ describe("info", () => {
1619
const std = mockConsoleMethods();
1720
const { setIsTTY } = useMockIsTTY();
1821

19-
it("should display version when alpha", async () => {
22+
beforeEach(() => {
2023
setIsTTY(false);
2124
mockGetMemberships([
2225
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
@@ -30,6 +33,8 @@ describe("info", () => {
3033
},
3134
],
3235
});
36+
});
37+
it("should display version when alpha", async () => {
3338
msw.use(
3439
http.get("*/accounts/:accountId/d1/database/*", async () => {
3540
return HttpResponse.json(
@@ -66,19 +71,6 @@ describe("info", () => {
6671
});
6772

6873
it("should not display version when not alpha", async () => {
69-
setIsTTY(false);
70-
mockGetMemberships([
71-
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
72-
]);
73-
writeWranglerConfig({
74-
d1_databases: [
75-
{
76-
binding: "DB",
77-
database_name: "northwind",
78-
database_id: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
79-
},
80-
],
81-
});
8274
msw.use(
8375
http.get("*/accounts/:accountId/d1/database/*", async () => {
8476
return HttpResponse.json(
@@ -129,4 +121,70 @@ describe("info", () => {
129121
}"
130122
`);
131123
});
124+
125+
it("should pretty print by default, incl. the wrangler banner", async () => {
126+
msw.use(
127+
http.get("*/accounts/:accountId/d1/database/*", async () => {
128+
return HttpResponse.json(
129+
{
130+
result: {
131+
uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
132+
name: "northwind",
133+
created_at: "2023-05-23T08:33:54.590Z",
134+
version: "beta",
135+
num_tables: 13,
136+
file_size: 33067008,
137+
running_in_region: "WEUR",
138+
},
139+
success: true,
140+
errors: [],
141+
messages: [],
142+
},
143+
{ status: 200 }
144+
);
145+
})
146+
);
147+
msw.use(
148+
http.post("*/graphql", async () => {
149+
return HttpResponse.json(
150+
{
151+
result: null,
152+
success: true,
153+
errors: [],
154+
messages: [],
155+
},
156+
{ status: 200 }
157+
);
158+
})
159+
);
160+
// pretty print by default
161+
await runWrangler("d1 info northwind");
162+
expect(std.out).toMatchInlineSnapshot(`
163+
"
164+
⛅️ wrangler x.x.x
165+
------------------
166+
167+
┌───────────────────┬──────────────────────────────────────┐
168+
│ DB │ d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06 │
169+
├───────────────────┼──────────────────────────────────────┤
170+
│ name │ northwind │
171+
├───────────────────┼──────────────────────────────────────┤
172+
│ created_at │ 2023-05-23T08:33:54.590Z │
173+
├───────────────────┼──────────────────────────────────────┤
174+
│ num_tables │ 13 │
175+
├───────────────────┼──────────────────────────────────────┤
176+
│ running_in_region │ WEUR │
177+
├───────────────────┼──────────────────────────────────────┤
178+
│ database_size │ 33.1 MB │
179+
├───────────────────┼──────────────────────────────────────┤
180+
│ read_queries_24h │ 0 │
181+
├───────────────────┼──────────────────────────────────────┤
182+
│ write_queries_24h │ 0 │
183+
├───────────────────┼──────────────────────────────────────┤
184+
│ rows_read_24h │ 0 │
185+
├───────────────────┼──────────────────────────────────────┤
186+
│ rows_written_24h │ 0 │
187+
└───────────────────┴──────────────────────────────────────┘"
188+
`);
189+
});
132190
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { http, HttpResponse } from "msw";
2+
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
3+
import { mockConsoleMethods } from "../helpers/mock-console";
4+
import { useMockIsTTY } from "../helpers/mock-istty";
5+
import { mockGetMemberships } from "../helpers/mock-oauth-flow";
6+
import { msw } from "../helpers/msw";
7+
import { runInTempDir } from "../helpers/run-in-tmp";
8+
import { runWrangler } from "../helpers/run-wrangler";
9+
10+
// we want to include the banner to make sure it doesn't show up in the output when
11+
// when --json=true
12+
vi.unmock("../../wrangler-banner");
13+
describe("list", () => {
14+
mockAccountId({ accountId: null });
15+
mockApiToken();
16+
mockConsoleMethods();
17+
runInTempDir();
18+
const std = mockConsoleMethods();
19+
const { setIsTTY } = useMockIsTTY();
20+
21+
beforeEach(() => {
22+
setIsTTY(false);
23+
mockGetMemberships([
24+
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
25+
]);
26+
msw.use(
27+
http.get("*/accounts/:accountId/d1/database", async () => {
28+
return HttpResponse.json(
29+
{
30+
result: [
31+
{
32+
uuid: "1",
33+
name: "a",
34+
binding: "A",
35+
},
36+
{
37+
uuid: "2",
38+
name: "b",
39+
binding: "B",
40+
},
41+
],
42+
success: true,
43+
errors: [],
44+
messages: [],
45+
},
46+
{ status: 200 }
47+
);
48+
})
49+
);
50+
});
51+
it("should print as json if `--json` flag is specified, without wrangler banner", async () => {
52+
await runWrangler("d1 list --json");
53+
expect(std.out).toMatchInlineSnapshot(`
54+
"[
55+
{
56+
\\"uuid\\": \\"1\\",
57+
\\"name\\": \\"a\\",
58+
\\"binding\\": \\"A\\"
59+
},
60+
{
61+
\\"uuid\\": \\"2\\",
62+
\\"name\\": \\"b\\",
63+
\\"binding\\": \\"B\\"
64+
}
65+
]"
66+
`);
67+
});
68+
69+
it("should pretty print by default, including the wrangler banner", async () => {
70+
await runWrangler("d1 list");
71+
expect(std.out).toMatchInlineSnapshot(`
72+
"
73+
⛅️ wrangler x.x.x
74+
------------------
75+
76+
┌──────┬──────┬─────────┐
77+
│ uuid │ name │ binding │
78+
├──────┼──────┼─────────┤
79+
│ 1 │ a │ A │
80+
├──────┼──────┼─────────┤
81+
│ 2 │ b │ B │
82+
└──────┴──────┴─────────┘"
83+
`);
84+
});
85+
});

packages/wrangler/src/__tests__/d1/timeTravel.test.ts

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import { runInTempDir } from "../helpers/run-in-tmp";
99
import { runWrangler } from "../helpers/run-wrangler";
1010
import { writeWranglerConfig } from "../helpers/write-wrangler-config";
1111

12+
// we want to include the banner to make sure it doesn't show up in the output when
13+
// when --json=true
14+
vi.unmock("../../wrangler-banner");
15+
1216
describe("time-travel", () => {
13-
mockConsoleMethods();
17+
const std = mockConsoleMethods();
1418
mockAccountId({ accountId: null });
1519
mockApiToken();
1620
runInTempDir();
@@ -110,4 +114,129 @@ describe("time-travel", () => {
110114
expect(result).toBeUndefined();
111115
});
112116
});
117+
118+
describe("--json", () => {
119+
beforeEach(() => {
120+
setIsTTY(false);
121+
writeWranglerConfig({
122+
d1_databases: [
123+
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
124+
],
125+
});
126+
mockGetMemberships([
127+
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
128+
]);
129+
msw.use(
130+
http.get("*/accounts/:accountId/d1/database/*", async () => {
131+
return HttpResponse.json(
132+
{
133+
result: {
134+
uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
135+
name: "db",
136+
created_at: "2023-05-23T08:33:54.590Z",
137+
version: "beta",
138+
num_tables: 13,
139+
file_size: 33067008,
140+
running_in_region: "WEUR",
141+
},
142+
success: true,
143+
errors: [],
144+
messages: [],
145+
},
146+
{ status: 200 }
147+
);
148+
}),
149+
http.post(
150+
"*/accounts/:accountId/d1/database/*/time_travel/restore",
151+
async () => {
152+
return HttpResponse.json(
153+
{
154+
result: {
155+
bookmark: "a",
156+
},
157+
success: true,
158+
errors: [],
159+
messages: [],
160+
},
161+
{ status: 200 }
162+
);
163+
}
164+
)
165+
);
166+
vi.useFakeTimers();
167+
vi.setSystemTime(new Date("2011-10-05T14:48:00.000Z"));
168+
});
169+
afterEach(() => {
170+
vi.useRealTimers();
171+
});
172+
describe("restore", () => {
173+
it("should print as json, without wrangler banner", async () => {
174+
await runWrangler(
175+
`d1 time-travel restore db --timestamp=2011-09-05T14:48:00.000Z --json`
176+
);
177+
expect(std.out).toMatchInlineSnapshot(`
178+
"{
179+
\\"bookmark\\": \\"a\\"
180+
}"
181+
`);
182+
});
183+
184+
it("should pretty print by default", async () => {
185+
await runWrangler(
186+
`d1 time-travel restore db --timestamp=2011-09-05T14:48:00.000Z"`
187+
);
188+
expect(std.out).toMatchInlineSnapshot(`
189+
"
190+
⛅️ wrangler x.x.x
191+
------------------
192+
193+
🚧 Restoring database db from bookmark undefined
194+
195+
⚠️ This will overwrite all data in database db.
196+
In-flight queries and transactions will be cancelled.
197+
198+
? OK to proceed (y/N)
199+
🤖 Using fallback value in non-interactive context: yes
200+
⚡️ Time travel in progress...
201+
✅ Database db restored back to bookmark a
202+
203+
↩️ To undo this operation, you can restore to the previous bookmark: undefined"
204+
`);
205+
});
206+
});
207+
describe("info", () => {
208+
it("should print as json, without wrangler banner", async () => {
209+
await runWrangler(
210+
`d1 time-travel info db --timestamp=2011-09-05T14:48:00.000Z --json`
211+
);
212+
expect(std.out).toMatchInlineSnapshot(`
213+
"{
214+
\\"uuid\\": \\"d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06\\",
215+
\\"name\\": \\"db\\",
216+
\\"created_at\\": \\"2023-05-23T08:33:54.590Z\\",
217+
\\"version\\": \\"beta\\",
218+
\\"num_tables\\": 13,
219+
\\"file_size\\": 33067008,
220+
\\"running_in_region\\": \\"WEUR\\"
221+
}"
222+
`);
223+
});
224+
it("should pretty print by default", async () => {
225+
await runWrangler(
226+
`d1 time-travel info db --timestamp=2011-09-05T14:48:00.000Z"`
227+
);
228+
expect(std.out).toMatchInlineSnapshot(`
229+
"
230+
⛅️ wrangler x.x.x
231+
------------------
232+
233+
🚧 Time Traveling...
234+
⚠️ Timestamp '2011-09-05T14:48:00.000Z' corresponds with bookmark 'undefined'
235+
⚡️ To restore to this specific bookmark, run:
236+
\`wrangler d1 time-travel restore db --bookmark=undefined\`
237+
"
238+
`);
239+
});
240+
});
241+
});
113242
});

0 commit comments

Comments
 (0)