TikzManager.js 5.8 KB

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