Skip to content

Commit ce9db80

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 ce9db80

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-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-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mMissing 'CLOUDFLARE_API_TOKEN' from non-TTY environment, please see docs for more info: TBD[0m
236+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mMissing 'CLOUDFLARE_API_TOKEN' from non-TTY environment, please see docs for more info[0m
237237
238+
"
239+
`);
240+
});
241+
it("should", async () => {
242+
setIsTTY(false);
243+
writeWranglerToml({
244+
account_id: undefined,
245+
});
246+
process.env = {
247+
CLOUDFLARE_API_TOKEN: "picard",
248+
CLOUDFLARE_ACCOUNT_ID: undefined,
249+
};
250+
writeWorkerSource();
251+
mockSubDomainRequest();
252+
mockUploadWorkerRequest();
253+
mockOAuthServerCallback();
254+
mockGetMemberships({
255+
success: false,
256+
result: [],
257+
});
258+
259+
await expect(runWrangler("publish index.js")).rejects.toThrowError();
238260

239-
X [ERROR] Did not login, quitting...
261+
expect(std.err).toMatchInlineSnapshot(`
262+
"X [ERROR] Failed to automatically retrieve account IDs the logged in user,
263+
Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
264+

240265
241266
"
242267
`);

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

+7-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,12 @@ 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 the logged in user,
181+
Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
182+
"
183+
`);
185184
});
186185

187186
it("should use the account from wrangler.toml", async () => {
@@ -204,7 +203,6 @@ describe("wrangler secret", () => {
204203
});
205204

206205
it("should error if a user has multiple accounts, and has not specified an account in wrangler.toml", async () => {
207-
setIsTTY(false);
208206
mockGetMemberships({
209207
success: true,
210208
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+
"Missing 'CLOUDFLARE_API_TOKEN' from non-TTY environment, please see docs for more info"
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 the logged in user,\nPlease set the appropriate \`account_id\` in your \`wrangler.toml\` file.\n`
1145+
);
11431146
}
11441147
return accountId;
11451148
}

0 commit comments

Comments
 (0)