-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
288 lines (255 loc) · 6.96 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
'use strict';
const {createGzip} = require('zlib');
const {join} = require('path');
const {mkdir, symlink} = require('fs').promises;
const enumerateFiles = require('enumerate-files');
const {extract} = require('tar');
const fileToTar = require('.');
const rmfr = require('rmfr');
const test = require('tape');
test('fileToTar()', async t => {
t.plan(23);
const tmp = join(__dirname, 'tmp');
const tmpSymlink = join(__dirname, 'tmp-symlink');
const dest = join(tmp, 'archive.tar');
await Promise.all([rmfr(tmp), rmfr(tmpSymlink)]);
await symlink(__filename, tmpSymlink);
fileToTar(join(__dirname, 'index.js'), dest).subscribe({
next(progress) {
if (progress.bytes === 0) {
t.equal(
progress.header.name,
'index.js',
'should send compression progress.'
);
return;
}
if (progress.bytes === progress.header.size) {
t.ok(
Number.isSafeInteger(progress.header.mode),
'should send file metadata.'
);
}
},
error: t.fail,
async complete() {
const cwd = join(tmp, '0');
await mkdir(cwd, {recursive: true});
await extract({file: dest, cwd});
t.deepEqual(
[...await enumerateFiles(cwd)],
[join(cwd, 'index.js')],
'should create a tar archive.'
);
}
});
const anotherDest = join(tmp, 'another-archive.tgz');
fileToTar(join(__filename), anotherDest, {
tarTransform: createGzip(),
map(header) {
header.name = 'modified.txt';
return header;
},
mapStream(fileStream) {
t.pass('should support `mapStream` and `tarTransform` options.');
return fileStream;
}
}).subscribe({
error: t.fail,
async complete() {
const cwd = join(tmp, '1');
await mkdir(cwd, {recursive: true});
await extract({file: anotherDest, cwd});
t.deepEqual(
[...await enumerateFiles(cwd)],
[join(cwd, 'modified.txt')],
'should support `tarTransform` and `map` option.'
);
}
});
const fail = t.fail.bind(t, 'Unexpectedly completed.');
fileToTar('123/456/789', '123/456/789').subscribe({
error(err) {
t.equal(
err.toString(),
`Error: Source file path must be different from the archive path. Both were specified to ${
join(__dirname, '123', '456', '789')
}.`,
'should fail when the first and second paths are the same path.'
);
},
complete: fail
});
fileToTar('none', 'dest').subscribe({
error({code}) {
t.equal(
code,
'ENOENT',
'should fail when the source file doesn\'t exists.'
);
},
complete: fail
});
fileToTar(tmpSymlink, 'dest').subscribe({
error(err) {
t.equal(
err.toString(),
`Error: Expected ${tmpSymlink} to be a file path, but it was a symbolic link.`,
'should fail when the source file doesn\'t exists.'
);
},
complete: fail
});
fileToTar(__dirname, join(tmp, '._')).subscribe({
error(err) {
t.equal(
err.toString(),
`Error: Expected ${__dirname} to be a file path, but it was a directory.`,
'should fail when the source is a directory.'
);
},
complete: fail
});
fileToTar(join(__dirname, 'index.js'), join(__dirname, 'node_modules')).subscribe({
error({code}) {
t.equal(code, 'EISDIR', 'should fail when it cannot write a tar file.');
},
complete: fail
});
fileToTar(join(__dirname, 'index.js'), join(__filename, 'a')).subscribe({
error({code}) {
t.equal(code, 'EEXIST', 'should fail when it cannot create a parent directory.');
},
complete: fail
});
fileToTar(1).subscribe({
error(err) {
t.equal(
err.toString(),
'TypeError: Expected a file path to be compressed as an archive, but got a non-string value 1 (number).',
'should fail when the file path is not a string.'
);
},
complete: fail
});
fileToTar('').subscribe({
error(err) {
t.equal(
err.toString(),
'Error: Expected a file path to be compressed as an archive, but got \'\' (empty string).',
'should fail when the file path is an empty string.'
);
},
complete: fail
});
fileToTar('a', true).subscribe({
error(err) {
t.equal(
err.toString(),
'TypeError: Expected a file path where an archive file will be created, but got a non-string value true (boolean).',
'should fail when the tar path is not a string.'
);
},
complete: fail
});
fileToTar('a', '').subscribe({
error(err) {
t.equal(
err.toString(),
'Error: Expected a file path where an archive file will be created, but got \'\' (empty string).',
'should fail when the tar path is an empty string.'
);
},
complete: fail
});
fileToTar('a', 'b', /c/u).subscribe({
error(err) {
t.equal(
err.toString(),
'TypeError: Expected a plain object to set file-to-tar options, but got /c/u (regexp).',
'should fail when the third argument is not a plain object.'
);
},
complete: fail
});
fileToTar('a', 'b', {entries: 1}).subscribe({
error(err) {
t.equal(
err.toString(),
'Error: file-to-tar doesn\'t support `entries` option, but 1 (number) was provided.',
'should fail when it takes an invalid option.'
);
},
complete: fail
});
fileToTar('a', 'b', {tarTransform: Symbol('c')}).subscribe({
error(err) {
t.equal(
err.toString(),
'TypeError: `tarTransform` option must be a transform stream that modifies the tar archive before writing, but got a non-stream value Symbol(c).',
'should fail when it takes a non-stream `tarTransform` option.'
);
},
complete: fail
});
fileToTar('a', 'b', {tarTransform: process.stdout}).subscribe({
error(err) {
t.equal(
err.toString(),
'TypeError: `tarTransform` option must be a transform stream that modifies ' +
'the tar archive before writing, but got a writable stream instead.',
'should fail when it takes an unreadable `tarTransform` option.'
);
},
complete: fail
});
fileToTar(__filename, join(tmp, '_'), {
mapStream() {
return Buffer.from('a');
}
}).subscribe({
error(err) {
t.equal(
err.toString(),
'TypeError: The function passed to `mapStream` option must return a stream, but returned a non-stream value <Buffer 61>.',
'should fail when `mapStream` function returns a non-stream value.'
);
},
complete: fail
});
fileToTar(__filename, join(tmp, '_'), {
mapStream() {
return process.stdout;
}
}).subscribe({
error(err) {
t.equal(
err.toString(),
'TypeError: The function passed to `mapStream` option ' +
'must return a stream that is readable, but returned a non-readable stream.',
'should fail when it takes a unreadable `tarTransform` option.'
);
},
complete: fail
});
fileToTar().subscribe({
error(err) {
t.equal(
err.toString(),
'RangeError: Expected 1, 2 or 3 arguments (<string>, <string>[, <Object>]), but got no arguments.',
'should fail when it takes no arguments.'
);
},
complete: fail
});
fileToTar(1, 2, 3, 4).subscribe({
error(err) {
t.equal(
err.toString(),
'RangeError: Expected 1, 2 or 3 arguments (<string>, <string>[, <Object>]), but got 4 arguments.',
'should fail when it takes too many arguments.'
);
},
complete: fail
});
});