TikzManager.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* eslint-disable
  2. no-return-assign,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  10. */
  11. const SandboxedModule = require('sandboxed-module')
  12. const sinon = require('sinon')
  13. require('chai').should()
  14. const modulePath = require('path').join(
  15. __dirname,
  16. '../../../app/js/TikzManager'
  17. )
  18. describe('TikzManager', function () {
  19. beforeEach(function () {
  20. return (this.TikzManager = SandboxedModule.require(modulePath, {
  21. requires: {
  22. './ResourceWriter': (this.ResourceWriter = {}),
  23. './SafeReader': (this.SafeReader = {}),
  24. fs: (this.fs = {}),
  25. 'logger-sharelatex': (this.logger = { log() {} })
  26. }
  27. }))
  28. })
  29. describe('checkMainFile', function () {
  30. beforeEach(function () {
  31. this.compileDir = 'compile-dir'
  32. this.mainFile = 'main.tex'
  33. return (this.callback = sinon.stub())
  34. })
  35. describe('if there is already an output.tex file in the resources', function () {
  36. beforeEach(function () {
  37. this.resources = [{ path: 'main.tex' }, { path: 'output.tex' }]
  38. return this.TikzManager.checkMainFile(
  39. this.compileDir,
  40. this.mainFile,
  41. this.resources,
  42. this.callback
  43. )
  44. })
  45. return it('should call the callback with false ', function () {
  46. return this.callback.calledWithExactly(null, false).should.equal(true)
  47. })
  48. })
  49. return describe('if there is no output.tex file in the resources', function () {
  50. beforeEach(function () {
  51. this.resources = [{ path: 'main.tex' }]
  52. return (this.ResourceWriter.checkPath = sinon
  53. .stub()
  54. .withArgs(this.compileDir, this.mainFile)
  55. .callsArgWith(2, null, `${this.compileDir}/${this.mainFile}`))
  56. })
  57. describe('and the main file contains tikzexternalize', function () {
  58. beforeEach(function () {
  59. this.SafeReader.readFile = sinon
  60. .stub()
  61. .withArgs(`${this.compileDir}/${this.mainFile}`)
  62. .callsArgWith(3, null, 'hello \\tikzexternalize')
  63. return this.TikzManager.checkMainFile(
  64. this.compileDir,
  65. this.mainFile,
  66. this.resources,
  67. this.callback
  68. )
  69. })
  70. it('should look at the file on disk', function () {
  71. return this.SafeReader.readFile
  72. .calledWith(`${this.compileDir}/${this.mainFile}`)
  73. .should.equal(true)
  74. })
  75. return it('should call the callback with true ', function () {
  76. return this.callback.calledWithExactly(null, true).should.equal(true)
  77. })
  78. })
  79. describe('and the main file does not contain tikzexternalize', function () {
  80. beforeEach(function () {
  81. this.SafeReader.readFile = sinon
  82. .stub()
  83. .withArgs(`${this.compileDir}/${this.mainFile}`)
  84. .callsArgWith(3, null, 'hello')
  85. return this.TikzManager.checkMainFile(
  86. this.compileDir,
  87. this.mainFile,
  88. this.resources,
  89. this.callback
  90. )
  91. })
  92. it('should look at the file on disk', function () {
  93. return this.SafeReader.readFile
  94. .calledWith(`${this.compileDir}/${this.mainFile}`)
  95. .should.equal(true)
  96. })
  97. return it('should call the callback with false', function () {
  98. return this.callback.calledWithExactly(null, false).should.equal(true)
  99. })
  100. })
  101. return describe('and the main file contains \\usepackage{pstool}', function () {
  102. beforeEach(function () {
  103. this.SafeReader.readFile = sinon
  104. .stub()
  105. .withArgs(`${this.compileDir}/${this.mainFile}`)
  106. .callsArgWith(3, null, 'hello \\usepackage[random-options]{pstool}')
  107. return this.TikzManager.checkMainFile(
  108. this.compileDir,
  109. this.mainFile,
  110. this.resources,
  111. this.callback
  112. )
  113. })
  114. it('should look at the file on disk', function () {
  115. return this.SafeReader.readFile
  116. .calledWith(`${this.compileDir}/${this.mainFile}`)
  117. .should.equal(true)
  118. })
  119. return it('should call the callback with true ', function () {
  120. return this.callback.calledWithExactly(null, true).should.equal(true)
  121. })
  122. })
  123. })
  124. })
  125. return describe('injectOutputFile', function () {
  126. beforeEach(function () {
  127. this.rootDir = '/mock'
  128. this.filename = 'filename.tex'
  129. this.callback = sinon.stub()
  130. this.content = `\
  131. \\documentclass{article}
  132. \\usepackage{tikz}
  133. \\tikzexternalize
  134. \\begin{document}
  135. Hello world
  136. \\end{document}\
  137. `
  138. this.fs.readFile = sinon.stub().callsArgWith(2, null, this.content)
  139. this.fs.writeFile = sinon.stub().callsArg(3)
  140. this.ResourceWriter.checkPath = sinon
  141. .stub()
  142. .callsArgWith(2, null, `${this.rootDir}/${this.filename}`)
  143. return this.TikzManager.injectOutputFile(
  144. this.rootDir,
  145. this.filename,
  146. this.callback
  147. )
  148. })
  149. it('sould check the path', function () {
  150. return this.ResourceWriter.checkPath
  151. .calledWith(this.rootDir, this.filename)
  152. .should.equal(true)
  153. })
  154. it('should read the file', function () {
  155. return this.fs.readFile
  156. .calledWith(`${this.rootDir}/${this.filename}`, 'utf8')
  157. .should.equal(true)
  158. })
  159. it('should write out the same file as output.tex', function () {
  160. return this.fs.writeFile
  161. .calledWith(`${this.rootDir}/output.tex`, this.content, { flag: 'wx' })
  162. .should.equal(true)
  163. })
  164. return it('should call the callback', function () {
  165. return this.callback.called.should.equal(true)
  166. })
  167. })
  168. })