-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
test.js
289 lines (232 loc) · 7.78 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import {Buffer} from 'node:buffer';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import Vinyl from 'vinyl';
import unzip from 'decompress-unzip';
import vinylAssign from 'vinyl-assign';
import {vinylFileSync} from 'vinyl-file';
import yazl from 'yazl';
import {pEvent} from 'p-event';
import easyTransformStream from 'easy-transform-stream';
import zip from './index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
test('should zip files', async t => {
const zipStream = zip('test.zip');
const unzipStream = unzip();
const stats = fs.statSync(path.join(__dirname, 'fixture/fixture.txt'));
const files = [];
const finalStream = zipStream.pipe(vinylAssign({extract: true})).pipe(unzipStream);
const promise = pEvent(finalStream, 'end');
unzipStream.on('data', file => {
files.push(file);
});
zipStream.on('data', file => {
t.is(path.normalize(file.path), path.join(__dirname, 'fixture/test.zip'));
t.is(file.relative, 'test.zip');
t.true(file.isBuffer());
t.true(file.contents.length > 0);
});
zipStream.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fixture.txt'),
contents: Buffer.from('hello world'),
stat: {
mode: stats.mode,
mtime: stats.mtime,
},
}));
zipStream.write(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fixture2.txt'),
contents: Buffer.from('hello world 2'),
stat: {
mode: stats.mode,
mtime: stats.mtime,
},
}));
zipStream.end();
await promise;
t.is(files[0].path, 'fixture.txt');
t.is(files[1].path, 'fixture2.txt');
t.is(files[0].contents.toString(), 'hello world');
t.is(files[1].contents.toString(), 'hello world 2');
t.is(files[0].stat.mode, stats.mode);
t.is(files[1].stat.mode, stats.mode);
});
test('should zip files (using streams)', async t => {
const file = vinylFileSync(path.join(__dirname, 'fixture/fixture.txt'), {buffer: false});
const stats = fs.statSync(path.join(__dirname, 'fixture/fixture.txt'));
const zipStream = zip('test.zip');
const unzipStream = unzip();
const files = [];
zipStream.pipe(vinylAssign({extract: true})).pipe(unzipStream);
unzipStream.on('data', file => {
files.push(file);
});
zipStream.on('data', file => {
t.is(path.normalize(file.path), path.join(__dirname, 'test.zip'));
t.is(file.relative, 'test.zip');
t.true(file.contents.length > 0);
});
zipStream.end(file);
await pEvent(unzipStream, 'end');
t.is(path.normalize(files[0].path), path.normalize('fixture/fixture.txt'));
t.is(files[0].contents.toString(), 'hello world\n');
t.is(files[0].stat.mode, stats.mode);
});
test('should not skip empty directories', async t => {
const zipStream = zip('test.zip');
const unzipStream = unzip();
const files = [];
const stats = {
isDirectory() {
return true;
},
mode: 0o664,
};
const promise = pEvent(unzipStream, 'end');
zipStream.pipe(vinylAssign({extract: true})).pipe(unzipStream);
unzipStream.on('data', file => {
files.push(file);
});
zipStream.on('data', file => {
t.is(path.normalize(file.path), path.join(__dirname, 'test.zip'));
t.is(file.relative, 'test.zip');
t.true(file.contents.length > 0);
});
zipStream.write(new Vinyl({
cwd: __dirname,
base: __dirname,
path: path.join(__dirname, 'foo'),
contents: null,
stat: stats,
}));
zipStream.end();
await promise;
t.is(files[0].path, 'foo');
t.is(files[0].stat.mode & 0o777, 0o775); // eslint-disable-line no-bitwise
});
test('when `options.modifiedTime` is specified, should override files\' actual `mtime`s', async t => {
const modifiedTime = new Date();
const zipStream = zip('test.zip', {modifiedTime});
const unzipStream = unzip();
zipStream.pipe(vinylAssign({extract: true})).pipe(unzipStream);
const promise = pEvent(unzipStream, 'end');
const files = [];
unzipStream.on('data', file => {
files.push(file);
});
// Send the fixture file through the pipeline as a test case of a file having a real modification timestamp.
const fixtureFile = vinylFileSync(path.join(__dirname, 'fixture/fixture.txt'), {buffer: false});
zipStream.write(fixtureFile);
// Send a fake file through the pipeline as another test case of a file with a different modification timestamp.
const fakeFile = new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fake.txt'),
contents: Buffer.from('hello world'),
stat: {
mode: 0,
mtime: new Date(0),
},
});
zipStream.write(fakeFile);
zipStream.end();
await promise;
for (const file of files) {
t.deepEqual(yazl.dateToDosDateTime(file.stat.mtime), yazl.dateToDosDateTime(modifiedTime));
}
});
test('when `options.modifiedTime` is specified, should create identical zips when files\' `mtime`s change but their content doesn\'t', async t => {
const modifiedTime = new Date();
const stream1 = zip('test1.zip', {modifiedTime});
let zipFile1;
stream1.pipe(easyTransformStream({objectMode: true}, chunk => {
zipFile1 = chunk;
return chunk;
}));
const stream2 = zip('test2.zip', {modifiedTime});
let zipFile2;
stream2.pipe(easyTransformStream({objectMode: true}, chunk => {
zipFile2 = chunk;
return chunk;
}));
// Send a fake file through the first pipeline.
stream1.end(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fake.txt'),
contents: Buffer.from('hello world'),
stat: {
mode: 0,
mtime: new Date(0),
},
}));
// Send a fake file through the second pipeline with the same contents but a different timestamp.
stream2.end(new Vinyl({
cwd: __dirname,
base: path.join(__dirname, 'fixture'),
path: path.join(__dirname, 'fixture/fake.txt'),
contents: Buffer.from('hello world'),
stat: {
mode: 0,
mtime: new Date(999_999_999_999),
},
}));
await Promise.all([pEvent(stream1, 'end'), pEvent(stream2, 'end')]);
t.true(zipFile1.contents.equals(zipFile2.contents));
});
test('should produce a buffer by default', async t => {
t.plan(1);
const zipStream = zip('test.zip');
const promise = pEvent(zipStream, 'end');
const file = vinylFileSync(path.join(__dirname, 'fixture/fixture.txt'));
zipStream.on('data', file => {
t.true(file.isBuffer());
});
zipStream.write(file);
zipStream.end();
await promise;
});
test('should produce a stream if requested', async t => {
t.plan(1);
const zipStream = zip('test.zip', {buffer: false});
const promise = pEvent(zipStream, 'end');
const file = vinylFileSync(path.join(__dirname, 'fixture/fixture.txt'));
zipStream.on('data', file => {
t.true(file.isStream());
});
zipStream.write(file);
zipStream.end();
await promise;
});
// FIXME
// test('should explain buffer size errors', async t => {
// const zipStream = zip('test.zip', {compress: false});
// const unzipStream = unzip();
// const stats = fs.statSync(path.join(__dirname, 'fixture/fixture.txt'));
// zipStream.pipe(vinylAssign({extract: true})).pipe(unzipStream);
// const errorPromise = pEvent(zipStream, 'error');
// function addFile(contents) {
// zipStream.write(new Vinyl({
// cwd: __dirname,
// base: path.join(__dirname, 'fixture'),
// path: path.join(__dirname, 'fixture/file.txt'),
// contents,
// stat: stats,
// }));
// }
// const maxYazlBuffer = 1_073_741_823;
// const filesNeeded = Math.floor(BufferConstants.MAX_LENGTH / maxYazlBuffer);
// for (let files = 0; files < filesNeeded; files++) {
// addFile(Buffer.allocUnsafe(maxYazlBuffer));
// }
// addFile(Buffer.allocUnsafe(BufferConstants.MAX_LENGTH % maxYazlBuffer));
// zipStream.end();
// const error = await errorPromise;
// t.is(error.message, 'The output ZIP file is too big to store in a buffer (larger than Buffer MAX_LENGTH). To output a stream instead, set the gulp-zip buffer option to `false`.');
// });