-
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
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import {
assertEquals,
assertRejects,
} from "../../deps/deno.land/std/assert/mod.ts";
import { DenoFsWriter } from "./deno_fs.ts";
const root = new URL("./.test/", import.meta.url);
const writePermission = await Deno.permissions.query({
name: "write",
path: root,
});
// Read permission is also required in order to check the output file.
const readPermission = await Deno.permissions.query({
name: "read",
path: root,
});
Deno.test("Should write a file", {
// Skip this test if write permission is not granted.
// Without this, simple `deno test` would fail or prompt permissions, which is annoying.
ignore: writePermission.state !== "granted" ||
readPermission.state !== "granted",
}, async () => {
await Deno.mkdir(root, { recursive: true });
try {
const writer = new DenoFsWriter(root);
const content = new TextEncoder().encode("Hello, World!\n");
await writer.write(["foo", "bar.txt"], content);
assertEquals(
await Deno.readTextFile(new URL("foo/bar.txt", root)),
"Hello, World!\n",
);
} finally {
await Deno.remove(root, { recursive: true });
}
});
Deno.test("Should abort on attempt to write different content at same path", {
// Skip this test if write permission is not granted.
// Without this, simple `deno test` would fail or prompt permissions, which is annoying.
ignore: writePermission.state !== "granted" ||
readPermission.state !== "granted",
}, async () => {
await Deno.mkdir(root, { recursive: true });
try {
const writer = new DenoFsWriter(root);
const enc = new TextEncoder();
await writer.write(["hash-test", "foo.txt"], enc.encode("Foo"));
await assertRejects(() =>
writer.write(["hash-test", "foo.txt"], enc.encode("Bar"))
);
} finally {
await Deno.remove(root, { recursive: true });
}
});