update tests

This commit is contained in:
Brian Gough 2017-10-16 11:13:53 +01:00
parent d6d76f132e
commit 96d08e41a7
2 changed files with 156 additions and 37 deletions

View file

@ -15,7 +15,7 @@ module.exports = ArchiveManager =
_isZipTooLarge: (source, callback = (err, isTooLarge)->)-> _isZipTooLarge: (source, callback = (err, isTooLarge)->)->
callback = _.once callback callback = _.once callback
totalSizeInBytes = 0 totalSizeInBytes = null
yauzl.open source, {lazyEntries: true}, (err, zipfile) -> yauzl.open source, {lazyEntries: true}, (err, zipfile) ->
return callback(err) if err? return callback(err) if err?
@ -61,11 +61,15 @@ module.exports = ArchiveManager =
readStream.on "error", callback readStream.on "error", callback
readStream.on "end", callback readStream.on "end", callback
errorHandler = (err) -> # clean up before calling callback
readStream.unpipe()
readStream.destroy()
callback(err)
fse.ensureDir Path.dirname(destFile), (err) -> fse.ensureDir Path.dirname(destFile), (err) ->
return callback(err) if err? return errorHandler(err) if err?
writeStream = fs.createWriteStream destFile writeStream = fs.createWriteStream destFile
writeStream.on 'error', (err) -> writeStream.on 'error', errorHandler
return callback(err)
readStream.pipe(writeStream) readStream.pipe(writeStream)
_extractZipFiles: (source, destination, callback = (err) ->) -> _extractZipFiles: (source, destination, callback = (err) ->) ->
@ -110,6 +114,7 @@ module.exports = ArchiveManager =
logger.log source: source, destination: destination, "unzipping file" logger.log source: source, destination: destination, "unzipping file"
ArchiveManager._extractZipFiles source, destination, (err) -> ArchiveManager._extractZipFiles source, destination, (err) ->
timer.done()
if err? if err?
logger.error {err, source, destination}, "unzip failed" logger.error {err, source, destination}, "unzip failed"
callback(err) callback(err)

View file

