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