Skip to content

Commit f13b802

Browse files
committed
test: add additional tests for different formats
#29
1 parent 7e35c5b commit f13b802

File tree

9 files changed

+96
-29
lines changed

9 files changed

+96
-29
lines changed

test/create.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { execSync } from "node:child_process";
2+
import { describe, expect, it } from "vitest";
3+
import { createTarGzip, TarFileItem } from "../src";
4+
5+
const mtime = 1_700_000_000_000;
6+
7+
const fixture: TarFileItem<any>[] = [
8+
{ name: "hello.txt", data: "Hello World!", attrs: { mtime } },
9+
{ name: "test", attrs: { mtime, uid: 1001, gid: 1001 } },
10+
{ name: "foo/bar.txt", data: "Hello World!", attrs: { mtime } },
11+
];
12+
13+
describe("create", () => {
14+
it("createTarGzip", async () => {
15+
const data = await createTarGzip(fixture);
16+
expect(data).toBeInstanceOf(Uint8Array);
17+
18+
const out = execSync("tar -tzvf-", { input: data })
19+
.toString()
20+
.split("\n")
21+
.map((l) => {
22+
// other columns might be insconsistent between platforms
23+
const parts = l.trim().split(/\s+/);
24+
const mod = parts[0];
25+
const name = parts.at(-1);
26+
return `${mod} ${name}`;
27+
})
28+
.join("\n");
29+
30+
expect(out).toMatchInlineSnapshot(`
31+
"-rw-rw-r-- hello.txt
32+
drwxrwxr-x test
33+
-rw-rw-r-- foo/bar.txt
34+
"
35+
`);
36+
});
37+
});

test/fixtures/gen.bash

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
3+
4+
cd "$(dirname "$0")" || exit 1
5+
6+
create_tar() {
7+
local format=$1
8+
local ext=$2
9+
local options=$3
10+
tar $options -cf "out/${format}.tar" -C in .
11+
if [[ $? -eq 0 ]]; then
12+
echo "Created: out/${format}.tar"
13+
else
14+
echo "Failed!: out/${format}.tar"
15+
fi
16+
}
17+
18+
# V7 (Original TAR format)
19+
create_tar "v7" "tar" "--format=v7"
20+
21+
# USTAR (POSIX 1988)
22+
create_tar "ustar" "tar" "--format=ustar"
23+
24+
# GNU TAR (Linux standard)
25+
create_tar "gnu" "tar" "--format=gnu"
26+
27+
# PAX TAR (POSIX 2001)
28+
create_tar "pax" "tar" "--format=pax"
29+
30+
# STAR TAR (Schily TAR - not always available, fallback to pax)
31+
create_tar "star" "tar" "--format=star"
32+

test/fixtures/in/bar/baz.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
baz.txt

test/fixtures/in/foo.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo.txt

test/fixtures/out/gnu.tar

10 KB
Binary file not shown.

test/fixtures/out/pax.tar

10 KB
Binary file not shown.

test/fixtures/out/ustar.tar

10 KB
Binary file not shown.

test/fixtures/out/v7.tar

10 KB
Binary file not shown.

test/index.test.ts test/parse.test.ts

+25-29
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { execSync } from "node:child_process";
21
import { expect, it, describe } from "vitest";
32
import { inspect } from "node:util";
4-
import { createTarGzip, parseTarGzip, TarFileItem } from "../src";
3+
import { createTarGzip, parseTar, parseTarGzip, TarFileItem } from "../src";
4+
import { readFile } from "node:fs/promises";
55

66
const mtime = 1_700_000_000_000;
77

@@ -11,32 +11,8 @@ const fixture: TarFileItem<any>[] = [
1111
{ name: "foo/bar.txt", data: "Hello World!", attrs: { mtime } },
1212
];
1313

14-
describe("nanotar", () => {
15-
it("createTar", async () => {
16-
const data = await createTarGzip(fixture);
17-
expect(data).toBeInstanceOf(Uint8Array);
18-
19-
const out = execSync("tar -tzvf-", { input: data })
20-
.toString()
21-
.split("\n")
22-
.map((l) => {
23-
// other columns might be insconsistent between platforms
24-
const parts = l.trim().split(/\s+/);
25-
const mod = parts[0];
26-
const name = parts.at(-1);
27-
return `${mod} ${name}`;
28-
})
29-
.join("\n");
30-
31-
expect(out).toMatchInlineSnapshot(`
32-
"-rw-rw-r-- hello.txt
33-
drwxrwxr-x test
34-
-rw-rw-r-- foo/bar.txt
35-
"
36-
`);
37-
});
38-
39-
it("parseTar", async () => {
14+
describe("parse", () => {
15+
it("parseTarGzip", async () => {
4016
const data = await createTarGzip(fixture);
4117
const files = (await parseTarGzip(data)).map((f) => ({
4218
...f,
@@ -93,7 +69,7 @@ describe("nanotar", () => {
9369
`);
9470
});
9571

96-
it("parseTar (with filter)", async () => {
72+
it("parseTarGzip (with filter)", async () => {
9773
const data = await createTarGzip(fixture);
9874
const files = (
9975
await parseTarGzip(data, {
@@ -105,4 +81,24 @@ describe("nanotar", () => {
10581
}));
10682
expect(files.map((f) => f.name)).toMatchObject(["foo/bar.txt"]);
10783
});
84+
85+
describe("parse different formats", async () => {
86+
const formats = ["gnu", "pax", "ustar", "v7"];
87+
88+
for (const format of formats) {
89+
it.skipIf(format === "pax")(`parseTar (${format})`, async () => {
90+
const blob = await readFile(
91+
new URL(`fixtures/out/${format}.tar`, import.meta.url),
92+
);
93+
const parsed = await parseTar(blob);
94+
const names = parsed.map((f) => f.name);
95+
expect(names).toMatchObject([
96+
"./",
97+
"./foo.txt",
98+
"./bar/",
99+
"./bar/baz.txt",
100+
]);
101+
});
102+
}
103+
});
108104
});

0 commit comments

Comments
 (0)