2014-02-12 12:27:43 -05:00
|
|
|
SandboxedModule = require('sandboxed-module')
|
|
|
|
sinon = require('sinon')
|
2015-09-09 04:44:38 -04:00
|
|
|
should = require('chai').should()
|
2014-02-12 12:27:43 -05:00
|
|
|
modulePath = require('path').join __dirname, '../../../app/js/ResourceWriter'
|
|
|
|
path = require "path"
|
|
|
|
|
|
|
|
describe "ResourceWriter", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ResourceWriter = SandboxedModule.require modulePath, requires:
|
2017-08-09 10:22:44 -04:00
|
|
|
"fs": @fs =
|
|
|
|
mkdir: sinon.stub().callsArg(1)
|
|
|
|
unlink: sinon.stub().callsArg(1)
|
2017-08-18 05:22:17 -04:00
|
|
|
"./ResourceStateManager": @ResourceStateManager = {}
|
2014-02-12 12:27:43 -05:00
|
|
|
"wrench": @wrench = {}
|
|
|
|
"./UrlCache" : @UrlCache = {}
|
|
|
|
"mkdirp" : @mkdirp = sinon.stub().callsArg(1)
|
|
|
|
"./OutputFileFinder": @OutputFileFinder = {}
|
2017-06-20 03:25:50 -04:00
|
|
|
"logger-sharelatex": {log: sinon.stub(), err: sinon.stub()}
|
2014-02-12 12:27:43 -05:00
|
|
|
"./Metrics": @Metrics =
|
|
|
|
Timer: class Timer
|
|
|
|
done: sinon.stub()
|
|
|
|
@project_id = "project-id-123"
|
|
|
|
@basePath = "/path/to/write/files/to"
|
|
|
|
@callback = sinon.stub()
|
|
|
|
|
2017-08-17 11:59:37 -04:00
|
|
|
describe "syncResourcesToDisk on a full request", ->
|
2014-02-12 12:27:43 -05:00
|
|
|
beforeEach ->
|
|
|
|
@resources = [
|
|
|
|
"resource-1-mock"
|
|
|
|
"resource-2-mock"
|
|
|
|
"resource-3-mock"
|
|
|
|
]
|
|
|
|
@ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
|
|
|
|
@ResourceWriter._removeExtraneousFiles = sinon.stub().callsArg(2)
|
2017-08-18 05:22:17 -04:00
|
|
|
@ResourceStateManager.checkProjectStateHashMatches = sinon.stub().callsArg(2)
|
2017-09-07 06:54:38 -04:00
|
|
|
@ResourceStateManager.saveProjectStateHash = sinon.stub().callsArg(3)
|
2017-08-17 11:59:37 -04:00
|
|
|
@ResourceWriter.syncResourcesToDisk({
|
|
|
|
project_id: @project_id
|
|
|
|
syncState: @syncState = "0123456789abcdef"
|
|
|
|
resources: @resources
|
|
|
|
}, @basePath, @callback)
|
|
|
|
|
|
|
|
it "should remove old files", ->
|
|
|
|
@ResourceWriter._removeExtraneousFiles
|
|
|
|
.calledWith(@resources, @basePath)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should write each resource to disk", ->
|
|
|
|
for resource in @resources
|
|
|
|
@ResourceWriter._writeResourceToDisk
|
|
|
|
.calledWith(@project_id, resource, @basePath)
|
|
|
|
.should.equal true
|
|
|
|
|
2017-09-07 06:54:38 -04:00
|
|
|
it "should store the sync state and resource list", ->
|
2017-08-18 05:22:17 -04:00
|
|
|
@ResourceStateManager.saveProjectStateHash
|
2017-09-07 06:54:38 -04:00
|
|
|
.calledWith(@syncState, @resources, @basePath)
|
2017-08-17 11:59:37 -04:00
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "syncResourcesToDisk on an incremental update", ->
|
|
|
|
beforeEach ->
|
|
|
|
@resources = [
|
|
|
|
"resource-1-mock"
|
|
|
|
]
|
|
|
|
@ResourceWriter._writeResourceToDisk = sinon.stub().callsArg(3)
|
|
|
|
@ResourceWriter._removeExtraneousFiles = sinon.stub().callsArg(2)
|
2017-09-07 06:54:38 -04:00
|
|
|
@ResourceStateManager.checkProjectStateHashMatches = sinon.stub().callsArgWith(2, null, @resources)
|
|
|
|
@ResourceStateManager.saveProjectStateHash = sinon.stub().callsArg(3)
|
2017-08-17 11:59:37 -04:00
|
|
|
@ResourceWriter.syncResourcesToDisk({
|
|
|
|
project_id: @project_id,
|
|
|
|
syncType: "incremental",
|
|
|
|
syncState: @syncState = "1234567890abcdef",
|
|
|
|
resources: @resources
|
|
|
|
}, @basePath, @callback)
|
|
|
|
|
|
|
|
it "should check the sync state matches", ->
|
2017-08-18 05:22:17 -04:00
|
|
|
@ResourceStateManager.checkProjectStateHashMatches
|
2017-08-17 11:59:37 -04:00
|
|
|
.calledWith(@syncState, @basePath)
|
|
|
|
.should.equal true
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
it "should remove old files", ->
|
|
|
|
@ResourceWriter._removeExtraneousFiles
|
|
|
|
.calledWith(@resources, @basePath)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should write each resource to disk", ->
|
|
|
|
for resource in @resources
|
|
|
|
@ResourceWriter._writeResourceToDisk
|
|
|
|
.calledWith(@project_id, resource, @basePath)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
2017-08-18 05:22:17 -04:00
|
|
|
describe "syncResourcesToDisk on an incremental update when the state does not match", ->
|
|
|
|
beforeEach ->
|
|
|
|
@resources = [
|
|
|
|
"resource-1-mock"
|
|
|
|
]
|
|
|
|
@ResourceStateManager.checkProjectStateHashMatches = sinon.stub().callsArgWith(2, @error = new Error())
|
|
|
|
@ResourceWriter.syncResourcesToDisk({
|
|
|
|
project_id: @project_id,
|
|
|
|
syncType: "incremental",
|
|
|
|
syncState: @syncState = "1234567890abcdef",
|
|
|
|
resources: @resources
|
|
|
|
}, @basePath, @callback)
|
|
|
|
|
|
|
|
it "should check whether the sync state matches", ->
|
|
|
|
@ResourceStateManager.checkProjectStateHashMatches
|
|
|
|
.calledWith(@syncState, @basePath)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback with an error", ->
|
|
|
|
@callback.calledWith(@error).should.equal true
|
|
|
|
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
describe "_removeExtraneousFiles", ->
|
|
|
|
beforeEach ->
|
|
|
|
@output_files = [{
|
|
|
|
path: "output.pdf"
|
|
|
|
type: "pdf"
|
|
|
|
}, {
|
|
|
|
path: "extra/file.tex"
|
|
|
|
type: "tex"
|
|
|
|
}, {
|
|
|
|
path: "extra.aux"
|
|
|
|
type: "aux"
|
2016-09-22 09:14:29 -04:00
|
|
|
}, {
|
|
|
|
path: "cache/_chunk1"
|
2014-02-12 12:27:43 -05:00
|
|
|
}]
|
|
|
|
@resources = "mock-resources"
|
|
|
|
@OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, @output_files)
|
|
|
|
@ResourceWriter._deleteFileIfNotDirectory = sinon.stub().callsArg(1)
|
|
|
|
@ResourceWriter._removeExtraneousFiles(@resources, @basePath, @callback)
|
|
|
|
|
|
|
|
it "should find the existing output files", ->
|
|
|
|
@OutputFileFinder.findOutputFiles
|
|
|
|
.calledWith(@resources, @basePath)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should delete the output files", ->
|
|
|
|
@ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(@basePath, "output.pdf"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should delete the extra files", ->
|
|
|
|
@ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(@basePath, "extra/file.tex"))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should not delete the extra aux files", ->
|
|
|
|
@ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(@basePath, "extra.aux"))
|
|
|
|
.should.equal false
|
2016-09-22 09:14:29 -04:00
|
|
|
|
|
|
|
it "should not delete the knitr cache file", ->
|
|
|
|
@ResourceWriter._deleteFileIfNotDirectory
|
|
|
|
.calledWith(path.join(@basePath, "cache/_chunk1"))
|
|
|
|
.should.equal false
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
it "should time the request", ->
|
|
|
|
@Metrics.Timer::done.called.should.equal true
|
|
|
|
|
|
|
|
describe "_writeResourceToDisk", ->
|
|
|
|
describe "with a url based resource", ->
|
|
|
|
beforeEach ->
|
|
|
|
@resource =
|
|
|
|
path: "main.tex"
|
|
|
|
url: "http://www.example.com/main.tex"
|
|
|
|
modified: Date.now()
|
2015-09-09 04:44:38 -04:00
|
|
|
@UrlCache.downloadUrlToFile = sinon.stub().callsArgWith(4, "fake error downloading file")
|
2014-02-12 12:27:43 -05:00
|
|
|
@ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
|
|
|
|
|
|
|
|
it "should ensure the directory exists", ->
|
|
|
|
@mkdirp
|
|
|
|
.calledWith(path.dirname(path.join(@basePath, @resource.path)))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should write the URL from the cache", ->
|
|
|
|
@UrlCache.downloadUrlToFile
|
|
|
|
.calledWith(@project_id, @resource.url, path.join(@basePath, @resource.path), @resource.modified)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
2015-09-09 04:44:38 -04:00
|
|
|
it "should not return an error if the resource writer errored", ->
|
|
|
|
should.not.exist @callback.args[0][0]
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
describe "with a content based resource", ->
|
|
|
|
beforeEach ->
|
|
|
|
@resource =
|
|
|
|
path: "main.tex"
|
|
|
|
content: "Hello world"
|
|
|
|
@fs.writeFile = sinon.stub().callsArg(2)
|
|
|
|
@ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
|
|
|
|
|
|
|
|
it "should ensure the directory exists", ->
|
|
|
|
@mkdirp
|
|
|
|
.calledWith(path.dirname(path.join(@basePath, @resource.path)))
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should write the contents to disk", ->
|
|
|
|
@fs.writeFile
|
|
|
|
.calledWith(path.join(@basePath, @resource.path), @resource.content)
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
it "should call the callback", ->
|
|
|
|
@callback.called.should.equal true
|
|
|
|
|
|
|
|
describe "with a file path that breaks out of the root folder", ->
|
|
|
|
beforeEach ->
|
|
|
|
@resource =
|
|
|
|
path: "../../main.tex"
|
|
|
|
content: "Hello world"
|
|
|
|
@fs.writeFile = sinon.stub().callsArg(2)
|
|
|
|
@ResourceWriter._writeResourceToDisk(@project_id, @resource, @basePath, @callback)
|
|
|
|
|
|
|
|
it "should not write to disk", ->
|
|
|
|
@fs.writeFile.called.should.equal false
|
|
|
|
|
|
|
|
it "should return an error", ->
|
|
|
|
@callback
|
|
|
|
.calledWith(new Error("resource path is outside root directory"))
|
|
|
|
.should.equal true
|
|
|
|
|
2017-03-21 07:29:37 -04:00
|
|
|
describe "checkPath", ->
|
|
|
|
describe "with a valid path", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ResourceWriter.checkPath("foo", "bar", @callback)
|
2014-02-12 12:27:43 -05:00
|
|
|
|
2017-03-21 07:29:37 -04:00
|
|
|
it "should return the joined path", ->
|
|
|
|
@callback.calledWith(null, "foo/bar")
|
|
|
|
.should.equal true
|
|
|
|
|
|
|
|
describe "with an invalid path", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ResourceWriter.checkPath("foo", "baz/../../bar", @callback)
|
|
|
|
|
|
|
|
it "should return an error", ->
|
|
|
|
@callback.calledWith(new Error("resource path is outside root directory"))
|
|
|
|
.should.equal true
|
2017-03-21 07:30:32 -04:00
|
|
|
|
|
|
|
describe "with another invalid path matching on a prefix", ->
|
|
|
|
beforeEach ->
|
|
|
|
@ResourceWriter.checkPath("foo", "../foobar/baz", @callback)
|
|
|
|
|
|
|
|
it "should return an error", ->
|
|
|
|
@callback.calledWith(new Error("resource path is outside root directory"))
|
|
|
|
.should.equal true
|