Skip to content

Commit

Permalink
test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangfuxing committed Nov 6, 2024
1 parent 251423b commit 57c542b
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 31 deletions.
15 changes: 15 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { EventEmitter } from "node:events";
export { ensureDir, ensureFile } from "jsr:@std/[email protected]";
export { exists, ensureDir } from "jsr:@std/[email protected]";
export * as path from "jsr:@std/[email protected]";
export { Buffer, copy, readAll, writeAll } from "jsr:@std/[email protected]";
export { crc32, Crc32Stream } from "jsr:@deno-library/[email protected]";
Expand Down
10 changes: 4 additions & 6 deletions tar/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { path, ensureFile } from "../deps.ts";
import { path, exists } from "../deps.ts";
import { UntarStream } from "../deps.ts";
import { TarStream, type TarStreamInput } from "../deps.ts";

Expand All @@ -14,16 +14,14 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await ensureFile(src);
using srcFile = await Deno.open(src);
await exists(src, { isFile: true });
for await (
const entry of srcFile.readable.pipeThrough(new UntarStream())
const entry of (await Deno.open(src)).readable.pipeThrough(new UntarStream())
) {
const filePath = path.resolve(dest, entry.path);
if (options?.debug) console.log(filePath);
await Deno.mkdir(path.dirname(filePath), { recursive: true });
using destFile = await Deno.create(filePath);
await entry.readable?.pipeTo(destFile.writable);
await entry.readable?.pipeTo((await Deno.create(filePath)).writable);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tar_archive/mod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Buffer, copy, ensureDir, path, ensureFile } from "../deps.ts";
import { Buffer, copy, ensureDir, exists, path } from "../deps.ts";
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { Tar } from "jsr:@std/[email protected]/tar";
import { Untar } from "jsr:@std/[email protected]/untar";
Expand All @@ -14,7 +14,7 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await ensureFile(src);
await exists(src, { isFile: true });
using reader = await Deno.open(src, { read: true });
const untar = new Untar(reader);
for await (const entry of untar) {
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 4 additions & 7 deletions test/tar.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
assert,
assertEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { assert, assertEquals } from "jsr:@std/assert";
import { tar } from "../mod.ts";

Deno.test("tar.compress file", async () => {
const src = "./test/tar/tar.txt";
const src = "./test/dir/root.txt";
const dest = "./test.tar";
try {
await tar.compress(src, dest, { debug: true });
Expand All @@ -22,7 +19,7 @@ Deno.test("tar.compress file", async () => {
});

Deno.test("tar.compress folder", async () => {
const src = "./test/tar";
const src = "./test/dir";
const dest = "./test.tar";
try {
await tar.compress(src, dest, { debug: true });
Expand All @@ -49,7 +46,7 @@ Deno.test("tar.uncompress", async () => {
const landTxtSize = 5;
const landTxtContent = "land\n";
try {
await tar.uncompress(src, dest);
await tar.uncompress(src, dest, { debug: true });
const stat = await Deno.lstat(landTxtPath);
assertEquals(stat.size, landTxtSize);
const buf = await Deno.readFile(landTxtPath);
Expand Down
30 changes: 30 additions & 0 deletions test/zip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { assert, assertEquals } from "jsr:@std/assert";
import { zip } from "../mod.ts";

Deno.test("zip.compress file", async () => {
const src = "./test/dir/root.txt";
const dest = "./test.zip";
try {
await zip.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
assertEquals(stat.size, 255);
await Deno.remove(dest);
} catch (error) {
console.error(error);
assert(false);
}
});

Deno.test("zip.compress folder", async () => {
const src = "./test/dir";
const dest = "./deno.zip";
try {
await zip.compress(src, dest, { debug: true });
const stat = await Deno.lstat(dest);
assertEquals(stat.size, 943);
await Deno.remove(dest);
} catch (error) {
console.error(error);
assert(false);
}
});
15 changes: 6 additions & 9 deletions tgz/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { path, ensureFile } from "../deps.ts";
import { exists, path } from "../deps.ts";
import { UntarStream } from "../deps.ts";
import { TarStream, type TarStreamInput } from "../deps.ts";

Expand All @@ -22,19 +22,16 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await ensureFile(src);
using srcFile = await Deno.open(src);
await exists(src, { isFile: true });
for await (
const entry of srcFile
.readable
.pipeThrough(new DecompressionStream("gzip"))
.pipeThrough(new UntarStream())
const entry of (await Deno.open(src)).readable.pipeThrough(
new DecompressionStream("gzip"),
).pipeThrough(new UntarStream())
) {
const filePath = path.resolve(dest, entry.path);
if (options?.debug) console.log(filePath);
await Deno.mkdir(path.dirname(filePath), { recursive: true });
using destFile = await Deno.create(filePath);
await entry.readable?.pipeTo(destFile.writable);
await entry.readable?.pipeTo((await Deno.create(filePath)).writable);
}
}

Expand Down
4 changes: 2 additions & 2 deletions zip/mod.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { ensureFile, path } from "../deps.ts";
import { path, exists } from "../deps.ts";
import {
type EntryMetaData,
terminateWorkers,
Expand All @@ -18,7 +18,7 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await ensureFile(src);
await exists(src, { isFile: true });
for await (
const entry of (await Deno.open(src))
.readable
Expand Down
7 changes: 3 additions & 4 deletions zip_archive/archive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { compressInterface, uncompressInterface } from "../interface.ts";
import { ensureFile, path } from "../deps.ts";
import { path, exists } from "../deps.ts";
import {
terminateWorkers,
ZipReader,
Expand All @@ -17,7 +17,7 @@ export async function uncompress(
dest: string,
options?: uncompressInterface,
): Promise<void> {
await ensureFile(src);
await exists(src, { isFile: true });
using srcFile = await Deno.open(src);
const zipReader = new ZipReader(srcFile);
try {
Expand All @@ -27,8 +27,7 @@ export async function uncompress(
if (options?.debug) console.log(filePath);
await Deno.mkdir(path.dirname(filePath), { recursive: true });
if (entry.directory || !entry.getData) continue;
using destFile = await Deno.create(filePath);
await entry.getData(destFile.writable);
await entry.getData((await Deno.create(filePath)).writable);
}
} finally {
await zipReader.close();
Expand Down

0 comments on commit 57c542b

Please sign in to comment.