wrangler 0.0.0-e3cab74 → 0.0.0-e3ce82b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +49 -5
  4. package/miniflare-dist/index.mjs +4886 -0
  5. package/package.json +43 -28
  6. package/pages/functions/buildPlugin.ts +105 -0
  7. package/pages/functions/buildWorker.ts +93 -4
  8. package/pages/functions/filepath-routing.test.ts +136 -25
  9. package/pages/functions/filepath-routing.ts +14 -3
  10. package/pages/functions/routes.ts +6 -1
  11. package/pages/functions/template-plugin.ts +147 -0
  12. package/pages/functions/template-worker.ts +21 -8
  13. package/src/__tests__/config-cache.test.ts +36 -0
  14. package/src/__tests__/configuration.test.ts +1713 -411
  15. package/src/__tests__/dev.test.tsx +954 -84
  16. package/src/__tests__/guess-worker-format.test.ts +37 -18
  17. package/src/__tests__/helpers/mock-account-id.ts +22 -4
  18. package/src/__tests__/helpers/mock-cfetch.ts +8 -4
  19. package/src/__tests__/helpers/mock-console.ts +35 -10
  20. package/src/__tests__/helpers/mock-dialogs.ts +67 -2
  21. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  22. package/src/__tests__/helpers/mock-istty.ts +27 -0
  23. package/src/__tests__/helpers/mock-kv.ts +9 -16
  24. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  25. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  26. package/src/__tests__/helpers/run-in-tmp.ts +10 -0
  27. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  28. package/src/__tests__/https-options.test.ts +130 -0
  29. package/src/__tests__/index.test.ts +138 -747
  30. package/src/__tests__/init.test.ts +1637 -0
  31. package/src/__tests__/jest.setup.ts +28 -8
  32. package/src/__tests__/kv.test.ts +598 -258
  33. package/src/__tests__/logger.test.ts +115 -0
  34. package/src/__tests__/package-manager.test.ts +154 -7
  35. package/src/__tests__/pages.test.ts +754 -0
  36. package/src/__tests__/parse.test.ts +8 -0
  37. package/src/__tests__/publish.test.ts +4374 -1025
  38. package/src/__tests__/r2.test.ts +69 -34
  39. package/src/__tests__/route.test.ts +45 -0
  40. package/src/__tests__/secret.test.ts +228 -73
  41. package/src/__tests__/tail.test.ts +146 -20
  42. package/src/__tests__/user.test.ts +131 -0
  43. package/src/__tests__/whoami.test.tsx +60 -7
  44. package/src/abort.d.ts +3 -0
  45. package/src/bundle.ts +75 -11
  46. package/src/cfetch/index.ts +21 -4
  47. package/src/cfetch/internal.ts +29 -20
  48. package/src/cli.ts +5 -11
  49. package/src/config/README.md +7 -7
  50. package/src/config/config.ts +69 -100
  51. package/src/config/environment.ts +119 -4
  52. package/src/config/index.ts +177 -4
  53. package/src/config/validation-helpers.ts +237 -23
  54. package/src/config/validation.ts +722 -191
  55. package/src/config-cache.ts +45 -0
  56. package/src/{api/preview.ts → create-worker-preview.ts} +65 -27
  57. package/src/{api/form_data.ts → create-worker-upload-form.ts} +55 -18
  58. package/src/dev/dev-vars.ts +36 -0
  59. package/src/dev/dev.tsx +401 -0
  60. package/src/dev/local.tsx +322 -0
  61. package/src/dev/remote.tsx +237 -0
  62. package/src/dev/use-esbuild.ts +118 -0
  63. package/src/dialogs.tsx +64 -6
  64. package/src/durable.ts +102 -0
  65. package/src/entry.ts +279 -14
  66. package/src/environment-variables.ts +3 -1
  67. package/src/errors.ts +11 -0
  68. package/src/https-options.ts +123 -0
  69. package/src/index.tsx +1386 -867
  70. package/src/inspect.ts +64 -11
  71. package/src/intl-polyfill.d.ts +139 -0
  72. package/src/{kv.tsx → kv.ts} +195 -62
  73. package/src/logger.ts +66 -0
  74. package/src/miniflare-cli/README.md +30 -0
  75. package/src/miniflare-cli/enum-keys.ts +17 -0
  76. package/src/miniflare-cli/index.ts +41 -0
  77. package/src/miniflare-cli/request-context.ts +40 -0
  78. package/src/module-collection.ts +94 -6
  79. package/src/open-in-browser.ts +9 -3
  80. package/src/package-manager.ts +59 -11
  81. package/src/pages.tsx +1123 -75
  82. package/src/parse.ts +26 -6
  83. package/src/proxy.ts +159 -57
  84. package/src/publish.ts +581 -228
  85. package/src/selfsigned.d.ts +29 -0
  86. package/src/sites.tsx +130 -45
  87. package/src/tail/index.ts +2 -2
  88. package/src/tail/printing.ts +13 -8
  89. package/src/update-check.ts +19 -0
  90. package/src/user.tsx +275 -110
  91. package/src/whoami.tsx +5 -3
  92. package/src/{api/worker.ts → worker.ts} +24 -29
  93. package/src/zones.ts +73 -0
  94. package/templates/gitignore +171 -0
  95. package/templates/new-worker-scheduled.js +17 -0
  96. package/templates/new-worker-scheduled.ts +32 -0
  97. package/templates/new-worker.ts +16 -1
  98. package/templates/static-asset-facade.js +1 -5
  99. package/wrangler-dist/cli.js +104803 -118375
  100. package/src/__tests__/helpers/mock-user.ts +0 -27
  101. package/src/__tests__/logout.test.ts +0 -50
  102. package/src/__tests__/sentry.test.ts +0 -146
  103. package/src/dev.tsx +0 -961
  104. package/src/guess-worker-format.ts +0 -68
  105. package/src/reporting.ts +0 -115
  106. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  107. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  108. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  109. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  110. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  111. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  112. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  113. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  114. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  115. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  116. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  117. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  118. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  119. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  120. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  121. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  122. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  123. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  124. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  125. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  126. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  127. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  128. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  129. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  130. package/vendor/wrangler-mime/LICENSE +0 -21
  131. package/vendor/wrangler-mime/Mime.js +0 -97
  132. package/vendor/wrangler-mime/README.md +0 -187
  133. package/vendor/wrangler-mime/cli.js +0 -46
  134. package/vendor/wrangler-mime/index.js +0 -4
  135. package/vendor/wrangler-mime/lite.js +0 -4
  136. package/vendor/wrangler-mime/package.json +0 -52
  137. package/vendor/wrangler-mime/types/other.js +0 -1
  138. package/vendor/wrangler-mime/types/standard.js +0 -1
  139. package/wrangler-dist/cli.js.map +0 -7
