Skip to content

Commit 3fbaf9d

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

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", () => {
@@ -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 }) => {
@@ -418,6 +424,65 @@ describe("Profile", () => {
418424
});
419425
});
420426

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

434499
await runWrangler("profiles unset");
435500

436-
expect(getActiveProfileName()).toBe("default");
501+
expect(getActiveProfileName()).toBeUndefined();
437502
expect(std.out).toContain(
438503
'Switched from profile "work" back to the default profile.'
439504
);

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)