Skip to content

Commit f1c3343

Browse files
committed
non-TTY check for required variables:
Added a check in non-TTY environments for `account_id`, `CLOUDFLARE_ACCOUNT_ID` and `CLOUDFLARE_API_TOKEN`. If `account_id` exists in `wrangler.toml` then `CLOUDFLARE_ACCOUNT_ID` is not needed in non-TTY scope. The `CLOUDFLARE_API_TOKEN` is necessary in non-TTY scope and will always error if missing. resolves #827
1 parent 370200f commit f1c3343

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

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

+28-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe("publish", () => {
178178
expect(std.err).toMatchInlineSnapshot(`""`);
179179
});
180180

181-
it("should throw an error in non-TTY if 'account_id' & 'CLOUDFLARE_ACCOUNT_ID' is missing", async () => {
181+
it("should throw an error in non-TTY & there is more than one account associated with API token", async () => {
182182
setIsTTY(false);
183183
process.env = {
184184
CLOUDFLARE_API_TOKEN: "hunter2",
@@ -233,10 +233,35 @@ describe("publish", () => {
233233
await expect(runWrangler("publish index.js")).rejects.toThrowError();
234234

235235
expect(std.err).toMatchInlineSnapshot(`
236-
"X [ERROR] Missing 'CLOUDFLARE_API_TOKEN' from non-TTY environment, please see docs for more info: TBD
236+
"X [ERROR] In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work.
237+
Please go to https://developers.cloudflare.com/api/tokens/create/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN.
237238
239+
"
240+
`);
241+
});
242+
it("should throw error with no account ID provided and no members retrieved", async () => {
243+
setIsTTY(false);
244+
writeWranglerToml({
245+
account_id: undefined,
246+
});
247+
process.env = {
248+
CLOUDFLARE_API_TOKEN: "picard",
249+
CLOUDFLARE_ACCOUNT_ID: undefined,
250+
};
251+
writeWorkerSource();
252+
mockSubDomainRequest();
253+
mockUploadWorkerRequest();
254+
mockOAuthServerCallback();
255+
mockGetMemberships({
256+
success: false,
257+
result: [],
258+
});
238259

239-
X [ERROR] Did not login, quitting...
260+
await expect(runWrangler("publish index.js")).rejects.toThrowError();
261+
262+
expect(std.err).toMatchInlineSnapshot(`
263+
"X [ERROR] Failed to automatically retrieve account IDs for the logged in user.
264+
In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file.
240265
241266
"
242267
`);

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
44
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
55
import { mockConsoleMethods } from "./helpers/mock-console";
66
import { mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
7-
import { useMockIsTTY } from "./helpers/mock-istty";
87
import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
98
import { useMockStdin } from "./helpers/mock-stdin";
109
import { runInTempDir } from "./helpers/run-in-tmp";
@@ -13,14 +12,13 @@ import { runWrangler } from "./helpers/run-wrangler";
1312
describe("wrangler secret", () => {
1413
const std = mockConsoleMethods();
1514
const { mockGetMemberships } = mockOAuthFlow();
16-
const { setIsTTY } = useMockIsTTY();
15+
1716
runInTempDir();
1817
mockAccountId();
1918
mockApiToken();
2019

2120
afterEach(() => {
2221
unsetAllMocks();
23-
setIsTTY(true);
2422
});
2523

2624
describe("put", () => {
@@ -177,11 +175,11 @@ describe("wrangler secret", () => {
177175
success: false,
178176
result: [],
179177
});
180-
await expect(
181-
runWrangler("secret put the-key --name script-name")
182-
).rejects.toThrowErrorMatchingInlineSnapshot(
183-
`"No account id found, quitting..."`
184-
);
178+
await expect(runWrangler("secret put the-key --name script-name"))
179+
.rejects.toThrowErrorMatchingInlineSnapshot(`
180+
"Failed to automatically retrieve account IDs for the logged in user.
181+
In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file."
182+
`);
185183
});
186184

187185
it("should use the account from wrangler.toml", async () => {
@@ -204,7 +202,6 @@ describe("wrangler secret", () => {
204202
});
205203

206204
it("should error if a user has multiple accounts, and has not specified an account in wrangler.toml", async () => {
207-
setIsTTY(false);
208205
mockGetMemberships({
209206
success: true,
210207
result: [

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

+6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import { writeAuthConfigFile } from "../user";
44
import { getUserInfo, WhoAmI } from "../whoami";
55
import { setMockResponse } from "./helpers/mock-cfetch";
66
import { mockConsoleMethods } from "./helpers/mock-console";
7+
import { useMockIsTTY } from "./helpers/mock-istty";
78
import { runInTempDir } from "./helpers/run-in-tmp";
89
import type { UserInfo } from "../whoami";
910

1011
describe("getUserInfo()", () => {
1112
runInTempDir({ homedir: "./home" });
1213
const std = mockConsoleMethods();
14+
const { setIsTTY } = useMockIsTTY();
15+
16+
beforeEach(() => {
17+
setIsTTY(true);
18+
});
1319

1420
it("should return undefined if there is no config file", async () => {
1521
const userInfo = await getUserInfo();

packages/wrangler/src/user.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,9 @@ export function getAPIToken(): string | undefined {
418418
!localAPIToken &&
419419
!LocalState.accessToken?.value
420420
) {
421-
logger.error(
422-
"Missing 'CLOUDFLARE_API_TOKEN' from non-TTY environment, please see docs for more info: TBD"
421+
throw new Error(
422+
"In a non-interactive environment, it's necessary to set a CLOUDFLARE_API_TOKEN environment variable for wrangler to work.\nPlease go to https://developers.cloudflare.com/api/tokens/create/ for instructions on how to create an api token, and assign its value to CLOUDFLARE_API_TOKEN."
423423
);
424-
425-
return;
426424
}
427425

428426
return localAPIToken ?? LocalState.accessToken?.value;
@@ -1140,6 +1138,11 @@ export async function getAccountId(
11401138
.join("\n")
11411139
);
11421140
}
1141+
} else {
1142+
if (!isInteractive)
1143+
throw new Error(
1144+
`Failed to automatically retrieve account IDs for the logged in user.\nIn a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file.`
1145+
);
11431146
}
11441147
return accountId;
11451148
}

0 commit comments

Comments
 (0)