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(async function createTarArchive(): Promise<void> { const tar = new Tar();
const content = new TextEncoder().encode("hello tar world!"); await tar.append("output.txt", { reader: new Deno.Buffer(content), contentSize: content.byteLength });
await tar.append("dir/tar.ts", { filePath });
const writer = new Deno.Buffer(), wrote = await Deno.copy(writer, tar.getReader());
assertEquals(wrote, 3072);});
Deno.test(async function deflateTarArchive(): Promise<void> { const fileName = "output.txt"; const text = "hello tar world!";
const tar = new Tar(); const content = new TextEncoder().encode(text); await tar.append(fileName, { reader: new Deno.Buffer(content), contentSize: content.byteLength });
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());
assertEquals(result.fileName, fileName); assertEquals(untarText, text);});
Deno.test(async function appendFileWithLongNameToTarArchive(): Promise<void> { const fileName = new Array(10).join("long-file-name/") + "file-name.txt"; const text = "hello tar world!";
const tar = new Tar(); const content = new TextEncoder().encode(text); await tar.append(fileName, { reader: new Deno.Buffer(content), contentSize: content.byteLength });
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());
assertEquals(result.fileName, fileName); assertEquals(untarText, text);});