2020-02-19 06:15:25 -05:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// 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:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* 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')
|
2020-02-19 06:15:37 -05:00
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/ResourceStateManager'
|
|
|
|
)
|
|
|
|
const Path = require('path')
|
|
|
|
const Errors = require('../../../app/js/Errors')
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('ResourceStateManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.ResourceStateManager = 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 = {}),
|
2021-07-13 07:04:48 -04:00
|
|
|
'./SafeReader': (this.SafeReader = {}),
|
|
|
|
},
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
this.basePath = '/path/to/write/files/to'
|
|
|
|
this.resources = [
|
|
|
|
{ path: 'resource-1-mock' },
|
|
|
|
{ path: 'resource-2-mock' },
|
2021-07-13 07:04:48 -04:00
|
|
|
{ path: 'resource-3-mock' },
|
2020-02-19 06:15:37 -05:00
|
|
|
]
|
|
|
|
this.state = '1234567890'
|
|
|
|
this.resourceFileName = `${this.basePath}/.project-sync-state`
|
|
|
|
this.resourceFileContents = `${this.resources[0].path}\n${this.resources[1].path}\n${this.resources[2].path}\nstateHash:${this.state}`
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('saveProjectState', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.fs.writeFile = sinon.stub().callsArg(2))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when the state is specified', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceStateManager.saveProjectState(
|
|
|
|
this.state,
|
|
|
|
this.resources,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should write the resource list to disk', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.writeFile
|
|
|
|
.calledWith(this.resourceFileName, this.resourceFileContents)
|
|
|
|
.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('when the state is undefined', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.state = undefined
|
|
|
|
this.fs.unlink = sinon.stub().callsArg(1)
|
|
|
|
return this.ResourceStateManager.saveProjectState(
|
|
|
|
this.state,
|
|
|
|
this.resources,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should unlink the resource file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.unlink
|
|
|
|
.calledWith(this.resourceFileName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should not write the resource list 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
|
|
|
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('checkProjectStateMatches', function () {
|
|
|
|
describe('when the state matches', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.SafeReader.readFile = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.resourceFileContents)
|
|
|
|
return this.ResourceStateManager.checkProjectStateMatches(
|
|
|
|
this.state,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should read the resource file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.SafeReader.readFile
|
|
|
|
.calledWith(this.resourceFileName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with the results', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback
|
|
|
|
.calledWithMatch(null, this.resources)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-26 06:03:18 -05:00
|
|
|
describe('when the state file is not present', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
this.SafeReader.readFile = sinon.stub().callsArg(3)
|
|
|
|
return this.ResourceStateManager.checkProjectStateMatches(
|
|
|
|
this.state,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should read the resource file', function () {
|
|
|
|
return this.SafeReader.readFile
|
|
|
|
.calledWith(this.resourceFileName)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call the callback with an error', function () {
|
|
|
|
this.callback
|
|
|
|
.calledWith(sinon.match(Errors.FilesOutOfSyncError))
|
|
|
|
.should.equal(true)
|
|
|
|
|
|
|
|
const message = this.callback.args[0][0].message
|
|
|
|
expect(message).to.include('invalid state for incremental update')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when the state does not match', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.SafeReader.readFile = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(3, null, this.resourceFileContents)
|
|
|
|
return this.ResourceStateManager.checkProjectStateMatches(
|
|
|
|
'not-the-original-state',
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback
|
|
|
|
.calledWith(sinon.match(Errors.FilesOutOfSyncError))
|
|
|
|
.should.equal(true)
|
|
|
|
|
|
|
|
const message = this.callback.args[0][0].message
|
|
|
|
expect(message).to.include('invalid state for incremental update')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('checkResourceFiles', function () {
|
|
|
|
describe('when all the files are present', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.allFiles = [
|
|
|
|
this.resources[0].path,
|
|
|
|
this.resources[1].path,
|
2021-07-13 07:04:48 -04:00
|
|
|
this.resources[2].path,
|
2020-02-19 06:15:37 -05:00
|
|
|
]
|
|
|
|
return this.ResourceStateManager.checkResourceFiles(
|
|
|
|
this.resources,
|
|
|
|
this.allFiles,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
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.calledWithExactly().should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('when there is a missing file', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.allFiles = [this.resources[0].path, this.resources[1].path]
|
|
|
|
this.fs.stat = sinon.stub().callsArgWith(1, new Error())
|
|
|
|
return this.ResourceStateManager.checkResourceFiles(
|
|
|
|
this.resources,
|
|
|
|
this.allFiles,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with an error', function () {
|
2020-03-12 05:35:11 -04:00
|
|
|
this.callback
|
|
|
|
.calledWith(sinon.match(Errors.FilesOutOfSyncError))
|
|
|
|
.should.equal(true)
|
|
|
|
|
|
|
|
const message = this.callback.args[0][0].message
|
|
|
|
expect(message).to.include(
|
2020-02-19 06:15:37 -05:00
|
|
|
'resource files missing in incremental update'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('when a resource contains a relative path', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resources[0].path = '../foo/bar.tex'
|
|
|
|
this.allFiles = [
|
|
|
|
this.resources[0].path,
|
|
|
|
this.resources[1].path,
|
2021-07-13 07:04:48 -04:00
|
|
|
this.resources[2].path,
|
2020-02-19 06:15:37 -05:00
|
|
|
]
|
|
|
|
return this.ResourceStateManager.checkResourceFiles(
|
|
|
|
this.resources,
|
|
|
|
this.allFiles,
|
|
|
|
this.basePath,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should call the callback with 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('relative path in resource file list')
|
2020-02-19 06:15:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|