Skip to content

Commit ae58fb1

Browse files
committed
fixup tests and devin comments
1 parent 3aa4897 commit ae58fb1

13 files changed

Lines changed: 231 additions & 123 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: 73 additions & 8 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", () => {
@@ -67,6 +71,12 @@ describe("Profile", () => {
6771
});
6872

6973
describe("getAuthConfigFilePath", () => {
74+
it("should return default.toml when no profile is active", ({ expect }) => {
75+
const filePath = getAuthConfigFilePath();
76+
expect(filePath).toContain("default.toml");
77+
expect(filePath).not.toContain("profiles");
78+
});
79+
7080
it("should return default.toml for the default profile", ({ expect }) => {
7181
const filePath = getAuthConfigFilePath("default");
7282
expect(filePath).toContain("default.toml");
@@ -82,10 +92,6 @@ describe("Profile", () => {
8292
});
8393

8494
describe("profileExists", () => {
85-
it("should always return true for default", ({ expect }) => {
86-
expect(profileExists("default")).toBe(true);
87-
});
88-
8995
it("should return false for non-existent profiles", ({ expect }) => {
9096
expect(profileExists("nonexistent")).toBe(false);
9197
});
@@ -104,8 +110,8 @@ describe("Profile", () => {
104110
});
105111

106112
describe("getActiveProfileName", () => {
107-
it("should return default when nothing is configured", ({ expect }) => {
108-
expect(getActiveProfileName()).toBe("default");
113+
it("should return undefined when nothing is configured", ({ expect }) => {
114+
expect(getActiveProfileName()).toBeUndefined();
109115
});
110116

111117
it("should respect WRANGLER_PROFILE env var", ({ expect }) => {
@@ -451,6 +457,65 @@ describe("Profile", () => {
451457
});
452458
});
453459

460+
describe("active profile in banner", () => {
461+
beforeEach(() => {
462+
msw.use(...mswSuccessUserHandlers);
463+
});
464+
465+
it("should print the active profile under the banner", async ({
466+
expect,
467+
}) => {
468+
writeAuthConfigFile(
469+
{
470+
oauth_token: "token",
471+
refresh_token: "refresh",
472+
expiration_time: "2030-01-01T00:00:00Z",
473+
},
474+
"work"
475+
);
476+
setActiveProfile("work");
477+
478+
await runWrangler("whoami");
479+
480+
expect(std.out).toContain("Using profile: work");
481+
});
482+
483+
it("should not print the active profile with --json", async ({
484+
expect,
485+
}) => {
486+
writeAuthConfigFile(
487+
{
488+
oauth_token: "token",
489+
refresh_token: "refresh",
490+
expiration_time: "2030-01-01T00:00:00Z",
491+
},
492+
"work"
493+
);
494+
setActiveProfile("work");
495+
496+
await runWrangler("whoami --json");
497+
498+
expect(std.out).not.toContain("Using profile");
499+
const output = JSON.parse(std.out);
500+
expect(output.loggedIn).toBe(true);
501+
expect(output.profile).toBe("work");
502+
});
503+
504+
it("should not print profile line when using default profile", async ({
505+
expect,
506+
}) => {
507+
writeAuthConfigFile({
508+
oauth_token: "token",
509+
refresh_token: "refresh",
510+
expiration_time: "2030-01-01T00:00:00Z",
511+
});
512+
513+
await runWrangler("whoami");
514+
515+
expect(std.out).not.toContain("Using profile");
516+
});
517+
});
518+
454519
describe("wrangler profiles unset", () => {
455520
it("should reset the active profile to default", async ({ expect }) => {
456521
writeAuthConfigFile(
@@ -466,7 +531,7 @@ describe("Profile", () => {
466531

467532
await runWrangler("profiles unset");
468533

469-
expect(getActiveProfileName()).toBe("default");
534+
expect(getActiveProfileName()).toBeUndefined();
470535
expect(std.out).toContain(
471536
'Switched from profile "work" back to the default profile.'
472537
);

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
}

packages/wrangler/src/user/commands.ts

Lines changed: 7 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { CommandLineArgsError, UserError } from "@cloudflare/workers-utils";
1+
import { UserError } from "@cloudflare/workers-utils";
22
import { readConfig } from "../config";
33
import { createCommand, createNamespace } from "../core/create-command";
44
import { logger } from "../logger";
55
import * as metrics from "../metrics";
6+
import { handleScopesArgs, loginArgs } from "./login-shared";
67
import { getActiveProfileName, getProfileForDirectory } from "./profiles";
78
import {
89
getAuthFromEnv,
910
getOAuthTokenFromLocalState,
10-
listScopes,
1111
login,
1212
logout,
13-
validateScopeKeys,
1413
type Scope,
1514
} from "./user";
1615
import { whoami } from "./whoami";
@@ -23,63 +22,6 @@ export type AuthTokenInfo =
2322
| { type: "api_token"; token: string }
2423
| { type: "api_key"; key: string; email: string };
2524

26-
export const loginArgs = {
27-
browser: {
28-
default: true,
29-
type: "boolean",
30-
describe: "Automatically open the OAuth link in a browser",
31-
},
32-
scopes: {
33-
describe: "Pick the set of applicable OAuth scopes when logging in",
34-
array: true,
35-
type: "string",
36-
requiresArg: true,
37-
},
38-
"scopes-list": {
39-
describe: "List all the available OAuth scopes with descriptions",
40-
},
41-
"callback-host": {
42-
describe:
43-
"Use the ip or host address for the temporary login callback server.",
44-
type: "string",
45-
requiresArg: false,
46-
default: "localhost",
47-
},
48-
"callback-port": {
49-
describe: "Use the port for the temporary login callback server.",
50-
type: "number",
51-
requiresArg: false,
52-
default: 8976,
53-
},
54-
} as const;
55-
56-
/**
57-
* Handle `--scopes-list` and `--scopes` validation.
58-
* Returns `true` when the caller should return early (scopes were listed or empty).
59-
*/
60-
export function handleScopesArgs(args: {
61-
scopesList?: unknown;
62-
scopes?: string[];
63-
}): boolean {
64-
if (args.scopesList) {
65-
listScopes();
66-
return true;
67-
}
68-
if (args.scopes) {
69-
if (args.scopes.length === 0) {
70-
listScopes();
71-
return true;
72-
}
73-
if (!validateScopeKeys(args.scopes)) {
74-
throw new CommandLineArgsError(
75-
`One of ${args.scopes} is not a valid authentication scope. Run "wrangler login --scopes-list" to see the valid scopes.`,
76-
{ telemetryMessage: "user login invalid scope" }
77-
);
78-
}
79-
}
80-
return false;
81-
}
82-
8325
export const loginCommand = createCommand({
8426
metadata: {
8527
description: "🔓 Login to Cloudflare",
@@ -100,10 +42,10 @@ export const loginCommand = createCommand({
10042
}
10143

10244
const currentProfile = getActiveProfileName();
103-
if (currentProfile !== "default") {
45+
if (currentProfile) {
10446
const message = getProfileForDirectory()
105-
? `This directory is bound to the auth profile "${currentProfile}\n"`
106-
: `You are currently using the auth profile "${currentProfile}\n".`;
47+
? `This directory is bound to the auth profile "${currentProfile}"\n`
48+
: `You are currently using the auth profile "${currentProfile}".\n`;
10749
logger.error(
10850
message,
10951
"If you want to create a new auth profile, run `wrangler profiles create <profile name>`.\n",
@@ -164,6 +106,8 @@ export const whoamiCommand = createCommand({
164106
behaviour: {
165107
printBanner: (args) => !args.json,
166108
printConfigWarnings: false,
109+
// handle this separately so we include the profile when calling the whoami() function
110+
printActiveProfile: false,
167111
},
168112
args: {
169113
account: {

0 commit comments

Comments
 (0)