-
Notifications
You must be signed in to change notification settings - Fork 16
/
test.ts
77 lines (60 loc) · 2.11 KB
/
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
import { decode, encode } from "https://deno.land/[email protected]/encoding/utf8.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { JSZip, readZip, zipDir } from "./mod.ts";
// FIXME use tmp directory and clean up.
async function exampleZip(path: string) {
const zip = new JSZip();
zip.addFile("Hello.txt", "Hello World\n");
const img = zip.folder("images");
img.addFile("smile.gif", "\0", { base64: true });
await zip.writeZip(path);
}
async function fromDir<T>(dir: string, f: () => Promise<T>) {
const cwd = Deno.cwd();
Deno.chdir(dir);
try {
return await f();
} finally {
Deno.chdir(cwd);
}
}
async function exampleDir(): Promise<string> {
const dir = await Deno.makeTempDir();
await fromDir(dir, async () => {
await Deno.writeFile("Hello.txt", encode("Hello World\n"));
await Deno.mkdir("images");
Deno.chdir("images");
await Deno.writeFile("smile.gif", encode("\0"));
});
return dir;
}
Deno.test("read", async () => {
await exampleZip("example.zip");
const z = await readZip("example.zip");
assertEquals(z.file("Hello.txt").name, "Hello.txt");
let i = 0;
for (const f of z) i++;
assertEquals(i, 3);
assertEquals("Hello World\n", await z.file("Hello.txt").async("string"));
await Deno.remove("example.zip");
});
// TODO add tests for unzip
Deno.test("dir", async () => {
const dir = await exampleDir();
const z = await zipDir(dir);
assertEquals(z.file("Hello.txt").name, "Hello.txt");
assertEquals("Hello World\n", await z.file("Hello.txt").async("string"));
const img = z.folder("images");
assertEquals(img.file("smile.gif").name, "images/smile.gif");
});
Deno.test("unzip", async () => {
const dir = await Deno.makeTempDir();
await exampleZip("example.zip");
const z = await readZip("example.zip");
await z.unzip(dir);
const content = await Deno.readFile(join(dir, "Hello.txt"));
assertEquals("Hello World\n", decode(content));
const smile = await Deno.readFile(join(dir, "images", "smile.gif"));
assertEquals("", decode(smile));
});