deno.land / std@0.224.0 / archive / tar_test.ts

tar_test.ts
View Documentation
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
122
123
124
125
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license./** * Tar test * * **test summary** * - create a tar archive in memory containing output.txt and dir/tar.ts. * - read and deflate a tar archive containing output.txt * * **to run this test** * deno run --allow-read archive/tar_test.ts */import { assert, assertEquals } from "../assert/mod.ts";import { resolve } from "../path/mod.ts";import { Tar } from "./tar.ts";import { Untar } from "./untar.ts";import { Buffer } from "../io/buffer.ts";import { copy } from "../io/copy.ts";import { readAll } from "../io/read_all.ts";import { filePath, testdataDir } from "./_test_common.ts";
Deno.test("createTarArchive", async function () { // initialize const tar = new Tar();
// put data on memory const content = new TextEncoder().encode("hello tar world!"); await tar.append("output.txt", { reader: new Buffer(content), contentSize: content.byteLength, });
// put a file await tar.append("dir/tar.ts", { filePath });
// write tar data to a buffer const writer = new Buffer(); const wrote = await copy(tar.getReader(), writer);
/** * 3072 = 512 (header) + 512 (content) + 512 (header) + 512 (content) * + 1024 (footer) */ assertEquals(wrote, 3072);});
Deno.test("Tar() deflates tar archive", async function () { const fileName = "output.txt"; const text = "hello tar world!";
// create a tar archive const tar = new Tar(); const content = new TextEncoder().encode(text); await tar.append(fileName, { reader: new Buffer(content), contentSize: content.byteLength, });
// read data from a tar archive const untar = new Untar(tar.getReader()); const result = await untar.extract(); assert(result !== null); const untarText = new TextDecoder("utf-8").decode(await readAll(result));
assertEquals(await untar.extract(), null); // EOF // tests assertEquals(result.fileName, fileName); assertEquals(untarText, text);});
Deno.test("Tar() appends file with long name to tar archive", async function (): Promise< void> { // 9 * 15 + 13 = 148 bytes const fileName = "long-file-name/".repeat(10) + "file-name.txt"; const text = "hello tar world!";
// create a tar archive const tar = new Tar(); const content = new TextEncoder().encode(text); await tar.append(fileName, { reader: new Buffer(content), contentSize: content.byteLength, });
// read data from a tar archive const untar = new Untar(tar.getReader()); const result = await untar.extract(); assert(result !== null); assert(!result.consumed); const untarText = new TextDecoder("utf-8").decode(await readAll(result)); assert(result.consumed);
// tests assertEquals(result.fileName, fileName); assertEquals(untarText, text);});
Deno.test("Tar() checks directory entry type", async function () { const tar = new Tar();
await tar.append("directory/", { reader: new Buffer(), contentSize: 0, type: "directory", });
const filePath = resolve(testdataDir); await tar.append("archive/testdata/", { filePath, });
const outputFile = resolve(testdataDir, "directory_type_test.tar"); using file = await Deno.open(outputFile, { create: true, write: true }); await copy(tar.getReader(), file);
using reader = await Deno.open(outputFile, { read: true }); const untar = new Untar(reader); await Array.fromAsync( untar, (entry) => assertEquals(entry.type, "directory"), );
await Deno.remove(outputFile);});
std

Version Info

Tagged at
6 months ago