diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-10-09 17:10:09 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-10-09 17:10:09 -0400 |
commit | 151ce0266eb4de2c8fc600c81c192a5f791b6169 (patch) | |
tree | 7cb04016a1c7ee88adde83814548d7a9409dcde3 /std/archive/tar_test.ts | |
parent | a355f7c807686918734416d91b79c26c21effba9 (diff) |
Move everything into std subdir
Diffstat (limited to 'std/archive/tar_test.ts')
-rw-r--r-- | std/archive/tar_test.ts | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/std/archive/tar_test.ts b/std/archive/tar_test.ts new file mode 100644 index 000000000..474e1e2d3 --- /dev/null +++ b/std/archive/tar_test.ts @@ -0,0 +1,91 @@ +/** + * 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 { test, runIfMain } from "../testing/mod.ts"; +import { assertEquals } from "../testing/asserts.ts"; + +import { Tar, Untar } from "./tar.ts"; +import { resolve } from "../fs/path/mod.ts"; + +const filePath = resolve("archive", "testdata", "example.txt"); + +test(async function createTarArchive(): 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(), + wrote = await Deno.copy(writer, tar.getReader()); + + /** + * 3072 = 512 (header) + 512 (content) + 512 (header) + 512 (content) + * + 1024 (footer) + */ + assertEquals(wrote, 3072); +}); + +test(async function deflateTarArchive(): 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); +}); + +test(async function appendFileWithLongNameToTarArchive(): 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); +}); + +runIfMain(import.meta); |