Skip to content

Commit a699560

Browse files
committed
fixup tests and devin comments
1 parent 1fc7b51 commit a699560

13 files changed

Lines changed: 306 additions & 132 deletions

File tree

packages/wrangler/src/__tests__/artifacts.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
12
import { http, HttpResponse } from "msw";
23
import { afterEach, beforeEach, describe, it, vi } from "vitest";
34
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
@@ -34,7 +35,7 @@ describe("artifacts", () => {
3435
clearDialogs();
3536
msw.resetHandlers();
3637
});
37-
38+
runInTempDir();
3839
describe("namespaces", () => {
3940
it("should show help for namespace commands", async ({ expect }) => {
4041
await runWrangler("artifacts namespaces --help");

packages/wrangler/src/__tests__/banner.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ describe("wrangler banner", () => {
1616
expect(std.out).toMatchInlineSnapshot(`
1717
"
1818
⛅️ wrangler x.x.x
19-
──────────────────
20-
Using profile: testing"
19+
──────────────────"
2120
`);
2221
});
2322

@@ -39,8 +38,7 @@ describe("wrangler banner", () => {
3938
expect(std.out).toMatchInlineSnapshot(`
4039
"
4140
⛅️ wrangler x.x.x
42-
──────────────────
43-
Using profile: testing"
41+
──────────────────"
4442
`);
4543
});
4644
});

packages/wrangler/src/__tests__/containers/registries.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
12
import { http, HttpResponse } from "msw";
23
import { afterEach, beforeEach, describe, it, vi } from "vitest";
34
import { mockAccount } from "../cloudchamber/utils";
@@ -19,6 +20,7 @@ import { runWrangler } from "../helpers/run-wrangler";
1920
import type { ExpectStatic } from "vitest";
2021

2122
describe("containers registries --help", () => {
23+
runInTempDir();
2224
const std = mockConsoleMethods();
2325

2426
it("should help", async ({ expect }) => {
@@ -46,6 +48,7 @@ describe("containers registries --help", () => {
4648
});
4749

4850
describe("containers registries configure", () => {
51+
runInTempDir();
4952
const std = mockConsoleMethods();
5053
const { setIsTTY } = useMockIsTTY();
5154
const cliStd = mockCLIOutput();
@@ -569,6 +572,7 @@ describe("containers registries configure", () => {
569572
});
570573

571574
describe("containers registries list", () => {
575+
runInTempDir();
572576
const std = mockConsoleMethods();
573577
const cliStd = mockCLIOutput();
574578
mockAccountId();
@@ -601,8 +605,7 @@ describe("containers registries list", () => {
601605
expect(std.out).toMatchInlineSnapshot(`
602606
"
603607
⛅️ wrangler x.x.x
604-
──────────────────
605-
Using profile: testing"
608+
──────────────────"
606609
`);
607610
expect(cliStd.stdout).toMatchInlineSnapshot(`
608611
"╭ List configured container registries
@@ -636,6 +639,7 @@ describe("containers registries list", () => {
636639
});
637640

638641
describe("containers registries delete", () => {
642+
runInTempDir();
639643
const cliStd = mockCLIOutput();
640644
const std = mockConsoleMethods();
641645
const { setIsTTY } = useMockIsTTY();
@@ -698,7 +702,6 @@ describe("containers registries delete", () => {
698702
"
699703
⛅️ wrangler x.x.x
700704
──────────────────
701-
Using profile: testing
702705
? Are you sure you want to delete the registry credentials for 123456789012.dkr.ecr.us-west-2.amazonaws.com? This action cannot be undone.
703706
🤖 Using fallback value in non-interactive context: yes"
704707
`);
@@ -837,6 +840,7 @@ describe("containers registries delete", () => {
837840
});
838841

839842
describe("containers registries credentials", () => {
843+
runInTempDir();
840844
const { setIsTTY } = useMockIsTTY();
841845
const std = mockConsoleMethods();
842846
mockAccountId();

packages/wrangler/src/__tests__/profile.test.ts

Lines changed: 127 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import { mockConsoleMethods } from "./helpers/mock-console";
2020
import { mockConfirm } from "./helpers/mock-dialogs";
2121
import { useMockIsTTY } from "./helpers/mock-istty";
2222
import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
23-
import { msw, mswSuccessOauthHandlers } from "./helpers/msw";
23+
import {
24+
msw,
25+
mswSuccessOauthHandlers,
26+
mswSuccessUserHandlers,
27+
} from "./helpers/msw";
2428
import { runWrangler } from "./helpers/run-wrangler";
2529

2630
describe("Profile", () => {
@@ -51,6 +55,12 @@ describe("Profile", () => {
5155
});
5256

5357
describe("getAuthConfigFilePath", () => {
58+
it("should return default.toml when no profile is active", ({ expect }) => {
59+
const filePath = getAuthConfigFilePath();
60+
expect(filePath).toContain("default.toml");
61+
expect(filePath).not.toContain("profiles");
62+
});
63+
5464
it("should return default.toml for the default profile", ({ expect }) => {
5565
const filePath = getAuthConfigFilePath("default");
5666
expect(filePath).toContain("default.toml");
@@ -66,10 +76,6 @@ describe("Profile", () => {
6676
});
6777

6878
describe("profileExists", () => {
69-
it("should always return true for default", ({ expect }) => {
70-
expect(profileExists("default")).toBe(true);
71-
});
72-
7379
it("should return false for non-existent profiles", ({ expect }) => {
7480
expect(profileExists("nonexistent")).toBe(false);
7581
});
@@ -88,8 +94,8 @@ describe("Profile", () => {
8894
});
8995

9096
describe("getActiveProfileName", () => {
91-
it("should return default when nothing is configured", ({ expect }) => {
92-
expect(getActiveProfileName()).toBe("default");
97+
it("should return undefined when nothing is configured", ({ expect }) => {
98+
expect(getActiveProfileName()).toBeUndefined();
9399
});
94100

95101
it("should respect WRANGLER_PROFILE env var", ({ expect }) => {
@@ -152,7 +158,6 @@ describe("Profile", () => {
152158
});
153159
expect(getProfileForDirectory(dir)).toBe("child-profile");
154160
});
155-
156161
});
157162

158163
describe("CLI commands", () => {
@@ -418,6 +423,65 @@ describe("Profile", () => {
418423
});
419424
});
420425

426+
describe("active profile in banner", () => {
427+
beforeEach(() => {
428+
msw.use(...mswSuccessUserHandlers);
429+
});
430+
431+
it("should print the active profile under the banner", async ({
432+
expect,
433+
}) => {
434+
writeAuthConfigFile(
435+
{
436+
oauth_token: "token",
437+
refresh_token: "refresh",
438+
expiration_time: "2030-01-01T00:00:00Z",
439+
},
440+
"work"
441+
);
442+
setActiveProfile("work");
443+
444+
await runWrangler("whoami");
445+
446+
expect(std.out).toContain("Using profile: work");
447+
});
448+
449+
it("should not print the active profile with --json", async ({
450+
expect,
451+
}) => {
452+
writeAuthConfigFile(
453+
{
454+
oauth_token: "token",
455+
refresh_token: "refresh",
456+
expiration_time: "2030-01-01T00:00:00Z",
457+
},
458+
"work"
459+
);
460+
setActiveProfile("work");
461+
462+
await runWrangler("whoami --json");
463+
464+
expect(std.out).not.toContain("Using profile");
465+
const output = JSON.parse(std.out);
466+
expect(output.loggedIn).toBe(true);
467+
expect(output.profile).toBe("work");
468+
});
469+
470+
it("should not print profile line when using default profile", async ({
471+
expect,
472+
}) => {
473+
writeAuthConfigFile({
474+
oauth_token: "token",
475+
refresh_token: "refresh",
476+
expiration_time: "2030-01-01T00:00:00Z",
477+
});
478+
479+
await runWrangler("whoami");
480+
481+
expect(std.out).not.toContain("Using profile");
482+
});
483+
});
484+
421485
describe("wrangler profiles unset", () => {
422486
it("should reset the active profile to default", async ({ expect }) => {
423487
writeAuthConfigFile(
@@ -433,7 +497,7 @@ describe("Profile", () => {
433497

434498
await runWrangler("profiles unset");
435499

436-
expect(getActiveProfileName()).toBe("default");
500+
expect(getActiveProfileName()).toBeUndefined();
437501
expect(std.out).toContain(
438502
'Switched from profile "work" back to the default profile.'
439503
);
@@ -446,6 +510,60 @@ describe("Profile", () => {
446510
expect(std.out).toContain("Already using the default profile.");
447511
});
448512

513+
it("should throw when the active profile comes from WRANGLER_PROFILE env var", async ({
514+
expect,
515+
}) => {
516+
vi.stubEnv("WRANGLER_PROFILE", "work");
517+
await expect(
518+
runWrangler("profiles unset")
519+
).rejects.toThrowErrorMatchingInlineSnapshot(
520+
`
521+
[Error: The active profile "work" is set via the WRANGLER_PROFILE environment variable.
522+
]
523+
`
524+
);
525+
vi.unstubAllEnvs();
526+
});
527+
528+
it("should not destroy active.json when WRANGLER_PROFILE is set", async ({
529+
expect,
530+
}) => {
531+
setActiveProfile("personal");
532+
vi.stubEnv("WRANGLER_PROFILE", "work");
533+
534+
await expect(runWrangler("profiles unset")).rejects
535+
.toThrowErrorMatchingInlineSnapshot(`
536+
[Error: The active profile "work" is set via the WRANGLER_PROFILE environment variable.
537+
]
538+
`);
539+
});
540+
541+
it("should warn about directory binding but still unset active.json", async ({
542+
expect,
543+
}) => {
544+
writeAuthConfigFile(
545+
{
546+
oauth_token: "token",
547+
refresh_token: "refresh",
548+
expiration_time: "2030-01-01T00:00:00Z",
549+
},
550+
"work"
551+
);
552+
setActiveProfile("work");
553+
554+
const dir = process.cwd();
555+
writeDirectoryBindings({ [dir]: "work" });
556+
557+
await runWrangler("profiles unset");
558+
559+
expect(std.warn).toContain(
560+
'This directory is bound to the profile "work"'
561+
);
562+
expect(std.out).toContain(
563+
'Switched from profile "work" back to the default profile.'
564+
);
565+
});
566+
449567
it("should remove a directory binding when --dir is provided", async ({
450568
expect,
451569
}) => {

packages/wrangler/src/__tests__/user.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ describe("User", () => {
301301
await runWrangler("login");
302302

303303
expect(std.err).toMatchInlineSnapshot(`
304-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mYou are currently using the auth profile "work[0m
304+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mYou are currently using the auth profile "work".[0m
305305
306-
". If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
306+
If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
307307
If you want to switch to an existing auth profile, run \`wrangler profiles set <profile name>\`.
308308
309309
@@ -321,9 +321,9 @@ describe("User", () => {
321321
await runWrangler("login");
322322

323323
expect(std.err).toMatchInlineSnapshot(`
324-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mThis directory is bound to the auth profile "work[0m
324+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mThis directory is bound to the auth profile "work"[0m
325325
326-
" If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
326+
If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
327327
If you want to switch to an existing auth profile, run \`wrangler profiles set <profile name>\`.
328328
329329

packages/wrangler/src/__tests__/vpc.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ describe("extractPortFromHostname", () => {
865865
});
866866

867867
describe("hostname validation", () => {
868+
runInTempDir();
868869
it("should accept valid hostnames", ({ expect }) => {
869870
expect(() => validateHostname("api.example.com")).not.toThrow();
870871
expect(() => validateHostname("localhost")).not.toThrow();

packages/wrangler/src/__tests__/whoami.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ describe("whoami", () => {
449449
expect(() => (output = JSON.parse(std.out))).not.toThrow();
450450
expect(output).toEqual({
451451
loggedIn: true,
452-
profile: "default",
453452
authType: "OAuth Token",
454453
email: "user@example.com",
455454
accounts: [

packages/wrangler/src/core/register-yargs-command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ function createHandler(def: InternalCommandDefinition, argv: string[]) {
147147
typeof shouldPrintBanner !== "function" || bannerEnabled;
148148

149149
if (!getWranglerHideBanner()) {
150-
if ((def.behaviour?.printActiveProfile ?? true) && shouldPrintBanner) {
150+
if ((def.behaviour?.printActiveProfile ?? true) && bannerEnabled) {
151151
const activeProfile = getActiveProfileName();
152-
if (activeProfile !== "default") {
152+
if (activeProfile) {
153153
logger.log(`Using profile: ${chalk.blue(activeProfile)}`);
154154
}
155155
}

0 commit comments

Comments
 (0)