Skip to content

Commit fca10bc

Browse files
committed
WC-5618 update no deployments message, enhance tests
1 parent f4dfd4c commit fca10bc

8 files changed

Lines changed: 77 additions & 38 deletions

File tree

packages/wrangler/src/__tests__/preview.base-config.secret.test.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ import { runWrangler } from "./helpers/run-wrangler";
1313

1414
type PreviewBaseConfigPatchBody = {
1515
previews_base_config?: {
16-
env?: Record<string, { type: string; text?: string } | null>;
16+
env?: Record<string, { type: string; text: string } | null>;
1717
};
1818
};
1919

2020
type PreviewBaseConfigWorkerResult = {
2121
previews_base_config?: {
22-
env?: Record<string, { type: string; text?: string }>;
22+
env?: Record<string, { type: string; text: string }>;
2323
};
2424
preview_defaults?: {
25-
env?: Record<string, { type: string; text?: string }>;
25+
env?: Record<string, { type: string; text: string }>;
2626
};
2727
};
2828

@@ -49,7 +49,7 @@ function mockPatchWorker(
4949
}
5050

5151
function mockGetWorker(
52-
env: Record<string, { type: string; text?: string }>,
52+
env: Record<string, { type: string; text: string }>,
5353
onRequest?: (info: { url: string }) => void
5454
) {
5555
mockGetWorkerResult({ previews_base_config: { env } }, onRequest);
@@ -396,30 +396,24 @@ describe("wrangler preview", () => {
396396
expect(requestUrl).not.toContain("/previews/");
397397
});
398398

399-
// Matrix over output format (json vs. pretty) and whether the API
400-
// returns a text value for the secret. In every combination we only
401-
// list secret bindings (never plain_text) and never print the value.
399+
// Matrix over output format (json vs. pretty). In every combination we
400+
// only list secret bindings (never plain_text) and never print the value.
402401
test.for([
403402
{
404403
name: "json, value provided",
405404
json: true,
406405
text: "super-secret-value",
407406
},
408-
{ name: "json, no value", json: true, text: undefined },
409407
{
410408
name: "pretty, value provided",
411409
json: false,
412410
text: "super-secret-value",
413411
},
414-
{ name: "pretty, no value", json: false, text: undefined },
415412
])(
416413
"lists only secrets and never leaks their values ($name)",
417414
async ({ json, text }, { expect }) => {
418415
mockGetWorker({
419-
MY_SECRET:
420-
text === undefined
421-
? { type: "secret_text" }
422-
: { type: "secret_text", text },
416+
MY_SECRET: { type: "secret_text", text },
423417
PLAIN: { type: "plain_text", text: "not-a-secret" },
424418
});
425419

packages/wrangler/src/__tests__/preview.secret.test.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { msw } from "./helpers/msw";
1010
import { runWrangler } from "./helpers/run-wrangler";
1111

1212
type PreviewDeploymentPatchBody = {
13-
env?: Record<string, { type: string; text?: string } | null>;
13+
env?: Record<string, { type: string; text: string } | null>;
1414
annotations?: Record<string, string | undefined>;
1515
};
1616

@@ -20,6 +20,8 @@ const BRANCH_ENV_VARS = [
2020
"GITHUB_REF_NAME",
2121
"CI_COMMIT_REF_NAME",
2222
] as const;
23+
const NO_ACTIVE_PREVIEW_URLS_MESSAGE =
24+
"Note: This Preview deployment has no active URLs. To get one, enable Preview Deployments on workers.dev or a custom domain. See https://developers.cloudflare.com/workers/previews/custom-domains/ for more information";
2325

2426
async function withoutBranchEnvVars<T>(callback: () => Promise<T>): Promise<T> {
2527
const originalBranchEnv = Object.fromEntries(
@@ -43,7 +45,8 @@ async function withoutBranchEnvVars<T>(callback: () => Promise<T>): Promise<T> {
4345
}
4446

4547
function mockPatchLatestPreviewDeployment(
46-
onRequest?: (info: { url: string; body: PreviewDeploymentPatchBody }) => void
48+
onRequest?: (info: { url: string; body: PreviewDeploymentPatchBody }) => void,
49+
urls: string[] | undefined = ["https://test-preview.example.workers.dev"]
4750
) {
4851
msw.use(
4952
http.patch(
@@ -59,7 +62,7 @@ function mockPatchLatestPreviewDeployment(
5962
id: "deployment-1",
6063
preview_id: "preview-1",
6164
preview_name: String(params.previewId),
62-
urls: ["https://test-preview.example.workers.dev"],
65+
urls,
6366
created_on: "2025-01-01T00:00:00Z",
6467
},
6568
});
@@ -87,7 +90,7 @@ function mockPatchPreviewDeploymentError(code: number) {
8790
}
8891

8992
function mockGetLatestPreviewDeployment(
90-
env: Record<string, { type: string; text?: string }>,
93+
env: Record<string, { type: string; text: string }>,
9194
onRequest?: (info: { url: string }) => void
9295
) {
9396
msw.use(
@@ -188,6 +191,21 @@ describe("wrangler preview", () => {
188191
expect(std.out).not.toContain("preview-secret");
189192
});
190193

194+
test("notes when the new Preview deployment has no active URLs", async ({
195+
expect,
196+
}) => {
197+
mockStdIn.send("preview-secret");
198+
mockPatchLatestPreviewDeployment(undefined, []);
199+
200+
await runWrangler(
201+
"preview secret put API_KEY --name test-preview --worker-name test-worker"
202+
);
203+
204+
expect(std.out).toContain("Created Preview deployment deployment-1");
205+
expect(std.out).toContain(NO_ACTIVE_PREVIEW_URLS_MESSAGE);
206+
expect(std.out).not.toContain("is now live at");
207+
});
208+
191209
test("defaults the Preview name to the current git branch", async ({
192210
expect,
193211
}) => {
@@ -378,6 +396,20 @@ describe("wrangler preview", () => {
378396
);
379397
});
380398

399+
test("notes when the new Preview deployment has no active URLs", async ({
400+
expect,
401+
}) => {
402+
mockPatchLatestPreviewDeployment(undefined, []);
403+
404+
await runWrangler(
405+
"preview secret delete REMOVE_ME --name test-preview --skip-confirmation --worker-name test-worker"
406+
);
407+
408+
expect(std.out).toContain("Created Preview deployment deployment-1");
409+
expect(std.out).toContain(NO_ACTIVE_PREVIEW_URLS_MESSAGE);
410+
expect(std.out).not.toContain("is now live at");
411+
});
412+
381413
test("respects env-specific worker name when deleting a secret", async ({
382414
expect,
383415
}) => {
@@ -450,7 +482,7 @@ describe("wrangler preview", () => {
450482
test("reads the latest Preview deployment", async ({ expect }) => {
451483
let requestUrl: string | undefined;
452484
mockGetLatestPreviewDeployment(
453-
{ API_KEY: { type: "secret_text" } },
485+
{ API_KEY: { type: "secret_text", text: "preview-secret" } },
454486
({ url }) => {
455487
requestUrl = url;
456488
}
@@ -463,30 +495,24 @@ describe("wrangler preview", () => {
463495
);
464496
});
465497

466-
// Matrix over output format (json vs. pretty) and whether the API
467-
// returns a text value for the secret. In every combination we only
468-
// list secret bindings (never plain_text) and never print the value.
498+
// Matrix over output format (json vs. pretty). In every combination we
499+
// only list secret bindings (never plain_text) and never print the value.
469500
test.for([
470501
{
471502
name: "json, value provided",
472503
json: true,
473504
text: "super-secret-value",
474505
},
475-
{ name: "json, no value", json: true, text: undefined },
476506
{
477507
name: "pretty, value provided",
478508
json: false,
479509
text: "super-secret-value",
480510
},
481-
{ name: "pretty, no value", json: false, text: undefined },
482511
])(
483512
"lists only secrets and never leaks their values ($name)",
484513
async ({ json, text }, { expect }) => {
485514
mockGetLatestPreviewDeployment({
486-
MY_SECRET:
487-
text === undefined
488-
? { type: "secret_text" }
489-
: { type: "secret_text", text },
515+
MY_SECRET: { type: "secret_text", text },
490516
PLAIN: { type: "plain_text", text: "not-a-secret" },
491517
});
492518
await runWrangler(
@@ -609,6 +635,21 @@ describe("wrangler preview", () => {
609635
expect(std.out).not.toContain("two");
610636
});
611637

638+
test("notes when the new Preview deployment has no active URLs", async ({
639+
expect,
640+
}) => {
641+
writeFileSync("secrets.env", "FIRST_KEY=one\nSECOND_KEY=two\n");
642+
mockPatchLatestPreviewDeployment(undefined, []);
643+
644+
await runWrangler(
645+
"preview secret bulk secrets.env --name test-preview --worker-name test-worker"
646+
);
647+
648+
expect(std.out).toContain("Created Preview deployment deployment-1");
649+
expect(std.out).toContain(NO_ACTIVE_PREVIEW_URLS_MESSAGE);
650+
expect(std.out).not.toContain("is now live at");
651+
});
652+
612653
test("should respect env-specific worker name when bulk uploading secrets", async ({
613654
expect,
614655
}) => {

packages/wrangler/src/preview/base-config/secrets/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ export const previewBaseConfigSecretNamespace = createNamespace({
1111
});
1212

1313
export function rejectUnsupportedPreviewArgs(args: Record<string, unknown>) {
14-
for (const [key, flag] of [
15-
["name", "name"],
16-
["tag", "tag"],
17-
["message", "message"],
18-
] as const) {
19-
if (args[key] !== undefined) {
14+
for (const flag of ["name", "tag", "message"] as const) {
15+
if (args[flag] !== undefined) {
2016
throw new CommandLineArgsError(`Unknown argument: ${flag}`, {
2117
telemetryMessage: "preview base-config unsupported flag",
2218
});

packages/wrangler/src/preview/secrets/bulk.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { logger } from "../../logger";
55
import { parseBulkInputToObject } from "../../secret";
66
import { requireAuth } from "../../user";
77
import {
8+
NO_ACTIVE_PREVIEW_URLS_MESSAGE,
89
patchPreviewDeploymentSecrets,
910
resolvePreviewName,
1011
toSecretBindingsPatch,
@@ -106,7 +107,7 @@ export const previewSecretBulkCommand = createCommand({
106107
? `\n➡️ Your Preview "${previewName}" is now live at ${liveUrls
107108
.map((url) => chalk.bold.underline(url))
108109
.join(", ")}`
109-
: "")
110+
: `\n${NO_ACTIVE_PREVIEW_URLS_MESSAGE}`)
110111
);
111112
},
112113
});

packages/wrangler/src/preview/secrets/delete.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { createCommand } from "../../core/create-command";
44
import { confirm } from "../../dialogs";
55
import { logger } from "../../logger";
66
import { requireAuth } from "../../user";
7-
import { patchPreviewDeploymentSecrets, resolvePreviewName } from "./index";
7+
import {
8+
NO_ACTIVE_PREVIEW_URLS_MESSAGE,
9+
patchPreviewDeploymentSecrets,
10+
resolvePreviewName,
11+
} from "./index";
812

913
export const previewSecretDeleteCommand = createCommand({
1014
metadata: {
@@ -90,7 +94,7 @@ export const previewSecretDeleteCommand = createCommand({
9094
? `\n➡️ Your Preview "${previewName}" is now live at ${liveUrls
9195
.map((url) => chalk.bold.underline(url))
9296
.join(", ")}`
93-
: "")
97+
: `\n${NO_ACTIVE_PREVIEW_URLS_MESSAGE}`)
9498
);
9599
}
96100
},

packages/wrangler/src/preview/secrets/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export function toSecretBindingsPatch(
4646
export const NO_PREVIEW_DEPLOYMENT_PATCH_ERR_CODE = 10032;
4747
export const NO_PREVIEW_DEPLOYMENT_GET_ERR_CODE = 10222;
4848
export const PREVIEW_NOT_FOUND_ERR_CODE = 10025;
49+
export const NO_ACTIVE_PREVIEW_URLS_MESSAGE =
50+
"Note: This Preview deployment has no active URLs. To get one, enable Preview Deployments on workers.dev or a custom domain. See https://developers.cloudflare.com/workers/previews/custom-domains/ for more information";
4951

5052
export function noPreviewDeploymentPatchMessage(previewName: string) {
5153
return `There are currently no deployments for the Preview "${previewName}". Please create a Preview deployment before modifying a secret.`;

packages/wrangler/src/preview/secrets/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type SecretSummary = {
3131

3232
function isSecretBinding(binding: Binding): binding is Binding & {
3333
type: "secret_text";
34-
text?: string;
34+
text: string;
3535
} {
3636
return binding.type === "secret_text";
3737
}

packages/wrangler/src/preview/secrets/put.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { logger } from "../../logger";
66
import { requireAuth } from "../../user";
77
import { readFromStdin, trimTrailingWhitespace } from "../../utils/std";
88
import {
9+
NO_ACTIVE_PREVIEW_URLS_MESSAGE,
910
patchPreviewDeploymentSecrets,
1011
resolvePreviewName,
1112
toSecretBindingsPatch,
@@ -88,7 +89,7 @@ export const previewSecretPutCommand = createCommand({
8889
? `\n➡️ Your Preview "${previewName}" is now live at ${liveUrls
8990
.map((url) => chalk.bold.underline(url))
9091
.join(", ")}`
91-
: "")
92+
: `\n${NO_ACTIVE_PREVIEW_URLS_MESSAGE}`)
9293
);
9394
},
9495
});

0 commit comments

Comments
 (0)