Skip to content

Commit

Permalink
feat: Add extractFull() command
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Oct 17, 2018
1 parent 1cba732 commit b09b02e
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
135 changes: 135 additions & 0 deletions test/func/extractFull.spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})

0 comments on commit b09b02e

Please sign in to comment.