Merge pull request #32874 from overleaf/bg-promisify-archive-manager
Promisify ArchiveManager GitOrigin-RevId: 7a3ee3dfb2f07dc06ee894cfce0a9196622c878e
This commit is contained in:
@@ -1,12 +1,4 @@
|
||||
import { vi, expect } from 'vitest'
|
||||
// TODO: This file was created by bulk-decaffeinate.
|
||||
// Fix any style issues and re-enable lint.
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* DS206: Consider reworking classes to avoid initClass
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
import sinon from 'sinon'
|
||||
import ArchiveErrors from '../../../../app/src/Features/Uploads/ArchiveErrors.mjs'
|
||||
import events from 'node:events'
|
||||
@@ -20,24 +12,17 @@ const modulePath = '../../../../app/src/Features/Uploads/ArchiveManager.mjs'
|
||||
|
||||
describe('ArchiveManager', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
let Timer
|
||||
ctx.metrics = {
|
||||
Timer: (Timer = (function () {
|
||||
Timer = class Timer {
|
||||
static initClass() {
|
||||
this.prototype.done = sinon.stub()
|
||||
}
|
||||
}
|
||||
Timer.initClass()
|
||||
return Timer
|
||||
})()),
|
||||
Timer: class Timer {},
|
||||
}
|
||||
ctx.metrics.Timer.prototype.done = sinon.stub()
|
||||
ctx.zipfile = new events.EventEmitter()
|
||||
ctx.zipfile.readEntry = sinon.stub()
|
||||
ctx.zipfile.close = sinon.stub()
|
||||
|
||||
ctx.Settings = {}
|
||||
vi.doMock('@overleaf/settings', () => ({
|
||||
default: {},
|
||||
default: ctx.Settings,
|
||||
}))
|
||||
|
||||
vi.doMock('yauzl', () => ({
|
||||
@@ -49,50 +34,57 @@ describe('ArchiveManager', function () {
|
||||
vi.doMock('@overleaf/metrics', () => ({
|
||||
default: ctx.metrics,
|
||||
}))
|
||||
ctx.fs = { mkdir: sinon.stub().yields(), stat: sinon.stub() }
|
||||
ctx.fs = { mkdir: sinon.stub().yields() }
|
||||
vi.doMock('fs', () => ({
|
||||
default: ctx.fs,
|
||||
}))
|
||||
|
||||
ctx.fsPromises = {
|
||||
readdir: sinon.stub(),
|
||||
stat: sinon.stub(),
|
||||
}
|
||||
vi.doMock('fs/promises', () => ({
|
||||
default: ctx.fsPromises,
|
||||
}))
|
||||
|
||||
vi.doMock(
|
||||
'../../../../app/src/Features/Uploads/ArchiveErrors',
|
||||
() => ArchiveErrors
|
||||
)
|
||||
|
||||
ctx.ArchiveManager = (await import(modulePath)).default
|
||||
ctx.callback = sinon.stub()
|
||||
})
|
||||
|
||||
describe('extractZipArchive', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.source = '/path/to/zip/source.zip'
|
||||
ctx.destination = '/path/to/zip/destination'
|
||||
ctx.ArchiveManager._isZipTooLarge = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, false)
|
||||
ctx.ArchiveManager._isZipTooLarge = sinon.stub().resolves(false)
|
||||
})
|
||||
|
||||
describe('successfully', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
resolve
|
||||
)
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
|
||||
// entry contains a single file
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.readStream.end()
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
// entry contains a single file
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.readStream.end()
|
||||
// flush pipeline callback (fires via nextTick)
|
||||
await new Promise(resolve => process.nextTick(resolve))
|
||||
ctx.zipfile.emit('end')
|
||||
await promise
|
||||
})
|
||||
|
||||
it('should run yauzl', function (ctx) {
|
||||
@@ -106,107 +98,96 @@ describe('ArchiveManager', function () {
|
||||
|
||||
describe('with a zipfile containing an empty directory', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
resolve()
|
||||
}
|
||||
)
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
|
||||
// entry contains a single, empty directory
|
||||
ctx.zipfile.emit('entry', { fileName: 'testdir/' })
|
||||
ctx.readStream.end()
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
// entry contains a single, empty directory
|
||||
ctx.zipfile.emit('entry', { fileName: 'testdir/' })
|
||||
ctx.readStream.end()
|
||||
ctx.zipfile.emit('end')
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
sinon.assert.calledWithExactly(
|
||||
ctx.callback,
|
||||
sinon.match.instanceOf(ArchiveErrors.EmptyZipFileError)
|
||||
)
|
||||
it('should reject with an EmptyZipFileError', function (ctx) {
|
||||
expect(ctx.error).to.be.instanceOf(ArchiveErrors.EmptyZipFileError)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with an empty zipfile', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
ctx.zipfile.emit('end')
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
sinon.assert.calledWithExactly(
|
||||
ctx.callback,
|
||||
sinon.match.instanceOf(ArchiveErrors.EmptyZipFileError)
|
||||
)
|
||||
it('should reject with an EmptyZipFileError', function (ctx) {
|
||||
expect(ctx.error).to.be.instanceOf(ArchiveErrors.EmptyZipFileError)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with an error in the zip file header', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.yauzl.open = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, new ArchiveErrors.InvalidZipFileError())
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.yauzl.open = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, new ArchiveErrors.InvalidZipFileError())
|
||||
|
||||
try {
|
||||
await ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
resolve()
|
||||
}
|
||||
ctx.destination
|
||||
)
|
||||
})
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
sinon.assert.calledWithExactly(
|
||||
ctx.callback,
|
||||
sinon.match.instanceOf(ArchiveErrors.InvalidZipFileError)
|
||||
)
|
||||
it('should reject with an error', function (ctx) {
|
||||
expect(ctx.error).to.be.instanceOf(ArchiveErrors.InvalidZipFileError)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with a zip that is too large', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager._isZipTooLarge = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, true)
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.ArchiveManager._isZipTooLarge = sinon.stub().resolves(true)
|
||||
|
||||
try {
|
||||
await ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
resolve()
|
||||
}
|
||||
ctx.destination
|
||||
)
|
||||
})
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
sinon.assert.calledWithExactly(
|
||||
ctx.callback,
|
||||
sinon.match.instanceOf(ArchiveErrors.ZipContentsTooLargeError)
|
||||
it('should reject with a ZipContentsTooLargeError', function (ctx) {
|
||||
expect(ctx.error).to.be.instanceOf(
|
||||
ArchiveErrors.ZipContentsTooLargeError
|
||||
)
|
||||
})
|
||||
|
||||
@@ -217,96 +198,101 @@ describe('ArchiveManager', function () {
|
||||
|
||||
describe('with an error in the extracted files', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('error', new Error('Something went wrong'))
|
||||
})
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
ctx.zipfile.emit('error', new Error('Something went wrong'))
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
return ctx.callback.should.have.been.calledWithExactly(
|
||||
sinon.match
|
||||
.instanceOf(Error)
|
||||
.and(sinon.match.has('message', 'Something went wrong'))
|
||||
)
|
||||
it('should reject with an error', function (ctx) {
|
||||
expect(ctx.error)
|
||||
.to.be.instanceOf(Error)
|
||||
.and.have.property('message', 'Something went wrong')
|
||||
})
|
||||
})
|
||||
|
||||
describe('with a relative extracted file path', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.zipfile.openReadStream = sinon.stub()
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
return resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('entry', { fileName: '../testfile.txt' })
|
||||
return ctx.zipfile.emit('end')
|
||||
})
|
||||
ctx.zipfile.openReadStream = sinon.stub()
|
||||
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
ctx.zipfile.emit('entry', { fileName: '../testfile.txt' })
|
||||
ctx.zipfile.emit('end')
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should not write try to read the file entry', function (ctx) {
|
||||
return ctx.zipfile.openReadStream.called.should.equal(false)
|
||||
it('should not try to read the file entry', function (ctx) {
|
||||
ctx.zipfile.openReadStream.called.should.equal(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with an unnormalized extracted file path', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.zipfile.openReadStream = sinon.stub()
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
return resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('entry', { fileName: 'foo/./testfile.txt' })
|
||||
return ctx.zipfile.emit('end')
|
||||
})
|
||||
ctx.zipfile.openReadStream = sinon.stub()
|
||||
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
ctx.zipfile.emit('entry', { fileName: 'foo/./testfile.txt' })
|
||||
ctx.zipfile.emit('end')
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should not try to read the file entry', function (ctx) {
|
||||
return ctx.zipfile.openReadStream.called.should.equal(false)
|
||||
ctx.zipfile.openReadStream.called.should.equal(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with backslashes in the path', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
return resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('entry', { fileName: 'wombat\\foo.tex' })
|
||||
ctx.readStream.end()
|
||||
ctx.zipfile.emit('entry', { fileName: 'potato\\bar.tex' })
|
||||
ctx.readStream.end()
|
||||
return ctx.zipfile.emit('end')
|
||||
})
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
ctx.zipfile.emit('entry', { fileName: 'wombat\\foo.tex' })
|
||||
ctx.readStream.end()
|
||||
ctx.zipfile.emit('entry', { fileName: 'potato\\bar.tex' })
|
||||
ctx.readStream.end()
|
||||
ctx.zipfile.emit('end')
|
||||
// Pipeline doesn't complete in this test (shared streams);
|
||||
// we only verify method call arguments, so swallow the rejection.
|
||||
await promise.catch(() => {})
|
||||
})
|
||||
|
||||
it('should read the file entry with its original path', function (ctx) {
|
||||
@@ -339,19 +325,22 @@ describe('ArchiveManager', function () {
|
||||
|
||||
describe('with a directory entry', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.zipfile.openReadStream = sinon.stub()
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('entry', { fileName: 'testdir/' })
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
ctx.zipfile.openReadStream = sinon.stub()
|
||||
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
ctx.zipfile.emit('entry', { fileName: 'testdir/' })
|
||||
ctx.zipfile.emit('end')
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should not try to read the entry', function (ctx) {
|
||||
@@ -361,30 +350,31 @@ describe('ArchiveManager', function () {
|
||||
|
||||
describe('with an error opening the file read stream', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('Something went wrong'))
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('Something went wrong'))
|
||||
ctx.writeStream = new PassThrough()
|
||||
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.zipfile.emit('end')
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
ctx.callback.should.have.been.calledWithExactly(
|
||||
sinon.match
|
||||
.instanceOf(Error)
|
||||
.and(sinon.match.has('message', 'Something went wrong'))
|
||||
)
|
||||
it('should reject with an error', function (ctx) {
|
||||
expect(ctx.error)
|
||||
.to.be.instanceOf(Error)
|
||||
.and.have.property('message', 'Something went wrong')
|
||||
})
|
||||
|
||||
it('should close the zipfile', function (ctx) {
|
||||
@@ -394,33 +384,34 @@ describe('ArchiveManager', function () {
|
||||
|
||||
describe('with an error in the file read stream', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
return resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.readStream.emit('error', new Error('Something went wrong'))
|
||||
})
|
||||
ctx.readStream = new PassThrough()
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.readStream.emit('error', new Error('Something went wrong'))
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
ctx.callback.should.have.been.calledWithExactly(
|
||||
sinon.match
|
||||
.instanceOf(Error)
|
||||
.and(sinon.match.has('message', 'Something went wrong'))
|
||||
)
|
||||
it('should reject with an error', function (ctx) {
|
||||
expect(ctx.error)
|
||||
.to.be.instanceOf(Error)
|
||||
.and.have.property('message', 'Something went wrong')
|
||||
})
|
||||
|
||||
it('should close the zipfile', function (ctx) {
|
||||
@@ -430,34 +421,35 @@ describe('ArchiveManager', function () {
|
||||
|
||||
describe('with an error in the file write stream', function () {
|
||||
beforeEach(async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.readStream = new PassThrough()
|
||||
sinon.spy(ctx.readStream, 'destroy')
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
ctx.ArchiveManager.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination,
|
||||
error => {
|
||||
ctx.callback(error)
|
||||
return resolve()
|
||||
}
|
||||
)
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.writeStream.emit('error', new Error('Something went wrong'))
|
||||
})
|
||||
ctx.readStream = new PassThrough()
|
||||
sinon.spy(ctx.readStream, 'destroy')
|
||||
ctx.zipfile.openReadStream = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, ctx.readStream)
|
||||
ctx.writeStream = new PassThrough()
|
||||
ctx.fs.createWriteStream = sinon.stub().returns(ctx.writeStream)
|
||||
sinon.spy(ctx.writeStream, 'destroy')
|
||||
|
||||
const promise = ctx.ArchiveManager.promises.extractZipArchive(
|
||||
ctx.source,
|
||||
ctx.destination
|
||||
)
|
||||
await Promise.resolve()
|
||||
|
||||
ctx.zipfile.emit('entry', { fileName: 'testfile.txt' })
|
||||
ctx.writeStream.emit('error', new Error('Something went wrong'))
|
||||
|
||||
try {
|
||||
await promise
|
||||
} catch (error) {
|
||||
ctx.error = error
|
||||
}
|
||||
})
|
||||
|
||||
it('should return the callback with an error', function (ctx) {
|
||||
ctx.callback.should.have.been.calledWithExactly(
|
||||
sinon.match
|
||||
.instanceOf(Error)
|
||||
.and(sinon.match.has('message', 'Something went wrong'))
|
||||
)
|
||||
it('should reject with an error', function (ctx) {
|
||||
expect(ctx.error)
|
||||
.to.be.instanceOf(Error)
|
||||
.and.have.property('message', 'Something went wrong')
|
||||
})
|
||||
it('should destroy the readstream', function (ctx) {
|
||||
ctx.readStream.destroy.called.should.equal(true)
|
||||
@@ -471,122 +463,123 @@ describe('ArchiveManager', function () {
|
||||
|
||||
describe('_isZipTooLarge', function () {
|
||||
it('should return false with small output', async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
|
||||
expect(error).not.to.exist
|
||||
isTooLarge.should.equal(false)
|
||||
resolve()
|
||||
})
|
||||
ctx.zipfile.emit('entry', { uncompressedSize: 109042 })
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager._isZipTooLarge(ctx.source)
|
||||
ctx.zipfile.emit('entry', { uncompressedSize: 109042 })
|
||||
ctx.zipfile.emit('end')
|
||||
const isTooLarge = await promise
|
||||
isTooLarge.should.equal(false)
|
||||
})
|
||||
|
||||
it('should return true with large bytes', async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
|
||||
expect(error).not.to.exist
|
||||
isTooLarge.should.equal(true)
|
||||
resolve()
|
||||
})
|
||||
ctx.zipfile.emit('entry', { uncompressedSize: 109e16 })
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager._isZipTooLarge(ctx.source)
|
||||
ctx.zipfile.emit('entry', { uncompressedSize: 109e16 })
|
||||
ctx.zipfile.emit('end')
|
||||
const isTooLarge = await promise
|
||||
isTooLarge.should.equal(true)
|
||||
})
|
||||
|
||||
it('should return error on no data', async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
|
||||
expect(error).to.exist
|
||||
resolve()
|
||||
})
|
||||
ctx.zipfile.emit('entry', {})
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager._isZipTooLarge(ctx.source)
|
||||
ctx.zipfile.emit('entry', {})
|
||||
ctx.zipfile.emit('end')
|
||||
await expect(promise).to.be.rejectedWith(
|
||||
ArchiveErrors.InvalidZipFileError
|
||||
)
|
||||
})
|
||||
|
||||
it("should return error if it didn't get a number", async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
|
||||
expect(error).to.exist
|
||||
resolve()
|
||||
})
|
||||
ctx.zipfile.emit('entry', { uncompressedSize: 'random-error' })
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager._isZipTooLarge(ctx.source)
|
||||
ctx.zipfile.emit('entry', { uncompressedSize: 'random-error' })
|
||||
ctx.zipfile.emit('end')
|
||||
await expect(promise).to.be.rejectedWith(
|
||||
ArchiveErrors.InvalidZipFileError
|
||||
)
|
||||
})
|
||||
|
||||
it('should return error if there is no data', async function (ctx) {
|
||||
await new Promise(resolve => {
|
||||
ctx.ArchiveManager._isZipTooLarge(ctx.source, (error, isTooLarge) => {
|
||||
expect(error).to.exist
|
||||
resolve()
|
||||
})
|
||||
ctx.zipfile.emit('end')
|
||||
})
|
||||
const promise = ctx.ArchiveManager._isZipTooLarge(ctx.source)
|
||||
ctx.zipfile.emit('end')
|
||||
await expect(promise).to.be.rejectedWith(
|
||||
ArchiveErrors.InvalidZipFileError
|
||||
)
|
||||
})
|
||||
|
||||
it('should return true and close zipfile when entryCount exceeds maxEntitiesPerProject', async function (ctx) {
|
||||
ctx.zipfile.entryCount = 100
|
||||
ctx.Settings.maxEntitiesPerProject = 50
|
||||
const isTooLarge = await ctx.ArchiveManager._isZipTooLarge(ctx.source)
|
||||
isTooLarge.should.equal(true)
|
||||
ctx.zipfile.close.called.should.equal(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('findTopLevelDirectory', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.fs.readdir = sinon.stub()
|
||||
ctx.fs.stat((ctx.directory = 'test/directory'))
|
||||
ctx.directory = 'test/directory'
|
||||
})
|
||||
|
||||
describe('with multiple files', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.fs.readdir.callsArgWith(1, null, ['multiple', 'files'])
|
||||
ctx.ArchiveManager.findTopLevelDirectory(ctx.directory, ctx.callback)
|
||||
beforeEach(async function (ctx) {
|
||||
ctx.fsPromises.readdir.resolves(['multiple', 'files'])
|
||||
ctx.result = await ctx.ArchiveManager.promises.findTopLevelDirectory(
|
||||
ctx.directory
|
||||
)
|
||||
})
|
||||
|
||||
it('should find the files in the directory', function (ctx) {
|
||||
ctx.fs.readdir.calledWith(ctx.directory).should.equal(true)
|
||||
ctx.fsPromises.readdir.calledWith(ctx.directory).should.equal(true)
|
||||
})
|
||||
|
||||
it('should return the original directory', function (ctx) {
|
||||
ctx.callback.calledWith(null, ctx.directory).should.equal(true)
|
||||
ctx.result.should.equal(ctx.directory)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with a single file (not folder)', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.fs.readdir.callsArgWith(1, null, ['foo.tex'])
|
||||
ctx.fs.stat.callsArgWith(1, null, {
|
||||
beforeEach(async function (ctx) {
|
||||
ctx.fsPromises.readdir.resolves(['foo.tex'])
|
||||
ctx.fsPromises.stat.resolves({
|
||||
isDirectory() {
|
||||
return false
|
||||
},
|
||||
})
|
||||
ctx.ArchiveManager.findTopLevelDirectory(ctx.directory, ctx.callback)
|
||||
ctx.result = await ctx.ArchiveManager.promises.findTopLevelDirectory(
|
||||
ctx.directory
|
||||
)
|
||||
})
|
||||
|
||||
it('should check if the file is a directory', function (ctx) {
|
||||
ctx.fs.stat.calledWith(ctx.directory + '/foo.tex').should.equal(true)
|
||||
ctx.fsPromises.stat
|
||||
.calledWith(ctx.directory + '/foo.tex')
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should return the original directory', function (ctx) {
|
||||
ctx.callback.calledWith(null, ctx.directory).should.equal(true)
|
||||
ctx.result.should.equal(ctx.directory)
|
||||
})
|
||||
})
|
||||
|
||||
describe('with a single top-level folder', function () {
|
||||
beforeEach(function (ctx) {
|
||||
ctx.fs.readdir.callsArgWith(1, null, ['folder'])
|
||||
ctx.fs.stat.callsArgWith(1, null, {
|
||||
beforeEach(async function (ctx) {
|
||||
ctx.fsPromises.readdir.resolves(['folder'])
|
||||
ctx.fsPromises.stat.resolves({
|
||||
isDirectory() {
|
||||
return true
|
||||
},
|
||||
})
|
||||
ctx.ArchiveManager.findTopLevelDirectory(ctx.directory, ctx.callback)
|
||||
ctx.result = await ctx.ArchiveManager.promises.findTopLevelDirectory(
|
||||
ctx.directory
|
||||
)
|
||||
})
|
||||
|
||||
it('should check if the file is a directory', function (ctx) {
|
||||
ctx.fs.stat.calledWith(ctx.directory + '/folder').should.equal(true)
|
||||
ctx.fsPromises.stat
|
||||
.calledWith(ctx.directory + '/folder')
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should return the child directory', function (ctx) {
|
||||
ctx.callback
|
||||
.calledWith(null, ctx.directory + '/folder')
|
||||
.should.equal(true)
|
||||
ctx.result.should.equal(ctx.directory + '/folder')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user