Skip to content

Commit

Permalink
test: add mock-tarball utility
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Feb 18, 2019
1 parent dd90c43 commit cec1e82
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions test/util/mock-tarball.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

const BB = require('bluebird')

const getStream = require('get-stream')
const pipeline = require('mississippi').pipeline
const tar = require('tar-stream')
const zlib = require('zlib')

module.exports = makeTarball
function makeTarball (files, opts) {
opts = opts || {}
const pack = tar.pack()
Object.keys(files).forEach(function (filename) {
const entry = files[filename]
pack.entry({
name: (opts.noPrefix ? '' : 'package/') + filename,
type: entry.type,
size: entry.size,
mode: entry.mode,
mtime: entry.mtime || new Date(0),
linkname: entry.linkname,
uid: entry.uid,
gid: entry.gid,
uname: entry.uname,
gname: entry.gname
}, typeof files[filename] === 'string'
? files[filename]
: files[filename].data)
})
pack.finalize()
return BB.try(() => {
if (opts.stream && opts.gzip) {
return pipeline(pack, zlib.createGzip())
} else if (opts.stream) {
return pack
} else {
return getStream.buffer(pack).then(ret => {
if (opts.gzip) {
return BB.fromNode(cb => zlib.gzip(ret, cb))
} else {
return ret
}
})
}
})
}

0 comments on commit cec1e82

Please sign in to comment.