Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: include banner in output of tests for commands with json/pretty mode #8109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/wrangler/src/__tests__/d1/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";
import { writeWranglerConfig } from "../helpers/write-wrangler-config";

// we want to include the banner to make sure it doesn't show up in the output
// when --json=true
vi.unmock("../../wrangler-banner");

describe("execute", () => {
const std = mockConsoleMethods();
runInTempDir();
Expand Down Expand Up @@ -137,6 +141,29 @@ describe("execute", () => {
)
);
});

it("should show banner by default", async () => {
setIsTTY(false);
writeWranglerConfig({
d1_databases: [
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
],
});

await runWrangler("d1 execute db --command 'select 1;'");
expect(std.out).toContain("⛅️ wrangler x.x.x");
});
it("should not show banner if --json=true", async () => {
setIsTTY(false);
writeWranglerConfig({
d1_databases: [
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
],
});

await runWrangler("d1 execute db --command 'select 1;' --json");
expect(std.out).not.toContain("⛅️ wrangler x.x.x");
});
});

function useSentry() {
Expand Down
86 changes: 72 additions & 14 deletions packages/wrangler/src/__tests__/d1/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";
import { writeWranglerConfig } from "../helpers/write-wrangler-config";

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

it("should display version when alpha", async () => {
beforeEach(() => {
setIsTTY(false);
mockGetMemberships([
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
Expand All @@ -30,6 +33,8 @@ describe("info", () => {
},
],
});
});
it("should display version when alpha", async () => {
msw.use(
http.get("*/accounts/:accountId/d1/database/*", async () => {
return HttpResponse.json(
Expand Down Expand Up @@ -66,19 +71,6 @@ describe("info", () => {
});

it("should not display version when not alpha", async () => {
setIsTTY(false);
mockGetMemberships([
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
]);
writeWranglerConfig({
d1_databases: [
{
binding: "DB",
database_name: "northwind",
database_id: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
},
],
});
msw.use(
http.get("*/accounts/:accountId/d1/database/*", async () => {
return HttpResponse.json(
Expand Down Expand Up @@ -129,4 +121,70 @@ describe("info", () => {
}"
`);
});

it("should print banner in pretty mode", async () => {
msw.use(
http.get("*/accounts/:accountId/d1/database/*", async () => {
return HttpResponse.json(
{
result: {
uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
name: "northwind",
created_at: "2023-05-23T08:33:54.590Z",
version: "beta",
num_tables: 13,
file_size: 33067008,
running_in_region: "WEUR",
},
success: true,
errors: [],
messages: [],
},
{ status: 200 }
);
})
);
msw.use(
http.post("*/graphql", async () => {
return HttpResponse.json(
{
result: null,
success: true,
errors: [],
messages: [],
},
{ status: 200 }
);
})
);
// pretty print by default
await runWrangler("d1 info northwind");
expect(std.out).toMatchInlineSnapshot(`
"
⛅️ wrangler x.x.x
------------------

┌───────────────────┬──────────────────────────────────────┐
│ DB │ d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06 │
├───────────────────┼──────────────────────────────────────┤
│ name │ northwind │
├───────────────────┼──────────────────────────────────────┤
│ created_at │ 2023-05-23T08:33:54.590Z │
├───────────────────┼──────────────────────────────────────┤
│ num_tables │ 13 │
├───────────────────┼──────────────────────────────────────┤
│ running_in_region │ WEUR │
├───────────────────┼──────────────────────────────────────┤
│ database_size │ 33.1 MB │
├───────────────────┼──────────────────────────────────────┤
│ read_queries_24h │ 0 │
├───────────────────┼──────────────────────────────────────┤
│ write_queries_24h │ 0 │
├───────────────────┼──────────────────────────────────────┤
│ rows_read_24h │ 0 │
├───────────────────┼──────────────────────────────────────┤
│ rows_written_24h │ 0 │
└───────────────────┴──────────────────────────────────────┘"
`);
});
});
85 changes: 85 additions & 0 deletions packages/wrangler/src/__tests__/d1/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { http, HttpResponse } from "msw";
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
import { mockConsoleMethods } from "../helpers/mock-console";
import { useMockIsTTY } from "../helpers/mock-istty";
import { mockGetMemberships } from "../helpers/mock-oauth-flow";
import { msw } from "../helpers/msw";
import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";

// we want to include the banner to make sure it doesn't show up in the output when
// when --json=true
vi.unmock("../../wrangler-banner");
describe("list", () => {
mockAccountId({ accountId: null });
mockApiToken();
mockConsoleMethods();
runInTempDir();
const std = mockConsoleMethods();
const { setIsTTY } = useMockIsTTY();

beforeEach(() => {
setIsTTY(false);
mockGetMemberships([
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
]);
msw.use(
http.get("*/accounts/:accountId/d1/database", async () => {
return HttpResponse.json(
{
result: [
{
uuid: "1",
name: "a",
binding: "A",
},
{
uuid: "2",
name: "b",
binding: "B",
},
],
success: true,
errors: [],
messages: [],
},
{ status: 200 }
);
})
);
});
it("should print as json if specified, without wrangler banner", async () => {
await runWrangler("d1 list --json");
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"uuid\\": \\"1\\",
\\"name\\": \\"a\\",
\\"binding\\": \\"A\\"
},
{
\\"uuid\\": \\"2\\",
\\"name\\": \\"b\\",
\\"binding\\": \\"B\\"
}
]"
`);
});

it("should pretty print by default", async () => {
await runWrangler("d1 list");
expect(std.out).toMatchInlineSnapshot(`
"
⛅️ wrangler x.x.x
------------------

┌──────┬──────┬─────────┐
│ uuid │ name │ binding │
├──────┼──────┼─────────┤
│ 1 │ a │ A │
├──────┼──────┼─────────┤
│ 2 │ b │ B │
└──────┴──────┴─────────┘"
`);
});
});
131 changes: 130 additions & 1 deletion packages/wrangler/src/__tests__/d1/timeTravel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import { runInTempDir } from "../helpers/run-in-tmp";
import { runWrangler } from "../helpers/run-wrangler";
import { writeWranglerConfig } from "../helpers/write-wrangler-config";

// we want to include the banner to make sure it doesn't show up in the output when
// when --json=true
vi.unmock("../../wrangler-banner");

describe("time-travel", () => {
mockConsoleMethods();
const std = mockConsoleMethods();
mockAccountId({ accountId: null });
mockApiToken();
runInTempDir();
Expand Down Expand Up @@ -110,4 +114,129 @@ describe("time-travel", () => {
expect(result).toBeUndefined();
});
});

describe("--json", () => {
beforeEach(() => {
setIsTTY(false);
writeWranglerConfig({
d1_databases: [
{ binding: "DATABASE", database_name: "db", database_id: "xxxx" },
],
});
mockGetMemberships([
{ id: "IG-88", account: { id: "1701", name: "enterprise" } },
]);
msw.use(
http.get("*/accounts/:accountId/d1/database/*", async () => {
return HttpResponse.json(
{
result: {
uuid: "d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06",
name: "db",
created_at: "2023-05-23T08:33:54.590Z",
version: "beta",
num_tables: 13,
file_size: 33067008,
running_in_region: "WEUR",
},
success: true,
errors: [],
messages: [],
},
{ status: 200 }
);
}),
http.post(
"*/accounts/:accountId/d1/database/*/time_travel/restore",
async () => {
return HttpResponse.json(
{
result: {
bookmark: "a",
},
success: true,
errors: [],
messages: [],
},
{ status: 200 }
);
}
)
);
vi.useFakeTimers();
vi.setSystemTime(new Date("2011-10-05T14:48:00.000Z"));
});
afterEach(() => {
vi.useRealTimers();
});
describe("restore", () => {
it("should print as json if specified, without wrangler banner", async () => {
await runWrangler(
`d1 time-travel restore db --timestamp=2011-09-05T14:48:00.000Z --json`
);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"bookmark\\": \\"a\\"
}"
`);
});

it("should pretty print by default", async () => {
await runWrangler(
`d1 time-travel restore db --timestamp=2011-09-05T14:48:00.000Z"`
);
expect(std.out).toMatchInlineSnapshot(`
"
⛅️ wrangler x.x.x
------------------

🚧 Restoring database db from bookmark undefined

⚠️ This will overwrite all data in database db.
In-flight queries and transactions will be cancelled.

? OK to proceed (y/N)
🤖 Using fallback value in non-interactive context: yes
⚡️ Time travel in progress...
✅ Database db restored back to bookmark a

↩️ To undo this operation, you can restore to the previous bookmark: undefined"
`);
});
});
describe("info", () => {
it("should print as json if specified, without wrangler banner", async () => {
await runWrangler(
`d1 time-travel info db --timestamp=2011-09-05T14:48:00.000Z --json`
);
expect(std.out).toMatchInlineSnapshot(`
"{
\\"uuid\\": \\"d5b1d127-xxxx-xxxx-xxxx-cbc69f0a9e06\\",
\\"name\\": \\"db\\",
\\"created_at\\": \\"2023-05-23T08:33:54.590Z\\",
\\"version\\": \\"beta\\",
\\"num_tables\\": 13,
\\"file_size\\": 33067008,
\\"running_in_region\\": \\"WEUR\\"
}"
`);
});
it("should pretty print by default", async () => {
await runWrangler(
`d1 time-travel info db --timestamp=2011-09-05T14:48:00.000Z"`
);
expect(std.out).toMatchInlineSnapshot(`
"
⛅️ wrangler x.x.x
------------------

🚧 Time Traveling...
⚠️ Timestamp '2011-09-05T14:48:00.000Z' corresponds with bookmark 'undefined'
⚡️ To restore to this specific bookmark, run:
\`wrangler d1 time-travel restore db --bookmark=undefined\`
"
`);
});
});
});
});
Loading
Loading