2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 06:15:08 -05:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* 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
|
|
|
|
*/
|
2020-02-19 06:15:37 -05:00
|
|
|
const SandboxedModule = require('sandboxed-module')
|
|
|
|
const sinon = require('sinon')
|
2020-03-12 05:35:11 -04:00
|
|
|
const { expect } = require('chai')
|
2024-11-08 05:21:56 -05:00
|
|
|
const modulePath = require('node:path').join(
|
2020-02-19 06:15:37 -05:00
|
|
|
__dirname,
|
|
|
|
'../../../app/js/ResourceWriter'
|
|
|
|
)
|
2024-11-08 05:21:56 -05:00
|
|
|
const path = require('node:path')
|
2020-02-19 06:15:37 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('ResourceWriter', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
let Timer
|
|
|
|
this.ResourceWriter = SandboxedModule.require(modulePath, {
|
2020-03-16 12:14:04 -04:00
|
|
|
singleOnly: true,
|
2020-02-19 06:15:37 -05:00
|
|
|
requires: {
|
|
|
|
fs: (this.fs = {
|
|
|
|
mkdir: sinon.stub().callsArg(1),
|
2021-07-13 07:04:48 -04:00
|
|
|
unlink: sinon.stub().callsArg(1),
|
2020-02-19 06:15:37 -05:00
|
|
|
}),
|
|
|
|
'./ResourceStateManager': (this.ResourceStateManager = {}),
|
2021-10-06 04:11:59 -04:00
|
|
|
'./UrlCache': (this.UrlCache = {
|
|
|
|
createProjectDir: sinon.stub().yields(),
|
|
|
|
}),
|
2020-02-19 06:15:37 -05:00
|
|
|
'./OutputFileFinder': (this.OutputFileFinder = {}),
|
|
|
|
'./Metrics': (this.Metrics = {
|
2020-03-26 06:18:50 -04:00
|
|
|
inc: sinon.stub(),
|
2020-08-10 12:01:11 -04:00
|
|
|
Timer: (Timer = (function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
Timer = class Timer {
|
|
|
|
static initClass() {
|
|
|
|
this.prototype.done = sinon.stub()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Timer.initClass()
|
|
|
|
return Timer
|
2021-07-13 07:04:48 -04:00
|
|
|
})()),
|
|
|
|
}),
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
this.project_id = 'project-id-123'
|
|
|
|
this.basePath = '/path/to/write/files/to'
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('syncResourcesToDisk on a full request', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resources = ['resource-1-mock', 'resource-2-mock', 'resource-3-mock']
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request = {
|
|
|
|
project_id: this.project_id,
|
|
|
|
syncState: (this.syncState = '0123456789abcdef'),
|
|
|
|
resources: this.resources,
|
|
|
|
}
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
|
2022-03-01 10:09:55 -05:00
|
|
|
this.ResourceWriter._removeExtraneousFiles = sinon.stub().yields(null)
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
|
|
|
|
return this.ResourceWriter.syncResourcesToDisk(
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should remove old files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._removeExtraneousFiles
|
2022-03-01 10:09:55 -05:00
|
|
|
.calledWith(this.request, this.resources, this.basePath)
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should write each resource to disk', function () {
|
2021-07-13 07:04:48 -04:00
|
|
|
return Array.from(this.resources).map(resource =>
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceWriter._writeResourceToDisk
|
|
|
|
.calledWith(this.project_id, resource, this.basePath)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should store the sync state and resource list', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceStateManager.saveProjectState
|
|
|
|
.calledWith(this.syncState, this.resources, this.basePath)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('syncResourcesToDisk on an incremental update', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resources = ['resource-1-mock']
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request = {
|
|
|
|
project_id: this.project_id,
|
|
|
|
syncType: 'incremental',
|
|
|
|
syncState: (this.syncState = '1234567890abcdef'),
|
|
|
|
resources: this.resources,
|
|
|
|
}
|
|
|
|
this.fullResources = this.resources.concat(['file-1'])
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
|
|
|
|
this.ResourceWriter._removeExtraneousFiles = sinon
|
|
|
|
.stub()
|
2022-03-01 10:09:55 -05:00
|
|
|
.yields(null, (this.outputFiles = []), (this.allFiles = []))
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceStateManager.checkProjectStateMatches = sinon
|
|
|
|
.stub()
|
2022-03-01 10:09:55 -05:00
|
|
|
.callsArgWith(2, null, this.fullResources)
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceStateManager.saveProjectState = sinon.stub().callsArg(3)
|
|
|
|
this.ResourceStateManager.checkResourceFiles = sinon.stub().callsArg(3)
|
|
|
|
return this.ResourceWriter.syncResourcesToDisk(
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should check the sync state matches', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceStateManager.checkProjectStateMatches
|
|
|
|
.calledWith(this.syncState, this.basePath)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should remove old files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._removeExtraneousFiles
|
2022-03-01 10:09:55 -05:00
|
|
|
.calledWith(this.request, this.fullResources, this.basePath)
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should check each resource exists', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceStateManager.checkResourceFiles
|
2022-03-01 10:09:55 -05:00
|
|
|
.calledWith(this.fullResources, this.allFiles, this.basePath)
|
2020-02-19 06:15:37 -05:00
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should write each resource to disk', function () {
|
2021-07-13 07:04:48 -04:00
|
|
|
return Array.from(this.resources).map(resource =>
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceWriter._writeResourceToDisk
|
|
|
|
.calledWith(this.project_id, resource, this.basePath)
|
|
|
|
.should.equal(true)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('syncResourcesToDisk on an incremental update when the state does not match', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resources = ['resource-1-mock']
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request = {
|
|
|
|
project_id: this.project_id,
|
|
|
|
syncType: 'incremental',
|
|
|
|
syncState: (this.syncState = '1234567890abcdef'),
|
|
|
|
resources: this.resources,
|
|
|
|
}
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceStateManager.checkProjectStateMatches = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, (this.error = new Error()))
|
|
|
|
return this.ResourceWriter.syncResourcesToDisk(
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should check whether the sync state matches', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceStateManager.checkProjectStateMatches
|
|
|
|
.calledWith(this.syncState, this.basePath)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with an error', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(this.error).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_removeExtraneousFiles', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.output_files = [
|
|
|
|
{
|
|
|
|
path: 'output.pdf',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'pdf',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'extra/file.tex',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'tex',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'extra.aux',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'aux',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
path: 'cache/_chunk1',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'figures/image-eps-converted-to.pdf',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'pdf',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'foo/main-figure0.md5',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'md5',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'foo/main-figure0.dpth',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'dpth',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: 'foo/main-figure0.pdf',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'pdf',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '_minted-main/default-pyg-prefix.pygstyle',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'pygstyle',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '_minted-main/default.pygstyle',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'pygstyle',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
path: '_minted-main/35E248B60965545BD232AE9F0FE9750D504A7AF0CD3BAA7542030FC560DFCC45.pygtex',
|
|
|
|
type: 'pygtex',
|
2020-02-19 06:15:37 -05:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '_markdown_main/30893013dec5d869a415610079774c2f.md.tex',
|
2021-07-13 07:04:48 -04:00
|
|
|
type: 'tex',
|
2020-05-20 09:12:08 -04:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
path: 'output.stdout',
|
2020-05-20 09:12:08 -04:00
|
|
|
},
|
|
|
|
{
|
2021-07-13 07:04:48 -04:00
|
|
|
path: 'output.stderr',
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
]
|
|
|
|
this.resources = 'mock-resources'
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request = {
|
|
|
|
project_id: this.project_id,
|
|
|
|
syncType: 'incremental',
|
|
|
|
syncState: (this.syncState = '1234567890abcdef'),
|
|
|
|
resources: this.resources,
|
|
|
|
}
|
2020-02-19 06:15:37 -05:00
|
|
|
this.OutputFileFinder.findOutputFiles = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, this.output_files)
|
|
|
|
this.ResourceWriter._deleteFileIfNotDirectory = sinon.stub().callsArg(1)
|
|
|
|
return this.ResourceWriter._removeExtraneousFiles(
|
2022-03-01 10:09:55 -05:00
|
|
|
this.request,
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resources,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should find the existing output files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.OutputFileFinder.findOutputFiles
|
|
|
|
.calledWith(this.resources, this.basePath)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should delete the output files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'output.pdf'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should delete the stdout log file', function () {
|
2020-05-20 09:12:08 -04:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'output.stdout'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should delete the stderr log file', function () {
|
2020-05-20 09:12:08 -04:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'output.stderr'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should delete the extra files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'extra/file.tex'))
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the extra aux files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'extra.aux'))
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the knitr cache file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'cache/_chunk1'))
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the epstopdf converted files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(
|
|
|
|
path.join(this.basePath, 'figures/image-eps-converted-to.pdf')
|
|
|
|
)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the tikz md5 files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'foo/main-figure0.md5'))
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the tikz dpth files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'foo/main-figure0.dpth'))
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the tikz pdf files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, 'foo/main-figure0.pdf'))
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the minted pygstyle files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(
|
|
|
|
path.join(this.basePath, '_minted-main/default-pyg-prefix.pygstyle')
|
|
|
|
)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the minted default pygstyle files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(this.basePath, '_minted-main/default.pygstyle'))
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the minted default pygtex files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(
|
|
|
|
path.join(
|
|
|
|
this.basePath,
|
|
|
|
'_minted-main/35E248B60965545BD232AE9F0FE9750D504A7AF0CD3BAA7542030FC560DFCC45.pygtex'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not delete the markdown md.tex files', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(
|
|
|
|
path.join(
|
|
|
|
this.basePath,
|
|
|
|
'_markdown_main/30893013dec5d869a415610079774c2f.md.tex'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should time the request', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.Metrics.Timer.prototype.done.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('_writeResourceToDisk', function () {
|
|
|
|
describe('with a url based resource', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-12 05:22:08 -04:00
|
|
|
this.fs.mkdir = sinon.stub().callsArg(2)
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resource = {
|
|
|
|
path: 'main.tex',
|
|
|
|
url: 'http://www.example.com/main.tex',
|
2021-07-13 07:04:48 -04:00
|
|
|
modified: Date.now(),
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
this.UrlCache.downloadUrlToFile = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(4, 'fake error downloading file')
|
|
|
|
return this.ResourceWriter._writeResourceToDisk(
|
|
|
|
this.project_id,
|
|
|
|
this.resource,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should ensure the directory exists', function () {
|
2020-03-12 05:22:08 -04:00
|
|
|
this.fs.mkdir
|
2020-02-19 06:15:37 -05:00
|
|
|
.calledWith(
|
|
|
|
path.dirname(path.join(this.basePath, this.resource.path))
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should write the URL from the cache', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.UrlCache.downloadUrlToFile
|
|
|
|
.calledWith(
|
|
|
|
this.project_id,
|
|
|
|
this.resource.url,
|
|
|
|
path.join(this.basePath, this.resource.path),
|
|
|
|
this.resource.modified
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should not return an error if the resource writer errored', function () {
|
2021-03-12 16:18:36 -05:00
|
|
|
return expect(this.callback.args[0][0]).not.to.exist
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with a content based resource', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resource = {
|
|
|
|
path: 'main.tex',
|
2021-07-13 07:04:48 -04:00
|
|
|
content: 'Hello world',
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
this.fs.writeFile = sinon.stub().callsArg(2)
|
2020-03-12 05:22:08 -04:00
|
|
|
this.fs.mkdir = sinon.stub().callsArg(2)
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter._writeResourceToDisk(
|
|
|
|
this.project_id,
|
|
|
|
this.resource,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should ensure the directory exists', function () {
|
2020-03-12 05:22:08 -04:00
|
|
|
return this.fs.mkdir
|
2020-02-19 06:15:37 -05:00
|
|
|
.calledWith(
|
|
|
|
path.dirname(path.join(this.basePath, this.resource.path))
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should write the contents to disk', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.writeFile
|
|
|
|
.calledWith(
|
|
|
|
path.join(this.basePath, this.resource.path),
|
|
|
|
this.resource.content
|
|
|
|
)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.called.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('with a file path that breaks out of the root folder', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resource = {
|
|
|
|
path: '../../main.tex',
|
2021-07-13 07:04:48 -04:00
|
|
|
content: 'Hello world',
|
2020-02-19 06:15:37 -05:00
|
|
|
}
|
|
|
|
this.fs.writeFile = sinon.stub().callsArg(2)
|
|
|
|
return this.ResourceWriter._writeResourceToDisk(
|
|
|
|
this.project_id,
|
|
|
|
this.resource,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not write to disk', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.writeFile.called.should.equal(false)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should return an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
|
|
|
|
|
|
|
const message = this.callback.args[0][0].message
|
|
|
|
expect(message).to.include('resource path is outside root directory')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('checkPath', function () {
|
|
|
|
describe('with a valid path', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter.checkPath('foo', 'bar', this.callback)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should return the joined path', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWith(null, 'foo/bar').should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with an invalid path', function () {
|
|
|
|
beforeEach(function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.ResourceWriter.checkPath('foo', 'baz/../../bar', this.callback)
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should return an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
|
|
|
|
|
|
|
const message = this.callback.args[0][0].message
|
|
|
|
expect(message).to.include('resource path is outside root directory')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('with another invalid path matching on a prefix', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter.checkPath(
|
|
|
|
'foo',
|
|
|
|
'../foobar/baz',
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should return an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback.calledWith(sinon.match(Error)).should.equal(true)
|
|
|
|
|
|
|
|
const message = this.callback.args[0][0].message
|
|
|
|
expect(message).to.include('resource path is outside root directory')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|