/
test.js
131 lines (110 loc) · 4.5 KB
/
test.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import fs from 'fs';
import path from 'path';
import isJpg from 'is-jpg';
import pathExists from 'path-exists';
import pify from 'pify';
import test from 'ava';
import m from '.';
const fsP = pify(fs);
test('extract file', async t => {
const tarFiles = await m(path.join(__dirname, 'fixtures', 'file.tar'));
const tarbzFiles = await m(path.join(__dirname, 'fixtures', 'file.tar.bz2'));
const targzFiles = await m(path.join(__dirname, 'fixtures', 'file.tar.gz'));
const zipFiles = await m(path.join(__dirname, 'fixtures', 'file.zip'));
t.is(tarFiles[0].path, 'test.jpg');
t.true(isJpg(tarFiles[0].data));
t.is(tarbzFiles[0].path, 'test.jpg');
t.true(isJpg(tarbzFiles[0].data));
t.is(targzFiles[0].path, 'test.jpg');
t.true(isJpg(targzFiles[0].data));
t.is(zipFiles[0].path, 'test.jpg');
t.true(isJpg(zipFiles[0].data));
});
test('extract file using buffer', async t => {
const tarBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar'));
const tarFiles = await m(tarBuf);
const tarbzBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar.bz2'));
const tarbzFiles = await m(tarbzBuf);
const targzBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar.gz'));
const targzFiles = await m(targzBuf);
const zipBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.zip'));
const zipFiles = await m(zipBuf);
t.is(tarFiles[0].path, 'test.jpg');
t.is(tarbzFiles[0].path, 'test.jpg');
t.is(targzFiles[0].path, 'test.jpg');
t.is(zipFiles[0].path, 'test.jpg');
});
test.serial('extract file to directory', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), __dirname);
t.is(files[0].path, 'test.jpg');
t.true(isJpg(files[0].data));
t.true(await pathExists(path.join(__dirname, 'test.jpg')));
await fsP.unlink(path.join(__dirname, 'test.jpg'));
});
test('extract symlink', async t => {
await m(path.join(__dirname, 'fixtures', 'symlink.tar'), __dirname, {strip: 1});
t.is(await fsP.realpath(path.join(__dirname, 'symlink')), path.join(__dirname, 'file.txt'));
await fsP.unlink(path.join(__dirname, 'symlink'));
await fsP.unlink(path.join(__dirname, 'file.txt'));
});
test('extract directory', async t => {
await m(path.join(__dirname, 'fixtures', 'directory.tar'), __dirname);
t.true(await pathExists(path.join(__dirname, 'directory')));
await fsP.rmdir(path.join(__dirname, 'directory'));
});
test('strip option', async t => {
const zipFiles = await m(path.join(__dirname, 'fixtures', 'strip.zip'), {strip: 1});
const tarFiles = await m(path.join(__dirname, 'fixtures', 'strip.tar'), {strip: 1});
t.is(zipFiles[0].path, 'test-strip.jpg');
t.true(isJpg(zipFiles[0].data));
t.is(tarFiles[0].path, 'test-strip.jpg');
t.true(isJpg(tarFiles[0].data));
});
test('filter option', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), {
filter: x => x.path !== 'test.jpg'
});
t.is(files.length, 0);
});
test('map option', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), {
map: x => {
x.path = `unicorn-${x.path}`;
return x;
}
});
t.is(files[0].path, 'unicorn-test.jpg');
});
test.serial('set mtime', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), __dirname);
const stat = await fsP.stat(path.join(__dirname, 'test.jpg'));
t.deepEqual(files[0].mtime, stat.mtime);
await fsP.unlink(path.join(__dirname, 'test.jpg'));
});
test('return emptpy array if no plugins are set', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), {plugins: []});
t.is(files.length, 0);
});
test('throw when a location outside the root is given', async t => {
await t.throwsAsync(async () => {
await m(path.join(__dirname, 'fixtures', 'slipping.tar.gz'), 'dist');
}, {message: /Refusing to write/});
});
test('throw when a location outside the root including symlinks is given', async t => {
await t.throwsAsync(async () => {
await m(path.join(__dirname, 'fixtures', 'slip.zip'), 'dist');
}, {message: /Refusing to write/});
});
test('allows filenames and directories to be written with dots in their names', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'edge_case_dots.tar.gz'), __dirname);
t.is(files.length, 6);
t.deepEqual(files.map(f => f.path).sort(), [
'edge_case_dots/',
'edge_case_dots/internal_dots..txt',
'edge_case_dots/sample../',
'edge_case_dots/ending_dots..',
'edge_case_dots/x',
'edge_case_dots/sample../test.txt'
].sort());
await fsP.rmdir(path.join(__dirname, 'edge_case_dots'), {recursive: true});
});