makes filter property of zip options optional, also adds strip directory tests

This commit is contained in:
Jim Unger 2016-07-27 15:16:19 -05:00
parent c226848467
commit 8bfb2aeb2b
3 changed files with 70 additions and 4 deletions

View file

@ -80,6 +80,68 @@ describe('kibana cli', function () {
describe('extractFiles', function () {
describe('strip files parameter', function () {
it('strips specified number of directories', function () {
return copyReplyFile('strip_test.zip')
.then(() => {
return extractFiles(settings.tempArchiveFile, settings.workingPath, 1);
})
.then(() => {
const files = glob.sync('**/*', { cwd: testWorkingPath });
const expected = [
'1 level deep.txt',
'test-plugin',
'test-plugin/2 levels deep.txt',
'test-plugin/public',
'test-plugin/public/3 levels deep.txt',
'archive.part'
];
expect(files.sort()).to.eql(expected.sort());
});
});
it('throws an exception if it tries to strip too many directories', function () {
return copyReplyFile('strip_test.zip')
.then(() => {
return extractFiles(settings.tempArchiveFile, settings.workingPath, 2);
})
.then(shouldReject, (err) => {
expect(err.message).to.match(/You cannot strip more levels than there are directories/i);
});
});
it('applies the filter before applying the strip directories logic', function () {
return copyReplyFile('strip_test.zip')
.then(() => {
const filter = {
paths: [
'test-plugin'
]
};
return extractFiles(settings.tempArchiveFile, settings.workingPath, 2, filter);
})
.then(() => {
const files = glob.sync('**/*', { cwd: testWorkingPath });
const expected = [
'2 levels deep.txt',
'public',
'public/3 levels deep.txt',
'archive.part'
];
expect(files.sort()).to.eql(expected.sort());
});
});
});
it('extracts files using the files filter', function () {
return copyReplyFile('test_plugin_many.zip')
.then(() => {

View file

@ -63,11 +63,15 @@ export async function extractFiles(zipPath, targetPath, strip, filter) {
unzipper.on('error', reject);
unzipper.extract({
const options = {
path: targetPath,
strip: strip,
filter: extractFilter(filter)
});
strip: strip
};
if (filter) {
options.filter = extractFilter(filter);
}
unzipper.extract(options);
unzipper.on('extract', resolve);
});