Skip to content

Commit

Permalink
refactor: Remove duplicate code by using command-interface function
Browse files Browse the repository at this point in the history
  • Loading branch information
q2s2t committed Oct 17, 2018
1 parent b245142 commit 8d323c0
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/commands.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SevenZipStream } from './stream.js'
import { matchProps, matchProgress } from './parser.js'

export function add (archive, source, options) {
function commandStandard (commandLetter, archive, source, options) {
const opts = Object.assign({}, options)
opts._commandArgs = ['a']
opts._commandArgs = [commandLetter]
opts._commandArgs.push(archive)

const isSourceMultiple = (Array.isArray(source))
Expand All @@ -20,16 +20,31 @@ export function add (archive, source, options) {
return stream
}

export function add (archive, source, options) {
return commandStandard('a', archive, source, options)
}

export function del (archive, source, options) {
return commandStandard('d', archive, source, options)
}

export function extract (archive, output = '', target = '', options) {
const opts = Object.assign({}, options)
opts._commandArgs = ['d']
opts._commandArgs.push(archive)
opts._commandArgs = ['e']
const isDefaultOuput = (output === '')
if (!isDefaultOuput) {
opts['o'] = output
}
const isDefaultTarget = (target === '')
if (!isDefaultTarget) {
opts._commandArgs.push(archive)
}

const isSourceMultiple = (Array.isArray(source))
if (isSourceMultiple) {
opts._commandArgs = opts._commandArgs.concat(source)
const isTargetMultiple = (Array.isArray(target))
if (isTargetMultiple) {
opts._commandArgs = opts._commandArgs.concat(target)
} else {
opts._commandArgs.push(source)
opts._commandArgs.push(target)
}

opts._matchHeaders = matchProps
Expand Down
98 changes: 98 additions & 0 deletions test/func/extract.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* global describe, it */
import { expect } from 'chai'
import { copyFileSync, statSync } from 'fs'
import { extract } from '../../lib/commands.js'

const mockDir = './test/_mock'
const tmpDir = './test/_tmp'

describe('Functional: extract()', function () {
it('should return an error on 7z error', function (done) {
const archive = '/i/hope/this/is/not/where/your/archive/is'
const seven = extract(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 = extract('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 reduce archive size by deleting content', function (done) {
// const archiveBase = `${mockDir}/DirNew/ExtArchive.7z`
// const archive = `${tmpDir}/del-md.7z`
// const target = `DirExt/*.md`
// copyFileSync(archiveBase, archive)
// const sizeBase = statSync(archiveBase).size
// const seven = del(archive, target, { r: true })
// seven.on('end', function () {
// const size = statSync(archive).size
// expect(size).to.lessThan(sizeBase)
// expect(seven.info['Updating archive']).to.equal(archive)
// done()
// })
// })

// it('should accept multiple sources as a array', function (done) {
// const archiveBase = `${mockDir}/DirNew/ExtArchive.7z`
// const archive = `${tmpDir}/del-multiple.7z`
// const target = [`DirExt/*.md`, `DirExt/*.txt`]
// copyFileSync(archiveBase, archive)
// const sizeBase = statSync(archiveBase).size
// const seven = del(archive, target, { r: true })
// seven.on('end', function () {
// const size = statSync(archive).size
// expect(size).to.lessThan(sizeBase)
// expect(seven.info['Updating archive']).to.equal(archive)
// done()
// })
// })

// it('should emit progress values', function (done) {
// const archiveBase = `${mockDir}/DirNew/ExtArchive.7z`
// const archive = `${tmpDir}/progress-del.7z`
// const target = `DirExt/*.md`
// copyFileSync(archiveBase, archive)
// const seven = del(archive, target, { bs: ['p1'] })
// let once = false
// 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}/progress-file-del.7z`
// const target = `DirExt/*.md`
// copyFileSync(archiveBase, archive)
// const seven = del(archive, target, { bs: ['p1'], r: true })
// let once = false
// seven.on('data', function (progress) {
// once = true
// expect(progress.symbol).to.be.an('string')
// expect(progress.file).to.be.an('string')
// }).on('end', function () {
// expect(once).to.be.equal(true)
// done()
// })
// })
})

0 comments on commit 8d323c0

Please sign in to comment.