From b09b02eca23ca5a072f5201def6ac5faeaea3e66 Mon Sep 17 00:00:00 2001 From: Quentin Rossetti Date: Wed, 17 Oct 2018 22:46:13 +0200 Subject: [PATCH] feat: Add extractFull() command --- lib/commands.js | 4 + package-lock.json | 6 ++ package.json | 1 + test/func/extractFull.spec.js | 135 ++++++++++++++++++++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 test/func/extractFull.spec.js diff --git a/lib/commands.js b/lib/commands.js index cd4f53b..fd9a506 100644 --- a/lib/commands.js +++ b/lib/commands.js @@ -53,3 +53,7 @@ export function remove (archive, source, options) { export function extract (archive, output, cherryPick, options) { return commandExtract('e', archive, output, cherryPick, options) } + +export function extractFull (archive, output, cherryPick, options) { + return commandExtract('x', archive, output, cherryPick, options) +} diff --git a/package-lock.json b/package-lock.json index 7d27391..cbdf80d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1106,6 +1106,12 @@ } } }, + "fs-readdir-recursive": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz", + "integrity": "sha512-GNanXlVr2pf02+sPN40XN8HG+ePaNcvM0q5mZBd668Obwb0yD5GiUbZOFgwn8kGMY6I3mdyDJzieUy3PTYyTRA==", + "dev": true + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index 5356778..d04d8be 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "chai": "^4.1.2", "chalk": "^2.4.1", "coveralls": "^3.0.2", + "fs-readdir-recursive": "^1.1.0", "mocha": "^5.2.0", "mock-fs": "^4.7.0", "nyc": "^12.0.2", diff --git a/test/func/extractFull.spec.js b/test/func/extractFull.spec.js new file mode 100644 index 0000000..dfad558 --- /dev/null +++ b/test/func/extractFull.spec.js @@ -0,0 +1,135 @@ +/* global describe, it */ +import { expect } from 'chai' +import { copyFileSync, readdirSync } from 'fs' +import { extractFull } from '../../lib/commands.js' +import readdirRecursiveSync from 'fs-readdir-recursive' + +const mockDir = './test/_mock' +const tmpDir = './test/_tmp' + +describe('Functional: extractFull()', function () { + it('should return an error on 7z error', function (done) { + const archive = '/i/hope/this/is/not/where/your/archive/is' + const seven = extractFull(archive) + seven.on('error', function (err) { + expect(err).to.be.an.instanceof(Error) + done() + }) + }) + + it('should return an error on spawn error', function (done) { + const bin = '/i/hope/this/is/not/where/yout/7zip/bin/is' + // or this test will fail + const seven = extractFull('archive', undefined, undefined, { + $bin: bin + }) + seven.on('error', function (err) { + expect(err).to.be.an.instanceof(Error) + expect(err.errno).to.equal('ENOENT') + expect(err.code).to.equal('ENOENT') + expect(err.syscall).to.equal(`spawn ${bin}`) + expect(err.path).to.equal(bin) + done() + }) + }) + + it('should overwrite -o switch with output argument', function () { + const seven = extractFull('archive.7z', 'this/path/is/better', undefined, { + o: 'than/this/path', + $defer: true + }) + expect(seven._args).to.contain('-othis/path/is/better') + expect(seven._args).not.to.contain('-othan/this/path') + }) + + it('should set default 7zip ouptut when non or falsy', function () { + const sevenUndefined = extractFull('archive.7z', 'output', undefined, { $defer: true }) + const sevenFalse = extractFull('archive.7z', 'output', null, { $defer: true }) + const sevenNull = extractFull('archive.7z', 'output', false, { $defer: true }) + const sevenEmptyString = extractFull('archive.7z', 'output', '', { $defer: true }) + expect(sevenUndefined._args).not.to.contain('-o') + expect(sevenFalse._args).not.to.contain('-o') + expect(sevenNull._args).not.to.contain('-o') + expect(sevenEmptyString._args).not.to.contain('-o') + }) + + it('should set default 7zip target when non or falsy', function () { + const sevenUndefined = extractFull('archive.7z', undefined, undefined, { $defer: true }) + const sevenFalse = extractFull('archive.7z', null, undefined, { $defer: true }) + const sevenNull = extractFull('archive.7z', false, undefined, { $defer: true }) + const sevenEmptyString = extractFull('archive.7z', '', undefined, { $defer: true }) + sevenUndefined._args.forEach(v => expect(v).not.to.match(/(^-o.)/)) + sevenFalse._args.forEach(v => expect(v).not.to.match(/(^-o.)/)) + sevenNull._args.forEach(v => expect(v).not.to.match(/(^-o.)/)) + sevenEmptyString._args.forEach(v => expect(v).not.to.match(/(^-o.)/)) + }) + + it('should single accept target as string', function () { + const seven = extractFull('archive.7z', undefined, 'target1', { $defer: true }) + expect(seven._args).to.contain('target1') + }) + + it('should multiple accept target as array', function () { + const seven = extractFull('archive.7z', undefined, ['target1', 'target2'], { $defer: true }) + expect(seven._args).to.contain('target1') + expect(seven._args).to.contain('target2') + }) + + it('should extractFull on the right path', function (done) { + const archiveBase = `${mockDir}/DirNew/ExtArchive.7z` + const archive = `${tmpDir}/extractFull-flat-exist.7z` + const output = `${tmpDir}/extractFull-flat-exist` + copyFileSync(archiveBase, archive) + const seven = extractFull(archive, output, false, { r: true }) + seven.on('end', function () { + expect(seven.info['Files']).to.equal('9') + expect(seven.info['Folders']).to.equal('3') + expect(seven.info['Path']).to.equal(archive) + const ls = readdirRecursiveSync(output) + expect(ls).to.contain('DirExt/root.md') + expect(ls).to.contain('DirExt/root.not') + expect(ls).to.contain('DirExt/root.txt') + expect(ls).to.contain('DirExt/sub1/sub1.md') + expect(ls).to.contain('DirExt/sub1/sub1.not') + expect(ls).to.contain('DirExt/sub1/sub1.txt') + expect(ls).to.contain('DirExt/sub2/sub2.md') + expect(ls).to.contain('DirExt/sub2/sub2.not') + expect(ls).to.contain('DirExt/sub2/sub2.txt') + done() + }) + }) + + it('should emit progress values', function (done) { + const archiveBase = `${mockDir}/DirNew/ExtArchive.7z` + const archive = `${tmpDir}/extractFull-flat-progress.7z` + const output = `${tmpDir}/extractFull-flat-progress` + copyFileSync(archiveBase, archive) + let once = false + const seven = extractFull(archive, output, false, { r: true, bs: ['p1'] }) + seven.on('progress', function (progress) { + once = true + expect(progress.percent).to.be.an('number') + expect(progress.fileCount).to.be.an('number') + }).on('end', function () { + expect(once).to.be.equal(true) + done() + }) + }) + + it('should emit files on progress', function (done) { + const archiveBase = `${mockDir}/DirNew/ExtArchive.7z` + const archive = `${tmpDir}/extractFull-flat-data.7z` + const output = `${tmpDir}/extractFull-flat-data` + copyFileSync(archiveBase, archive) + let once = false + const seven = extractFull(archive, output, false, { r: true }) + seven.on('data', function (data) { + once = true + expect(data.symbol).to.be.equal('-') + expect(data.file).to.be.an('string') + }).on('end', function () { + expect(once).to.be.equal(true) + done() + }) + }) +})