forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
r2.local.test.ts
121 lines (105 loc) · 4.13 KB
/
r2.local.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import * as fs from "node:fs";
import { vi } from "vitest";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
// Miniflare's use of undici doesn't play well with jest-mock-fetch
// and it is not needed here anyway.
vi.unmock("undici");
describe("r2", () => {
const std = mockConsoleMethods();
runInTempDir();
describe("r2 object", () => {
describe("local", () => {
it("should put R2 object from local bucket", async () => {
await expect(() =>
runWrangler(
`r2 object get bucketName-object-test/wormhole-img.png --file ./wormhole-img.png --local`
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The specified key does not exist.]`
);
fs.writeFileSync("wormhole-img.png", "passageway");
await runWrangler(
`r2 object put bucketName-object-test/wormhole-img.png --file ./wormhole-img.png --local`
);
expect(std.out).toMatchInlineSnapshot(`
"Downloading \\"wormhole-img.png\\" from \\"bucketName-object-test\\".
Creating object \\"wormhole-img.png\\" in bucket \\"bucketName-object-test\\".
Upload complete."
`);
await runWrangler(
`r2 object get bucketName-object-test/wormhole-img.png --file ./wormhole-img.png --local`
);
expect(std.out).toMatchInlineSnapshot(`
"Downloading \\"wormhole-img.png\\" from \\"bucketName-object-test\\".
Creating object \\"wormhole-img.png\\" in bucket \\"bucketName-object-test\\".
Upload complete.
Downloading \\"wormhole-img.png\\" from \\"bucketName-object-test\\".
Download complete."
`);
});
it("should delete R2 object from local bucket", async () => {
fs.writeFileSync("wormhole-img.png", "passageway");
await runWrangler(
`r2 object put bucketName-object-test/wormhole-img.png --file ./wormhole-img.png --local`
);
await runWrangler(
`r2 object get bucketName-object-test/wormhole-img.png --file ./wormhole-img.png --local`
);
expect(std.out).toMatchInlineSnapshot(`
"Creating object \\"wormhole-img.png\\" in bucket \\"bucketName-object-test\\".
Upload complete.
Downloading \\"wormhole-img.png\\" from \\"bucketName-object-test\\".
Download complete."
`);
await runWrangler(
`r2 object delete bucketName-object-test/wormhole-img.png --local`
);
expect(std.out).toMatchInlineSnapshot(`
"Creating object \\"wormhole-img.png\\" in bucket \\"bucketName-object-test\\".
Upload complete.
Downloading \\"wormhole-img.png\\" from \\"bucketName-object-test\\".
Download complete.
Deleting object \\"wormhole-img.png\\" from bucket \\"bucketName-object-test\\".
Delete complete."
`);
await expect(() =>
runWrangler(
`r2 object get bucketName-object-test/wormhole-img.png --file ./wormhole-img.png --local`
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The specified key does not exist.]`
);
});
it("should follow persist-to for object bucket", async () => {
fs.writeFileSync("wormhole-img.png", "passageway");
await runWrangler(
`r2 object put bucketName-object-test/file-one --file ./wormhole-img.png --local`
);
await runWrangler(
`r2 object put bucketName-object-test/file-two --file ./wormhole-img.png --local --persist-to ./different-dir`
);
await expect(() =>
runWrangler(
`r2 object get bucketName-object-test/file-one --file ./wormhole-img.png --local --persist-to ./different-dir`
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: The specified key does not exist.]`
);
await runWrangler(
`r2 object get bucketName-object-test/file-two --file ./wormhole-img.png --local --persist-to ./different-dir`
);
expect(std.out).toMatchInlineSnapshot(`
"Creating object \\"file-one\\" in bucket \\"bucketName-object-test\\".
Upload complete.
Creating object \\"file-two\\" in bucket \\"bucketName-object-test\\".
Upload complete.
Downloading \\"file-one\\" from \\"bucketName-object-test\\".
Downloading \\"file-two\\" from \\"bucketName-object-test\\".
Download complete."
`);
});
});
});
});