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:
|
|
|
|
* 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')
|
|
|
|
require('chai').should()
|
|
|
|
const modulePath = require('path').join(
|
|
|
|
__dirname,
|
|
|
|
'../../../app/js/TikzManager'
|
|
|
|
)
|
2020-02-19 06:15:08 -05:00
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('TikzManager', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return (this.TikzManager = SandboxedModule.require(modulePath, {
|
|
|
|
requires: {
|
|
|
|
'./ResourceWriter': (this.ResourceWriter = {}),
|
|
|
|
'./SafeReader': (this.SafeReader = {}),
|
|
|
|
fs: (this.fs = {}),
|
|
|
|
'logger-sharelatex': (this.logger = { log() {} })
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('checkMainFile', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.compileDir = 'compile-dir'
|
|
|
|
this.mainFile = 'main.tex'
|
|
|
|
return (this.callback = sinon.stub())
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('if there is already an output.tex file in the resources', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resources = [{ path: 'main.tex' }, { path: 'output.tex' }]
|
|
|
|
return this.TikzManager.checkMainFile(
|
|
|
|
this.compileDir,
|
|
|
|
this.mainFile,
|
|
|
|
this.resources,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with false ', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWithExactly(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('if there is no output.tex file in the resources', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.resources = [{ path: 'main.tex' }]
|
|
|
|
return (this.ResourceWriter.checkPath = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(this.compileDir, this.mainFile)
|
|
|
|
.callsArgWith(2, null, `${this.compileDir}/${this.mainFile}`))
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('and the main file contains tikzexternalize', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.SafeReader.readFile = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(`${this.compileDir}/${this.mainFile}`)
|
|
|
|
.callsArgWith(3, null, 'hello \\tikzexternalize')
|
|
|
|
return this.TikzManager.checkMainFile(
|
|
|
|
this.compileDir,
|
|
|
|
this.mainFile,
|
|
|
|
this.resources,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should look at the file on disk', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.SafeReader.readFile
|
|
|
|
.calledWith(`${this.compileDir}/${this.mainFile}`)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with true ', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWithExactly(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
describe('and the main file does not contain tikzexternalize', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.SafeReader.readFile = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(`${this.compileDir}/${this.mainFile}`)
|
|
|
|
.callsArgWith(3, null, 'hello')
|
|
|
|
return this.TikzManager.checkMainFile(
|
|
|
|
this.compileDir,
|
|
|
|
this.mainFile,
|
|
|
|
this.resources,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should look at the file on disk', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.SafeReader.readFile
|
|
|
|
.calledWith(`${this.compileDir}/${this.mainFile}`)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with false', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWithExactly(null, false).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('and the main file contains \\usepackage{pstool}', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.SafeReader.readFile = sinon
|
|
|
|
.stub()
|
|
|
|
.withArgs(`${this.compileDir}/${this.mainFile}`)
|
|
|
|
.callsArgWith(3, null, 'hello \\usepackage[random-options]{pstool}')
|
|
|
|
return this.TikzManager.checkMainFile(
|
|
|
|
this.compileDir,
|
|
|
|
this.mainFile,
|
|
|
|
this.resources,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should look at the file on disk', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.SafeReader.readFile
|
|
|
|
.calledWith(`${this.compileDir}/${this.mainFile}`)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return it('should call the callback with true ', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.callback.calledWithExactly(null, true).should.equal(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
return describe('injectOutputFile', function () {
|
|
|
|
beforeEach(function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
this.rootDir = '/mock'
|
|
|
|
this.filename = 'filename.tex'
|
|
|
|
this.callback = sinon.stub()
|
|
|
|
this.content = `\
|
2020-02-19 06:15:08 -05:00
|
|
|
\\documentclass{article}
|
|
|
|
\\usepackage{tikz}
|
|
|
|
\\tikzexternalize
|
|
|
|
\\begin{document}
|
|
|
|
Hello world
|
|
|
|
\\end{document}\
|
2020-02-19 06:15:37 -05:00
|
|
|
`
|
|
|
|
this.fs.readFile = sinon.stub().callsArgWith(2, null, this.content)
|
|
|
|
this.fs.writeFile = sinon.stub().callsArg(3)
|
|
|
|
this.ResourceWriter.checkPath = sinon
|
|
|
|
.stub()
|
|
|
|
.callsArgWith(2, null, `${this.rootDir}/${this.filename}`)
|
|
|
|
return this.TikzManager.injectOutputFile(
|
|
|
|
this.rootDir,
|
|
|
|
this.filename,
|
|
|
|
this.callback
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('sould check the path', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.ResourceWriter.checkPath
|
|
|
|
.calledWith(this.rootDir, this.filename)
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should read the file', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.readFile
|
|
|
|
.calledWith(`${this.rootDir}/${this.filename}`, 'utf8')
|
|
|
|
.should.equal(true)
|
|
|
|
})
|
|
|
|
|
2020-08-10 12:01:11 -04:00
|
|
|
it('should write out the same file as output.tex', function () {
|
2020-02-19 06:15:37 -05:00
|
|
|
return this.fs.writeFile
|
|
|
|
.calledWith(`${this.rootDir}/output.tex`, this.content, { flag: 'wx' })
|
|
|
|
.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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|