Skip to content

Commit

Permalink
Merge pull request elastic#7867 from BigFunger/decompress-zip-strip-t…
Browse files Browse the repository at this point in the history
…ests

Add tests for decompress-zip to check strip functionality
  • Loading branch information
epixa authored Aug 20, 2016
2 parents 9c432ef + 8bfb2ae commit 0eae35b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Binary file not shown.
62 changes: 62 additions & 0 deletions src/cli_plugin/install/__tests__/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
12 changes: 8 additions & 4 deletions src/cli_plugin/install/zip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit 0eae35b

Please sign in to comment.