-
Notifications
You must be signed in to change notification settings - Fork 52
/
test.js
160 lines (135 loc) · 5.59 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import fs from 'fs';
import path from 'path';
import isJpg from 'is-jpg';
import pathExists from 'path-exists';
import pify from 'pify';
import rimraf from 'rimraf';
import test from 'ava';
import m from '.';
const fsP = pify(fs);
const rimrafP = pify(rimraf);
test.serial.afterEach('ensure decompressed files and directories are cleaned up', async () => {
await rimrafP(path.join(__dirname, 'directory'));
await rimrafP(path.join(__dirname, 'dist'));
await rimrafP(path.join(__dirname, 'example.txt'));
await rimrafP(path.join(__dirname, 'file.txt'));
await rimrafP(path.join(__dirname, 'edge_case_dots'));
await rimrafP(path.join(__dirname, 'symlink'));
await rimrafP(path.join(__dirname, 'test.jpg'));
});
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')));
});
test.serial('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'));
});
test.serial('extract directory', async t => {
await m(path.join(__dirname, 'fixtures', 'directory.tar'), __dirname);
t.true(await pathExists(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);
});
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.serial('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/});
});
test.serial('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/});
});
test.serial('throw when a top-level symlink outside the root is given', async t => {
await t.throwsAsync(async () => {
await m(path.join(__dirname, 'fixtures', 'slip2.zip'), 'dist');
}, {message: /Refusing/});
});
test.serial('throw when a directory outside the root including symlinks is given', async t => {
await t.throwsAsync(async () => {
await m(path.join(__dirname, 'fixtures', 'slipping_directory.tar.gz'), 'dist');
}, {message: /Refusing/});
});
test.serial('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());
});
test.serial('allows top-level file', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'top_level_example.tar.gz'), 'dist');
t.is(files.length, 1);
t.is(files[0].path, 'example.txt');
});
test.serial('throw when chained symlinks to /tmp/dist allow escape outside root directory', async t => {
await t.throwsAsync(async () => {
await m(path.join(__dirname, 'fixtures', 'slip3.zip'), '/tmp/dist');
}, {message: /Refusing/});
});