@@ -22,6 +22,7 @@ import type { WorkerMetadata } from "../create-worker-upload-form";
22
22
import type { KVNamespaceInfo } from "../kv" ;
23
23
import type { CfWorkerInit } from "../worker" ;
24
24
import type { FormData , File } from "undici" ;
25
+ import { useMockIsTTY } from "./helpers/mock-istty" ;
25
26
26
27
describe ( "publish" , ( ) => {
27
28
mockAccountId ( ) ;
@@ -32,6 +33,7 @@ describe("publish", () => {
32
33
mockOAuthServerCallback,
33
34
mockGrantAccessToken,
34
35
mockGrantAuthorization,
36
+ mockGetMemberships,
35
37
} = mockOAuthFlow ( ) ;
36
38
37
39
beforeEach ( ( ) => {
@@ -56,6 +58,7 @@ describe("publish", () => {
56
58
} ) ;
57
59
58
60
it ( "drops a user into the login flow if they're unauthenticated" , async ( ) => {
61
+ // Should not throw missing Errors in TTY environment
59
62
writeWranglerToml ( ) ;
60
63
writeWorkerSource ( ) ;
61
64
mockSubDomainRequest ( ) ;
@@ -116,6 +119,128 @@ describe("publish", () => {
116
119
` ) ;
117
120
expect ( std . err ) . toMatchInlineSnapshot ( `""` ) ;
118
121
} ) ;
122
+
123
+ describe ( "non-TTY" , ( ) => {
124
+ const ENV_COPY = process . env ;
125
+ const { setIsTTY } = useMockIsTTY ( ) ;
126
+
127
+ afterEach ( ( ) => {
128
+ process . env = ENV_COPY ;
129
+ } ) ;
130
+
131
+ it ( "should not throw an error in non-TTY if 'CLOUDFLARE_API_TOKEN' & 'account_id' are in scope" , async ( ) => {
132
+ process . env = {
133
+ CLOUDFLARE_API_TOKEN : "123456789" ,
134
+ } ;
135
+ setIsTTY ( false ) ;
136
+ writeWranglerToml ( {
137
+ account_id : "some-account-id" ,
138
+ } ) ;
139
+ writeWorkerSource ( ) ;
140
+ mockSubDomainRequest ( ) ;
141
+ mockUploadWorkerRequest ( ) ;
142
+ mockOAuthServerCallback ( ) ;
143
+
144
+ await runWrangler ( "publish index.js" ) ;
145
+
146
+ expect ( std . out ) . toMatchInlineSnapshot ( `
147
+ "Uploaded test-name (TIMINGS)
148
+ Published test-name (TIMINGS)
149
+ test-name.test-sub-domain.workers.dev"
150
+ ` ) ;
151
+ expect ( std . err ) . toMatchInlineSnapshot ( `""` ) ;
152
+ } ) ;
153
+
154
+ it ( "should not throw an error if 'CLOUDFLARE_ACCOUNT_ID' & 'CLOUDFLARE_API_TOKEN' are in scope" , async ( ) => {
155
+ process . env = {
156
+ CLOUDFLARE_API_TOKEN : "hunter2" ,
157
+ CLOUDFLARE_ACCOUNT_ID : "some-account-id" ,
158
+ } ;
159
+ setIsTTY ( false ) ;
160
+ writeWranglerToml ( ) ;
161
+ writeWorkerSource ( ) ;
162
+ mockSubDomainRequest ( ) ;
163
+ mockUploadWorkerRequest ( ) ;
164
+ mockOAuthServerCallback ( ) ;
165
+ mockGetMemberships ( {
166
+ success : true ,
167
+ result : [ ] ,
168
+ } ) ;
169
+
170
+ await runWrangler ( "publish index.js" ) ;
171
+
172
+ expect ( std . out ) . toMatchInlineSnapshot ( `
173
+ "Uploaded test-name (TIMINGS)
174
+ Published test-name (TIMINGS)
175
+ test-name.test-sub-domain.workers.dev"
176
+ ` ) ;
177
+ expect ( std . err ) . toMatchInlineSnapshot ( `""` ) ;
178
+ } ) ;
179
+
180
+ it ( "should throw an error in non-TTY if 'account_id' & 'CLOUDFLARE_ACCOUNT_ID' is missing" , async ( ) => {
181
+ setIsTTY ( false ) ;
182
+ process . env = {
183
+ CLOUDFLARE_API_TOKEN : "hunter2" ,
184
+ CLOUDFLARE_ACCOUNT_ID : undefined ,
185
+ } ;
186
+ writeWranglerToml ( {
187
+ account_id : undefined ,
188
+ } ) ;
189
+ writeWorkerSource ( ) ;
190
+ mockSubDomainRequest ( ) ;
191
+ mockUploadWorkerRequest ( ) ;
192
+ mockOAuthServerCallback ( ) ;
193
+ mockGetMemberships ( {
194
+ success : true ,
195
+ result : [
196
+ { id : "IG-88" , account : { id : "1701" , name : "enterprise" } } ,
197
+ { id : "R2-D2" , account : { id : "nx01" , name : "enterprise-nx" } } ,
198
+ ] ,
199
+ } ) ;
200
+
201
+ await expect ( runWrangler ( "publish index.js" ) ) . rejects
202
+ . toMatchInlineSnapshot ( `
203
+ [Error: More than one account available but unable to select one in non-interactive mode.
204
+ Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
205
+ Available accounts are ("<name>" - "<id>"):
206
+ "enterprise" - "1701")
207
+ "enterprise-nx" - "nx01")]
208
+ ` ) ;
209
+ } ) ;
210
+
211
+ it ( "should throw error in non-TTY if 'CLOUDFLARE_API_TOKEN' is missing" , async ( ) => {
212
+ setIsTTY ( false ) ;
213
+ writeWranglerToml ( {
214
+ account_id : undefined ,
215
+ } ) ;
216
+ process . env = {
217
+ CLOUDFLARE_API_TOKEN : undefined ,
218
+ CLOUDFLARE_ACCOUNT_ID : "badwolf" ,
219
+ } ;
220
+ writeWorkerSource ( ) ;
221
+ mockSubDomainRequest ( ) ;
222
+ mockUploadWorkerRequest ( ) ;
223
+ mockOAuthServerCallback ( ) ;
224
+ mockGetMemberships ( {
225
+ success : true ,
226
+ result : [
227
+ { id : "IG-88" , account : { id : "1701" , name : "enterprise" } } ,
228
+ { id : "R2-D2" , account : { id : "nx01" , name : "enterprise-nx" } } ,
229
+ ] ,
230
+ } ) ;
231
+
232
+ await expect ( runWrangler ( "publish index.js" ) ) . rejects . toThrowError ( ) ;
233
+
234
+ expect ( std . err ) . toMatchInlineSnapshot ( `
235
+ "[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
+
237
+
238
+ [31mX [41;31m[[41;97mERROR[41;31m][0m [1mDid not login, quitting...[0m
239
+
240
+ "
241
+ ` ) ;
242
+ } ) ;
243
+ } ) ;
119
244
} ) ;
120
245
121
246
describe ( "environments" , ( ) => {
0 commit comments