@ -10,24 +10,22 @@ describe "ArchiveManager", ->
beforeEach -> beforeEach ->
@logger = @logger =
error: sinon.stub() error: sinon.stub()
warn: sinon.stub()
err:-> err:->
log: sinon.stub() log: sinon.stub()
@process = new events.EventEmitter
@process.stdout = new events.EventEmitter
@process.stderr = new events.EventEmitter
@child =
spawn: sinon.stub().returns(@process)
@metrics = @metrics =
Timer: class Timer Timer: class Timer
done: sinon.stub() done: sinon.stub()
@zipfile = new events.EventEmitter
@zipfile.readEntry = sinon.stub()
@zipfile.close = sinon.stub()
@ArchiveManager = SandboxedModule.require modulePath, requires: @ArchiveManager = SandboxedModule.require modulePath, requires:
"child_process": @child "yauzl": @yauzl = {open: sinon.stub().callsArgWith(2, null, @zipfile)}
"logger-sharelatex": @logger "logger-sharelatex": @logger
"metrics-sharelatex": @metrics "metrics-sharelatex": @metrics
"fs": @fs = {} "fs": @fs = {}
"fs-extra": @fse = {}
describe "extractZipArchive", -> describe "extractZipArchive", ->
beforeEach -> beforeEach ->
@ -39,10 +37,10 @@ describe "ArchiveManager", ->
describe "successfully", -> describe "successfully", ->
beforeEach (done) -> beforeEach (done) ->
@ArchiveManager.extractZipArchive @source, @destination, done @ArchiveManager.extractZipArchive @source, @destination, done
@process.emit "close" @zipfile.emit "end"
it "should run unzip", -> it "should run yauzl", ->
@child.spawn.calledWithExactly("unzip", [@source, "-d", @destination]).should.equal true @yauzl.open.calledWith(@source).should.equal true
it "should time the unzip", -> it "should time the unzip", ->
@metrics.Timer::done.called.should.equal true @metrics.Timer::done.called.should.equal true
@ -50,13 +48,12 @@ describe "ArchiveManager", ->
it "should log the unzip", -> it "should log the unzip", ->
@logger.log.calledWith(sinon.match.any, "unzipping file").should.equal true @logger.log.calledWith(sinon.match.any, "unzipping file").should.equal true
describe "with an error on stderr", -> describe "with an error in the zip file header", ->
beforeEach (done) -> beforeEach (done) ->
@yauzl.open = sinon.stub().callsArgWith(2, new Error("Something went wrong"))
@ArchiveManager.extractZipArchive @source, @destination, (error) => @ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error) @callback(error)
done() done()
@process.stderr.emit "data", "Something went wrong"
@process.emit "close"
it "should return the callback with an error", -> it "should return the callback with an error", ->
@callback.calledWithExactly(new Error("Something went wrong")).should.equal true @callback.calledWithExactly(new Error("Something went wrong")).should.equal true
@ -74,15 +71,15 @@ describe "ArchiveManager", ->
it "should return the callback with an error", -> it "should return the callback with an error", ->
@callback.calledWithExactly(new Error("zip_too_large")).should.equal true @callback.calledWithExactly(new Error("zip_too_large")).should.equal true
it "should not call spawn", -> it "should not call yauzl.open", ->
@child.spawn.called.should.equal false @yauzl.open.called.should.equal false
describe "with an error on the process", -> describe "with an error in the extracted files", ->
beforeEach (done) -> beforeEach (done) ->
@ArchiveManager.extractZipArchive @source, @destination, (error) => @ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error) @callback(error)
done() done()
@process.emit "error", new Error("Something went wrong") @zipfile.emit "error", new Error("Something went wrong")
it "should return the callback with an error", -> it "should return the callback with an error", ->
@callback.calledWithExactly(new Error("Something went wrong")).should.equal true @callback.calledWithExactly(new Error("Something went wrong")).should.equal true
@ -90,44 +87,161 @@ describe "ArchiveManager", ->
it "should log out the error", -> it "should log out the error", ->
@logger.error.called.should.equal true @logger.error.called.should.equal true
describe "with a relative extracted file path", ->
beforeEach (done) ->
@zipfile.openReadStream = sinon.stub()
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error)
done()
@zipfile.emit "entry", {fileName: "../testfile.txt"}
@zipfile.emit "end"
it "should not write try to read the file entry", ->
@zipfile.openReadStream.called.should.equal false
it "should log out a warning", ->
@logger.warn.called.should.equal true
describe "with an unnormalized extracted file path", ->
beforeEach (done) ->
@zipfile.openReadStream = sinon.stub()
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error)
done()
@zipfile.emit "entry", {fileName: "foo/./testfile.txt"}
@zipfile.emit "end"
it "should not write try to read the file entry", ->
@zipfile.openReadStream.called.should.equal false
it "should log out a warning", ->
@logger.warn.called.should.equal true
describe "with a directory entry", ->
beforeEach (done) ->
@zipfile.openReadStream = sinon.stub()
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error)
done()
@zipfile.emit "entry", {fileName: "testdir/"}
@zipfile.emit "end"
it "should not write try to read the entry", ->
@zipfile.openReadStream.called.should.equal false
it "should not log out a warning", ->
@logger.warn.called.should.equal false
describe "with an error opening the file read stream", ->
beforeEach (done) ->
@zipfile.openReadStream = sinon.stub().callsArgWith(1, new Error("Something went wrong"))
@writeStream = new events.EventEmitter
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error)
done()
@zipfile.emit "entry", {fileName: "testfile.txt"}
@zipfile.emit "end"
it "should return the callback with an error", ->
@callback.calledWithExactly(new Error("Something went wrong")).should.equal true
it "should log out the error", ->
@logger.error.called.should.equal true
it "should close the zipfile", ->
@zipfile.close.called.should.equal true
describe "with an error in the file read stream", ->
beforeEach (done) ->
@readStream = new events.EventEmitter
@readStream.pipe = sinon.stub()
@zipfile.openReadStream = sinon.stub().callsArgWith(1, null, @readStream)
@writeStream = new events.EventEmitter
@fs.createWriteStream = sinon.stub().returns @writeStream
@fse.ensureDir = sinon.stub().callsArg(1)
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error)
done()
@zipfile.emit "entry", {fileName: "testfile.txt"}
@readStream.emit "error", new Error("Something went wrong")
@zipfile.emit "end"
it "should return the callback with an error", ->
@callback.calledWithExactly(new Error("Something went wrong")).should.equal true
it "should log out the error", ->
@logger.error.called.should.equal true
it "should close the zipfile", ->
@zipfile.close.called.should.equal true
describe "with an error in the file write stream", ->
beforeEach (done) ->
@readStream = new events.EventEmitter
@readStream.pipe = sinon.stub()
@readStream.unpipe = sinon.stub()
@readStream.destroy = sinon.stub()
@zipfile.openReadStream = sinon.stub().callsArgWith(1, null, @readStream)
@writeStream = new events.EventEmitter
@fs.createWriteStream = sinon.stub().returns @writeStream
@fse.ensureDir = sinon.stub().callsArg(1)
@ArchiveManager.extractZipArchive @source, @destination, (error) =>
@callback(error)
done()
@zipfile.emit "entry", {fileName: "testfile.txt"}
@writeStream.emit "error", new Error("Something went wrong")
@zipfile.emit "end"
it "should return the callback with an error", ->
@callback.calledWithExactly(new Error("Something went wrong")).should.equal true
it "should log out the error", ->
@logger.error.called.should.equal true
it "should unpipe from the readstream", ->
@readStream.unpipe.called.should.equal true
it "should destroy the readstream", ->
@readStream.destroy.called.should.equal true
it "should close the zipfile", ->
@zipfile.close.called.should.equal true
describe "_isZipTooLarge", -> describe "_isZipTooLarge", ->
beforeEach ->
@output = (totalSize)->" Length Date Time Name \n-------- ---- ---- ---- \n241 03-12-16 12:20 main.tex \n108801 03-12-16 12:20 ddd/x1J5kHh.jpg \n-------- ------- \n#{totalSize} 2 files\n"
it "should return false with small output", (done)-> it "should return false with small output", (done)->
@ArchiveManager._isZipTooLarge @source, (error, isTooLarge) => @ArchiveManager._isZipTooLarge @source, (error, isTooLarge) =>
isTooLarge.should.equal false isTooLarge.should.equal false
done() done()
@process.stdout.emit "data", @output("109042") @zipfile.emit "entry", {uncompressedSize: 109042}
@process.emit "close" @zipfile.emit "end"
it "should return true with large bytes", (done)-> it "should return true with large bytes", (done)->
@ArchiveManager._isZipTooLarge @source, (error, isTooLarge) => @ArchiveManager._isZipTooLarge @source, (error, isTooLarge) =>
isTooLarge.should.equal true isTooLarge.should.equal true
done() done()
@process.stdout.emit "data", @output("1090000000000000042") @zipfile.emit "entry", {uncompressedSize: 1090000000000000042}
@process.emit "close" @zipfile.emit "end"
it "should return error on no data", (done)-> it "should return error on no data", (done)->
@ArchiveManager._isZipTooLarge @source, (error, isTooLarge) => @ArchiveManager._isZipTooLarge @source, (error, isTooLarge) =>
expect(error).to.exist expect(error).to.exist
done() done()
@process.stdout.emit "data", "" @zipfile.emit "entry", {}
@process.emit "close" @zipfile.emit "end"
it "should return error if it didn't get a number", (done)-> it "should return error if it didn't get a number", (done)->
@ArchiveManager._isZipTooLarge @source, (error, isTooLarge) => @ArchiveManager._isZipTooLarge @source, (error, isTooLarge) =>
expect(error).to.exist expect(error).to.exist
done() done()
@process.stdout.emit "data", @output("total_size_string") @zipfile.emit "entry", {uncompressedSize:"random-error"}
@process.emit "close" @zipfile.emit "end"
it "should return error if the is only a bit of data", (done)-> it "should return error if there is no data", (done)->
@ArchiveManager._isZipTooLarge @source, (error, isTooLarge) => @ArchiveManager._isZipTooLarge @source, (error, isTooLarge) =>
expect(error).to.exist expect(error).to.exist
done() done()
@process.stdout.emit "data", " Length Date Time Name \n--------" @zipfile.emit "end"
@process.emit "close"
describe "findTopLevelDirectory", -> describe "findTopLevelDirectory", ->
beforeEach -> beforeEach ->