-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
89 lines (78 loc) · 2.17 KB
/
index.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
import { jest, describe, expect, test } from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import { findFile, listFiles, readTar } from './index';
import { listTestTarCases, toPrintableString } from './utils.test';
jest.mock('react-native-fs');
describe('listFiles', () => {
listTestTarCases().forEach(tc => {
test(tc.filename, async () => {
expect(
await listFiles(tc.tarFilePath),
).toEqual(
tc.expectedFiles.map(([name]) => name),
);
});
});
});
describe('findFile', () => {
const tarFilePath = './test.d/01-simple.tar';
test('exact match', async () => {
expect(
await findFile(tarFilePath, '3.txt').then(async f => [
f.header.name,
f.header.size,
(await f.read()).toString('utf8'),
]),
).toEqual([
'3.txt',
5,
'three',
]);
});
test('exact match w/ directory', async () => {
expect(
await findFile(tarFilePath, 'directory/2.txt').then(async f => [
f.header.name,
f.header.size,
]),
).toEqual(
['directory/2.txt', 3],
);
});
test('regexp match', async () => {
expect(
await findFile(tarFilePath, /\.json$/).then(async f => [
f.header.name,
f.header.size,
(await f.read()).toString('utf8'),
]),
).toEqual([
'object.json',
16,
'{"prop":"value"}',
]);
});
test('exact mismatch', async () => {
await expect(findFile(tarFilePath, '9.txt')).rejects.toThrowError('File not found: 9.txt');
});
test('regexp mismatch', async () => {
await expect(findFile(tarFilePath, /qwe\.zip$/)).rejects.toThrowError('File not found: /qwe\\.zip$/');
});
});
describe('readTar', () => {
listTestTarCases().forEach(tc => {
test(tc.filename, async () => {
const extractedFiles: [string, number, string][] = [];
await readTar(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);
});
});
});