-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |