TikzManager.js 5.1 KB

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