1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-04 08:54:20 -05:00
denoland-deno/std/archive/tar_test.ts
Bartek Iwańczuk 8feb30e325
BREAKING: remove overload of Deno.test() (#4951)
This commit removes overload of Deno.test() that accepted named
function.
2020-04-28 12:33:09 +02:00

90 lines
2.5 KiB
TypeScript

/**
* 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 { assertEquals } from "../testing/asserts.ts";
import { resolve } from "../path/mod.ts";
import { Tar, Untar } from "./tar.ts";
const filePath = resolve("archive", "testdata", "example.txt");
Deno.test("createTarArchive", async function (): Promise<void> {
// initialize
const tar = new Tar();
// put data on memory
const content = new TextEncoder().encode("hello tar world!");
await tar.append("output.txt", {
reader: new Deno.Buffer(content),
contentSize: content.byteLength,
});
// put a file
await tar.append("dir/tar.ts", { filePath });
// write tar data to a buffer
const writer = new Deno.Buffer();
const wrote = await Deno.copy(tar.getReader(), writer);
/**
* 3072 = 512 (header) + 512 (content) + 512 (header) + 512 (content)
* + 1024 (footer)
*/
assertEquals(wrote, 3072);
});
Deno.test("deflateTarArchive", async function (): Promise<void> {
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 Deno.Buffer(content),
contentSize: content.byteLength,
});
// read data from a tar archive
const untar = new Untar(tar.getReader());
const buf = new Deno.Buffer();
const result = await untar.extract(buf);
const untarText = new TextDecoder("utf-8").decode(buf.bytes());
// tests
assertEquals(result.fileName, fileName);
assertEquals(untarText, text);
});
Deno.test("appendFileWithLongNameToTarArchive", async function (): Promise<
void
> {
// 9 * 15 + 13 = 148 bytes
const fileName = new Array(10).join("long-file-name/") + "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 Deno.Buffer(content),
contentSize: content.byteLength,
});
// read data from a tar archive
const untar = new Untar(tar.getReader());
const buf = new Deno.Buffer();
const result = await untar.extract(buf);
const untarText = new TextDecoder("utf-8").decode(buf.bytes());
// tests
assertEquals(result.fileName, fileName);
assertEquals(untarText, text);
});