@@ -17,6 +17,33 @@ describe("wrangler", () => {
17
17
 
18
18
  describe("r2", () => {
19
19
  describe("bucket", () => {
20
+ it("should show the correct help when an invalid command is passed", async () => {
21
+ await expect(() =>
22
+ runWrangler("r2 bucket foo")
23
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`"Unknown argument: foo"`);
24
+ expect(std.err).toMatchInlineSnapshot(`
25
+ "X [ERROR] Unknown argument: foo
26
+
27
+ "
28
+ `);
29
+ expect(std.out).toMatchInlineSnapshot(`
30
+ "
31
+ wrangler r2 bucket
32
+
33
+ Manage R2 buckets
34
+
35
+ Commands:
36
+ wrangler r2 bucket create <name> Create a new R2 bucket
37
+ wrangler r2 bucket list List R2 buckets
38
+ wrangler r2 bucket delete <name> Delete an R2 bucket
39
+
40
+ Flags:
41
+ -c, --config Path to .toml configuration file [string]
42
+ -h, --help Show help [boolean]
43
+ -v, --version Show version number [boolean]"
44
+ `);
45
+ });
46
+
20
47
  describe("list", () => {
21
48
  function mockListRequest(buckets: R2BucketInfo[]) {
22
49
  const requests = { count: 0 };
@@ -67,9 +94,9 @@ describe("wrangler", () => {
67
94
  ).rejects.toThrowErrorMatchingInlineSnapshot(
68
95
  `"Not enough non-option arguments: got 0, need at least 1"`
69
96
  );
70
- expect(std.out).toMatchInlineSnapshot(`""`);
71
- expect(std.err).toMatchInlineSnapshot(`
72
- "wrangler r2 bucket create <name>
97
+ expect(std.out).toMatchInlineSnapshot(`
98
+ "
99
+ wrangler r2 bucket create <name>
73
100
 
74
101
  Create a new R2 bucket
75
102
 
@@ -77,12 +104,14 @@ describe("wrangler", () => {
77
104
  name The name of the new bucket [string] [required]
78
105
 
79
106
  Flags:
80
- -c, --config Path to .toml configuration file [string]
81
- -h, --help Show help [boolean]
82
- -v, --version Show version number [boolean]
83
- --legacy-env Use legacy environments [boolean]
107
+ -c, --config Path to .toml configuration file [string]
108
+ -h, --help Show help [boolean]
109
+ -v, --version Show version number [boolean]"
110
+ `);
111
+ expect(std.err).toMatchInlineSnapshot(`
112
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
84
113
 
85
- Not enough non-option arguments: got 0, need at least 1"
114
+ "
86
115
  `);
87
116
  });
88
117
 
@@ -90,11 +119,11 @@ describe("wrangler", () => {
90
119
  await expect(
91
120
  runWrangler("r2 bucket create abc def ghi")
92
121
  ).rejects.toThrowErrorMatchingInlineSnapshot(
93
- `"Unexpected additional positional arguments \\"def ghi\\"."`
122
+ `"Unknown arguments: def, ghi"`
94
123
  );
95
- expect(std.out).toMatchInlineSnapshot(`""`);
96
- expect(std.err).toMatchInlineSnapshot(`
97
- "wrangler r2 bucket create <name>
124
+ expect(std.out).toMatchInlineSnapshot(`
125
+ "
126
+ wrangler r2 bucket create <name>
98
127
 
99
128
  Create a new R2 bucket
100
129
 
@@ -102,12 +131,14 @@ describe("wrangler", () => {
102
131
  name The name of the new bucket [string] [required]
103
132
 
104
133
  Flags:
105
- -c, --config Path to .toml configuration file [string]
106
- -h, --help Show help [boolean]
107
- -v, --version Show version number [boolean]
108
- --legacy-env Use legacy environments [boolean]
134
+ -c, --config Path to .toml configuration file [string]
135
+ -h, --help Show help [boolean]
136
+ -v, --version Show version number [boolean]"
137
+ `);
138
+ expect(std.err).toMatchInlineSnapshot(`
139
+ "X [ERROR] Unknown arguments: def, ghi
109
140
 
110
- Unexpected additional positional arguments \\"def ghi\\"."
141
+ "
111
142
  `);
112
143
  });
113
144
 
@@ -143,9 +174,9 @@ describe("wrangler", () => {
143
174
  ).rejects.toThrowErrorMatchingInlineSnapshot(
144
175
  `"Not enough non-option arguments: got 0, need at least 1"`
145
176
  );
146
- expect(std.out).toMatchInlineSnapshot(`""`);
147
- expect(std.err).toMatchInlineSnapshot(`
148
- "wrangler r2 bucket delete <name>
177
+ expect(std.out).toMatchInlineSnapshot(`
178
+ "
179
+ wrangler r2 bucket delete <name>
149
180
 
150
181
  Delete an R2 bucket
151
182
 
@@ -153,12 +184,14 @@ describe("wrangler", () => {
153
184
  name The name of the bucket to delete [string] [required]
154
185
 
155
186
  Flags:
156
- -c, --config Path to .toml configuration file [string]
157
- -h, --help Show help [boolean]
158
- -v, --version Show version number [boolean]
159
- --legacy-env Use legacy environments [boolean]
187
+ -c, --config Path to .toml configuration file [string]
188
+ -h, --help Show help [boolean]
189
+ -v, --version Show version number [boolean]"
190
+ `);
191
+ expect(std.err).toMatchInlineSnapshot(`
192
+ "X [ERROR] Not enough non-option arguments: got 0, need at least 1
160
193
 
161
- Not enough non-option arguments: got 0, need at least 1"
194
+ "
162
195
  `);
163
196
  });
164
197
 
@@ -166,11 +199,11 @@ describe("wrangler", () => {
166
199
  await expect(
167
200
  runWrangler("r2 bucket delete abc def ghi")
168
201
  ).rejects.toThrowErrorMatchingInlineSnapshot(
169
- `"Unexpected additional positional arguments \\"def ghi\\"."`
202
+ `"Unknown arguments: def, ghi"`
170
203
  );
171
- expect(std.out).toMatchInlineSnapshot(`""`);
172
- expect(std.err).toMatchInlineSnapshot(`
173
- "wrangler r2 bucket delete <name>
204
+ expect(std.out).toMatchInlineSnapshot(`
205
+ "
206
+ wrangler r2 bucket delete <name>
174
207
 
175
208
  Delete an R2 bucket
176
209
 
@@ -178,12 +211,14 @@ describe("wrangler", () => {
178
211
  name The name of the bucket to delete [string] [required]
179
212
 
180
213
  Flags:
181
- -c, --config Path to .toml configuration file [string]
182
- -h, --help Show help [boolean]
183
- -v, --version Show version number [boolean]
184
- --legacy-env Use legacy environments [boolean]
214
+ -c, --config Path to .toml configuration file [string]
215
+ -h, --help Show help [boolean]
216
+ -v, --version Show version number [boolean]"
217
+ `);
218
+ expect(std.err).toMatchInlineSnapshot(`
219
+ "X [ERROR] Unknown arguments: def, ghi
185
220
 
186
- Unexpected additional positional arguments \\"def ghi\\"."
221
+ "
187
222
  `);
188
223
  });
189
224
 
@@ -0,0 +1,45 @@
1
+ import { mockConsoleMethods } from "./helpers/mock-console";
2
+ import { runInTempDir } from "./helpers/run-in-tmp";
3
+ import { runWrangler } from "./helpers/run-wrangler";
4
+
5
+ describe("wrangler route", () => {
6
+ mockConsoleMethods();
7
+ runInTempDir();
8
+
9
+ it("shows a deprecation notice when `wrangler route` is run", async () => {
10
+ await expect(runWrangler("route")).rejects
11
+ .toThrowErrorMatchingInlineSnapshot(`
12
+ "Deprecation:
13
+ \`wrangler route\` has been deprecated.
14
+ Please use wrangler.toml and/or \`wrangler publish --routes\` to modify routes"
15
+ `);
16
+ });
17
+
18
+ it("shows a deprecation notice when `wrangler route delete` is run", async () => {
19
+ await expect(runWrangler("route delete")).rejects
20
+ .toThrowErrorMatchingInlineSnapshot(`
21
+ "Deprecation:
22
+ \`wrangler route delete\` has been deprecated.
23
+ Remove the unwanted route(s) from wrangler.toml and run \`wrangler publish\` to remove your worker from those routes."
24
+ `);
25
+ });
26
+
27
+ it("shows a deprecation notice when `wrangler route delete <id>` is run", async () => {
28
+ await expect(runWrangler("route delete some-zone-id")).rejects
29
+ .toThrowErrorMatchingInlineSnapshot(`
30
+ "Deprecation:
31
+ \`wrangler route delete\` has been deprecated.
32
+ Remove the unwanted route(s) from wrangler.toml and run \`wrangler publish\` to remove your worker from those routes."
33
+ `);
34
+ });
35
+
36
+ it("shows a deprecation notice when `wrangler route list` is run", async () => {
37
+ await expect(runWrangler("route list")).rejects
38
+ .toThrowErrorMatchingInlineSnapshot(`
39
+ "Deprecation:
40
+ \`wrangler route list\` has been deprecated.
41
+ Refer to wrangler.toml for a list of routes the worker will be deployed to upon publishing.
42
+ Refer to the Cloudflare Dashboard to see the routes this worker is currently running on."
43
+ `);
44
+ });
45
+ });
@@ -1,18 +1,31 @@
1
+ import * as fs from "node:fs";
2
+ import * as TOML from "@iarna/toml";
1
3
  import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2
4
  import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
3
5
  import { mockConsoleMethods } from "./helpers/mock-console";
4
- import { mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
6
+ import {
7
+ mockConfirm,
8
+ mockPrompt,
9
+ clearConfirmMocks,
10
+ clearPromptMocks,
11
+ } from "./helpers/mock-dialogs";
12
+ import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
13
+ import { useMockStdin } from "./helpers/mock-stdin";
5
14
  import { runInTempDir } from "./helpers/run-in-tmp";
6
15
  import { runWrangler } from "./helpers/run-wrangler";
7
16
 
8
17
  describe("wrangler secret", () => {
9
18
  const std = mockConsoleMethods();
19
+ const { mockGetMemberships } = mockOAuthFlow();
20
+
10
21
  runInTempDir();
11
22
  mockAccountId();
12
23
  mockApiToken();
13
24
 
14
25
  afterEach(() => {
15
26
  unsetAllMocks();
27
+ clearConfirmMocks();
28
+ clearPromptMocks();
16
29
  });
17
30
 
18
31
  describe("put", () => {
@@ -44,84 +57,222 @@ describe("wrangler secret", () => {
44
57
  );
45
58
  }
46
59
 
47
- it("should create a secret", async () => {
48
- mockPrompt({
49
- text: "Enter a secret value:",
50
- type: "password",
51
- result: "the-secret",
60
+ describe("interactive", () => {
61
+ useMockStdin({ isTTY: true });
62
+
63
+ it("should trim stdin secret value", async () => {
64
+ mockPrompt({
65
+ text: "Enter a secret value:",
66
+ type: "password",
67
+ result: `hunter2
68
+ `,
69
+ });
70
+
71
+ mockPutRequest({ name: `secret-name`, text: `hunter2` });
72
+ await runWrangler("secret put secret-name --name script-name");
73
+ expect(std.out).toMatchInlineSnapshot(`
74
+ "🌀 Creating the secret for script script-name
75
+ ✨ Success! Uploaded secret secret-name"
76
+ `);
52
77
  });
53
78
 
54
- mockPutRequest({ name: "the-secret-name", text: "the-secret" });
55
- await runWrangler("secret put the-key --name script-name");
79
+ it("should create a secret", async () => {
80
+ mockPrompt({
81
+ text: "Enter a secret value:",
82
+ type: "password",
83
+ result: "the-secret",
84
+ });
56
85
 
57
- expect(std.out).toMatchInlineSnapshot(`
58
- "🌀 Creating the secret for script script-name
59
- ✨ Success! Uploaded secret the-key"
60
- `);
61
- expect(std.err).toMatchInlineSnapshot(`""`);
62
- });
86
+ mockPutRequest({ name: "the-key", text: "the-secret" });
87
+ await runWrangler("secret put the-key --name script-name");
63
88
 
64
- it("should create a secret: legacy envs", async () => {
65
- mockPrompt({
66
- text: "Enter a secret value:",
67
- type: "password",
68
- result: "the-secret",
89
+ expect(std.out).toMatchInlineSnapshot(`
90
+ "🌀 Creating the secret for script script-name
91
+ ✨ Success! Uploaded secret the-key"
92
+ `);
93
+ expect(std.err).toMatchInlineSnapshot(`""`);
69
94
  });
70
95
 
71
- mockPutRequest(
72
- { name: "the-secret-name", text: "the-secret" },
73
- "some-env",
74
- true
75
- );
76
- await runWrangler(
77
- "secret put the-key --name script-name --env some-env --legacy-env"
78
- );
96
+ it("should create a secret: legacy envs", async () => {
97
+ mockPrompt({
98
+ text: "Enter a secret value:",
99
+ type: "password",
100
+ result: "the-secret",
101
+ });
79
102
 
80
- expect(std.out).toMatchInlineSnapshot(`
81
- "🌀 Creating the secret for script script-name-some-env
82
- ✨ Success! Uploaded secret the-key"
83
- `);
84
- expect(std.err).toMatchInlineSnapshot(`""`);
85
- });
103
+ mockPutRequest(
104
+ { name: "the-key", text: "the-secret" },
105
+ "some-env",
106
+ true
107
+ );
108
+ await runWrangler(
109
+ "secret put the-key --name script-name --env some-env --legacy-env"
110
+ );
86
111
 
87
- it("should create a secret: service envs", async () => {
88
- mockPrompt({
89
- text: "Enter a secret value:",
90
- type: "password",
91
- result: "the-secret",
112
+ expect(std.out).toMatchInlineSnapshot(`
113
+ "🌀 Creating the secret for script script-name-some-env
114
+ ✨ Success! Uploaded secret the-key"
115
+ `);
116
+ expect(std.err).toMatchInlineSnapshot(`""`);
92
117
  });
93
118
 
94
- mockPutRequest(
95
- { name: "the-secret-name", text: "the-secret" },
96
- "some-env",
97
- false
98
- );
99
- await runWrangler(
100
- "secret put the-key --name script-name --env some-env --legacy-env false"
101
- );
119
+ it("should create a secret: service envs", async () => {
120
+ mockPrompt({
121
+ text: "Enter a secret value:",
122
+ type: "password",
123
+ result: "the-secret",
124
+ });
102
125
 
103
- expect(std.out).toMatchInlineSnapshot(`
104
- "🌀 Creating the secret for script script-name (some-env)
105
- ✨ Success! Uploaded secret the-key"
106
- `);
107
- expect(std.err).toMatchInlineSnapshot(`""`);
126
+ mockPutRequest(
127
+ { name: "the-key", text: "the-secret" },
128
+ "some-env",
129
+ false
130
+ );
131
+ await runWrangler(
132
+ "secret put the-key --name script-name --env some-env --legacy-env false"
133
+ );
134
+
135
+ expect(std.out).toMatchInlineSnapshot(`
136
+ "🌀 Creating the secret for script script-name (some-env)
137
+ ✨ Success! Uploaded secret the-key"
138
+ `);
139
+ expect(std.err).toMatchInlineSnapshot(`""`);
140
+ });
141
+
142
+ it("should error without a script name", async () => {
143
+ let error: Error | undefined;
144
+ try {
145
+ await runWrangler("secret put the-key");
146
+ } catch (e) {
147
+ error = e as Error;
148
+ }
149
+ expect(std.out).toMatchInlineSnapshot(`
150
+ "
151
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
152
+ `);
153
+ expect(std.err).toMatchInlineSnapshot(`
154
+ "X [ERROR] Missing script name
155
+
156
+ "
157
+ `);
158
+ expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
159
+ });
108
160
  });
109
161
 
110
- it("should error without a script name", async () => {
111
- let error: Error | undefined;
112
- try {
113
- await runWrangler("secret put the-key");
114
- } catch (e) {
115
- error = e as Error;
116
- }
117
- expect(std.out).toMatchInlineSnapshot(`""`);
118
- expect(std.err).toMatchInlineSnapshot(`
119
- "Missing script name
162
+ describe("non-interactive", () => {
163
+ const mockStdIn = useMockStdin({ isTTY: false });
120
164
 
121
- %s
122
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
123
- `);
124
- expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
165
+ it("should trim stdin secret value, from piped input", async () => {
166
+ mockPutRequest({ name: "the-key", text: "the-secret" });
167
+ // Pipe the secret in as three chunks to test that we reconstitute it correctly.
168
+ mockStdIn.send(
169
+ `the`,
170
+ `-`,
171
+ `secret
172
+ ` // whitespace & newline being removed
173
+ );
174
+ await runWrangler("secret put the-key --name script-name");
175
+
176
+ expect(std.out).toMatchInlineSnapshot(`
177
+ "🌀 Creating the secret for script script-name
178
+ ✨ Success! Uploaded secret the-key"
179
+ `);
180
+ expect(std.warn).toMatchInlineSnapshot(`""`);
181
+ expect(std.err).toMatchInlineSnapshot(`""`);
182
+ });
183
+
184
+ it("should create a secret, from piped input", async () => {
185
+ mockPutRequest({ name: "the-key", text: "the-secret" });
186
+ // Pipe the secret in as three chunks to test that we reconstitute it correctly.
187
+ mockStdIn.send("the", "-", "secret");
188
+ await runWrangler("secret put the-key --name script-name");
189
+
190
+ expect(std.out).toMatchInlineSnapshot(`
191
+ "🌀 Creating the secret for script script-name
192
+ ✨ Success! Uploaded secret the-key"
193
+ `);
194
+ expect(std.warn).toMatchInlineSnapshot(`""`);
195
+ expect(std.err).toMatchInlineSnapshot(`""`);
196
+ });
197
+
198
+ it("should error if the piped input fails", async () => {
199
+ mockPutRequest({ name: "the-key", text: "the-secret" });
200
+ mockStdIn.throwError(new Error("Error in stdin stream"));
201
+ await expect(
202
+ runWrangler("secret put the-key --name script-name")
203
+ ).rejects.toThrowErrorMatchingInlineSnapshot(`"Error in stdin stream"`);
204
+
205
+ expect(std.out).toMatchInlineSnapshot(`
206
+ "
207
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
208
+ `);
209
+ expect(std.warn).toMatchInlineSnapshot(`""`);
210
+ });
211
+
212
+ describe("with accountId", () => {
213
+ mockAccountId({ accountId: null });
214
+
215
+ it("should error if a user has no account", async () => {
216
+ mockGetMemberships({
217
+ success: false,
218
+ result: [],
219
+ });
220
+ await expect(
221
+ runWrangler("secret put the-key --name script-name")
222
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
223
+ `"Failed to automatically retrieve account IDs for the logged in user. 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."`
224
+ );
225
+ });
226
+
227
+ it("should use the account from wrangler.toml", async () => {
228
+ fs.writeFileSync(
229
+ "wrangler.toml",
230
+ TOML.stringify({
231
+ account_id: "some-account-id",
232
+ }),
233
+ "utf-8"
234
+ );
235
+ mockStdIn.send("the-secret");
236
+ mockPutRequest({ name: "the-key", text: "the-secret" });
237
+ await runWrangler("secret put the-key --name script-name");
238
+ expect(std.out).toMatchInlineSnapshot(`
239
+ "🌀 Creating the secret for script script-name
240
+ ✨ Success! Uploaded secret the-key"
241
+ `);
242
+ expect(std.warn).toMatchInlineSnapshot(`""`);
243
+ expect(std.err).toMatchInlineSnapshot(`""`);
244
+ });
245
+
246
+ it("should error if a user has multiple accounts, and has not specified an account in wrangler.toml", async () => {
247
+ mockGetMemberships({
248
+ success: true,
249
+ result: [
250
+ {
251
+ id: "1",
252
+ account: { id: "account-id-1", name: "account-name-1" },
253
+ },
254
+ {
255
+ id: "2",
256
+ account: { id: "account-id-2", name: "account-name-2" },
257
+ },
258
+ {
259
+ id: "3",
260
+ account: { id: "account-id-3", name: "account-name-3" },
261
+ },
262
+ ],
263
+ });
264
+
265
+ await expect(runWrangler("secret put the-key --name script-name"))
266
+ .rejects.toThrowErrorMatchingInlineSnapshot(`
267
+ "More than one account available but unable to select one in non-interactive mode.
268
+ Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
269
+ Available accounts are (\\"<name>\\" - \\"<id>\\"):
270
+ \\"account-name-1\\" - \\"account-id-1\\")
271
+ \\"account-name-2\\" - \\"account-id-2\\")
272
+ \\"account-name-3\\" - \\"account-id-3\\")"
273
+ `);
274
+ });
275
+ });
125
276
  });
126
277
  });
127
278
 
@@ -219,12 +370,14 @@ describe("wrangler secret", () => {
219
370
  } catch (e) {
220
371
  error = e as Error;
221
372
  }
222
- expect(std.out).toMatchInlineSnapshot(`""`);
373
+ expect(std.out).toMatchInlineSnapshot(`
374
+ "
375
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
376
+ `);
223
377
  expect(std.err).toMatchInlineSnapshot(`
224
- "Missing script name
378
+ "X [ERROR] Missing script name
225
379
 
226
- %s
227
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
380
+ "
228
381
  `);
229
382
  expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
230
383
  });
@@ -313,12 +466,14 @@ describe("wrangler secret", () => {
313
466
  } catch (e) {
314
467
  error = e as Error;
315
468
  }
316
- expect(std.out).toMatchInlineSnapshot(`""`);
469
+ expect(std.out).toMatchInlineSnapshot(`
470
+ "
471
+ If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new/choose"
472
+ `);
317
473
  expect(std.err).toMatchInlineSnapshot(`
318
- "Missing script name
474
+ "X [ERROR] Missing script name
319
475
 
320
- %s
321
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
476
+ "
322
477
  `);
323
478
  expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
324
479
  });