Skip to content

Commit cec1e82

Browse files
committed
test: add mock-tarball utility
1 parent dd90c43 commit cec1e82

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

test/util/mock-tarball.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
const BB = require('bluebird')
4+
5+
const getStream = require('get-stream')
6+
const pipeline = require('mississippi').pipeline
7+
const tar = require('tar-stream')
8+
const zlib = require('zlib')
9+
10+
module.exports = makeTarball
11+
function makeTarball (files, opts) {
12+
opts = opts || {}
13+
const pack = tar.pack()
14+
Object.keys(files).forEach(function (filename) {
15+
const entry = files[filename]
16+
pack.entry({
17+
name: (opts.noPrefix ? '' : 'package/') + filename,
18+
type: entry.type,
19+
size: entry.size,
20+
mode: entry.mode,
21+
mtime: entry.mtime || new Date(0),
22+
linkname: entry.linkname,
23+
uid: entry.uid,
24+
gid: entry.gid,
25+
uname: entry.uname,
26+
gname: entry.gname
27+
}, typeof files[filename] === 'string'
28+
? files[filename]
29+
: files[filename].data)
30+
})
31+
pack.finalize()
32+
return BB.try(() => {
33+
if (opts.stream && opts.gzip) {
34+
return pipeline(pack, zlib.createGzip())
35+
} else if (opts.stream) {
36+
return pack
37+
} else {
38+
return getStream.buffer(pack).then(ret => {
39+
if (opts.gzip) {
40+
return BB.fromNode(cb => zlib.gzip(ret, cb))
41+
} else {
42+
return ret
43+
}
44+
})
45+
}
46+
})
47+
}

0 commit comments

Comments
 (0)