-
Notifications
You must be signed in to change notification settings - Fork 0
/
tar-extractor.test.ts
98 lines (81 loc) · 3.05 KB
/
tar-extractor.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { jest, describe, expect, test } from '@jest/globals';
import { TarExtractor } from './tar-extractor';
import { listTestTarCases, toPrintableString } from './utils.test';
jest.mock('react-native-fs');
describe('TarExtractor', () => {
const testDirectory = './test.d/';
listTestTarCases().forEach(tc => {
describe(tc.filename, () => {
test('list all files', async () => {
const tarExtractor = new TarExtractor();
const extractedFiles: [string, number][] = [];
await tarExtractor.read(tc.tarFilePath, async file => {
extractedFiles.push([file.header.name, file.header.size]);
return true; // Continue reading all files
});
expect(extractedFiles).toEqual(
tc.expectedFiles.map(([name, size]) => [name, size] as [string, number]),
);
});
test('read all files', async () => {
const tarExtractor = new TarExtractor();
const extractedFiles: [string, number, string][] = [];
await tarExtractor.read(tc.tarFilePath, async file => {
extractedFiles.push([
file.header.name,
file.header.size,
toPrintableString(await file.read()),
]);
return true; // Continue reading all files
});
expect(extractedFiles).toEqual(tc.expectedFiles);
});
test('read 3 files', async () => {
const tarExtractor = new TarExtractor();
const extractedFiles: [string, number, string][] = [];
let idx = 0;
await tarExtractor.read(tc.tarFilePath, async file => {
extractedFiles.push([
file.header.name,
file.header.size,
toPrintableString(await file.read()),
]);
return ++idx < 3; // Stop reading after 3 files
});
expect(extractedFiles).toEqual(tc.expectedFiles.slice(0, 3));
});
test('read 1st file', async () => {
const tarExtractor = new TarExtractor();
const extractedFiles: [string, number, string][] = [];
let idx = 0;
await tarExtractor.read(tc.tarFilePath, async file => {
if (idx === 0) {
extractedFiles.push([
file.header.name,
file.header.size,
toPrintableString(await file.read()),
]);
}
return ++idx < 1; // Stop reading after the 2nd file
});
expect(extractedFiles).toEqual(tc.expectedFiles.slice(0, 1));
});
test('read 2nd file', async () => {
const tarExtractor = new TarExtractor();
const extractedFiles: [string, number, string][] = [];
let idx = 0;
await tarExtractor.read(tc.tarFilePath, async file => {
if (idx === 1) {
extractedFiles.push([
file.header.name,
file.header.size,
toPrintableString(await file.read()),
]);
}
return ++idx < 2; // Stop reading after the 2nd file
});
expect(extractedFiles).toEqual(tc.expectedFiles.slice(1, 2));
});
});
});
});