CompileManagerTests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* eslint-disable
  2. camelcase,
  3. chai-friendly/no-unused-expressions,
  4. no-path-concat,
  5. no-return-assign,
  6. no-unused-vars,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. const SandboxedModule = require('sandboxed-module');
  17. const sinon = require('sinon');
  18. require('chai').should();
  19. const modulePath = require('path').join(__dirname, '../../../app/js/CompileManager');
  20. const tk = require("timekeeper");
  21. const { EventEmitter } = require("events");
  22. const Path = require("path");
  23. describe("CompileManager", function() {
  24. beforeEach(function() {
  25. this.CompileManager = SandboxedModule.require(modulePath, { requires: {
  26. "./LatexRunner": (this.LatexRunner = {}),
  27. "./ResourceWriter": (this.ResourceWriter = {}),
  28. "./OutputFileFinder": (this.OutputFileFinder = {}),
  29. "./OutputCacheManager": (this.OutputCacheManager = {}),
  30. "settings-sharelatex": (this.Settings = {
  31. path: {
  32. compilesDir: "/compiles/dir"
  33. },
  34. synctexBaseDir() { return "/compile"; },
  35. clsi: {
  36. docker: {
  37. image: "SOMEIMAGE"
  38. }
  39. }
  40. }),
  41. "logger-sharelatex": (this.logger = { log: sinon.stub() , info() {}}),
  42. "child_process": (this.child_process = {}),
  43. "./CommandRunner": (this.CommandRunner = {}),
  44. "./DraftModeManager": (this.DraftModeManager = {}),
  45. "./TikzManager": (this.TikzManager = {}),
  46. "./LockManager": (this.LockManager = {}),
  47. "fs": (this.fs = {}),
  48. "fs-extra": (this.fse = { ensureDir: sinon.stub().callsArg(1) })
  49. }
  50. });
  51. this.callback = sinon.stub();
  52. this.project_id = "project-id-123";
  53. return this.user_id = "1234";
  54. });
  55. describe("doCompileWithLock", function() {
  56. beforeEach(function() {
  57. this.request = {
  58. resources: (this.resources = "mock-resources"),
  59. project_id: this.project_id,
  60. user_id: this.user_id
  61. };
  62. this.output_files = ["foo", "bar"];
  63. this.Settings.compileDir = "compiles";
  64. this.compileDir = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`;
  65. this.CompileManager.doCompile = sinon.stub().callsArgWith(1, null, this.output_files);
  66. return this.LockManager.runWithLock = (lockFile, runner, callback) =>
  67. runner((err, ...result) => callback(err, ...Array.from(result)))
  68. ;
  69. });
  70. describe("when the project is not locked", function() {
  71. beforeEach(function() {
  72. return this.CompileManager.doCompileWithLock(this.request, this.callback);
  73. });
  74. it("should ensure that the compile directory exists", function() {
  75. return this.fse.ensureDir.calledWith(this.compileDir)
  76. .should.equal(true);
  77. });
  78. it("should call doCompile with the request", function() {
  79. return this.CompileManager.doCompile
  80. .calledWith(this.request)
  81. .should.equal(true);
  82. });
  83. return it("should call the callback with the output files", function() {
  84. return this.callback.calledWithExactly(null, this.output_files)
  85. .should.equal(true);
  86. });
  87. });
  88. return describe("when the project is locked", function() {
  89. beforeEach(function() {
  90. this.error = new Error("locked");
  91. this.LockManager.runWithLock = (lockFile, runner, callback) => {
  92. return callback(this.error);
  93. };
  94. return this.CompileManager.doCompileWithLock(this.request, this.callback);
  95. });
  96. it("should ensure that the compile directory exists", function() {
  97. return this.fse.ensureDir.calledWith(this.compileDir)
  98. .should.equal(true);
  99. });
  100. it("should not call doCompile with the request", function() {
  101. return this.CompileManager.doCompile
  102. .called.should.equal(false);
  103. });
  104. return it("should call the callback with the error", function() {
  105. return this.callback.calledWithExactly(this.error)
  106. .should.equal(true);
  107. });
  108. });
  109. });
  110. describe("doCompile", function() {
  111. beforeEach(function() {
  112. this.output_files = [{
  113. path: "output.log",
  114. type: "log"
  115. }, {
  116. path: "output.pdf",
  117. type: "pdf"
  118. }];
  119. this.build_files = [{
  120. path: "output.log",
  121. type: "log",
  122. build: 1234
  123. }, {
  124. path: "output.pdf",
  125. type: "pdf",
  126. build: 1234
  127. }];
  128. this.request = {
  129. resources: (this.resources = "mock-resources"),
  130. rootResourcePath: (this.rootResourcePath = "main.tex"),
  131. project_id: this.project_id,
  132. user_id: this.user_id,
  133. compiler: (this.compiler = "pdflatex"),
  134. timeout: (this.timeout = 42000),
  135. imageName: (this.image = "example.com/image"),
  136. flags: (this.flags = ["-file-line-error"])
  137. };
  138. this.env = {};
  139. this.Settings.compileDir = "compiles";
  140. this.compileDir = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`;
  141. this.ResourceWriter.syncResourcesToDisk = sinon.stub().callsArgWith(2, null, this.resources);
  142. this.LatexRunner.runLatex = sinon.stub().callsArg(2);
  143. this.OutputFileFinder.findOutputFiles = sinon.stub().callsArgWith(2, null, this.output_files);
  144. this.OutputCacheManager.saveOutputFiles = sinon.stub().callsArgWith(2, null, this.build_files);
  145. this.DraftModeManager.injectDraftMode = sinon.stub().callsArg(1);
  146. return this.TikzManager.checkMainFile = sinon.stub().callsArg(3, false);
  147. });
  148. describe("normally", function() {
  149. beforeEach(function() {
  150. return this.CompileManager.doCompile(this.request, this.callback);
  151. });
  152. it("should write the resources to disk", function() {
  153. return this.ResourceWriter.syncResourcesToDisk
  154. .calledWith(this.request, this.compileDir)
  155. .should.equal(true);
  156. });
  157. it("should run LaTeX", function() {
  158. return this.LatexRunner.runLatex
  159. .calledWith(`${this.project_id}-${this.user_id}`, {
  160. directory: this.compileDir,
  161. mainFile: this.rootResourcePath,
  162. compiler: this.compiler,
  163. timeout: this.timeout,
  164. image: this.image,
  165. flags: this.flags,
  166. environment: this.env
  167. })
  168. .should.equal(true);
  169. });
  170. it("should find the output files", function() {
  171. return this.OutputFileFinder.findOutputFiles
  172. .calledWith(this.resources, this.compileDir)
  173. .should.equal(true);
  174. });
  175. it("should return the output files", function() {
  176. return this.callback.calledWith(null, this.build_files).should.equal(true);
  177. });
  178. return it("should not inject draft mode by default", function() {
  179. return this.DraftModeManager.injectDraftMode.called.should.equal(false);
  180. });
  181. });
  182. describe("with draft mode", function() {
  183. beforeEach(function() {
  184. this.request.draft = true;
  185. return this.CompileManager.doCompile(this.request, this.callback);
  186. });
  187. return it("should inject the draft mode header", function() {
  188. return this.DraftModeManager.injectDraftMode
  189. .calledWith(this.compileDir + "/" + this.rootResourcePath)
  190. .should.equal(true);
  191. });
  192. });
  193. describe("with a check option", function() {
  194. beforeEach(function() {
  195. this.request.check = "error";
  196. return this.CompileManager.doCompile(this.request, this.callback);
  197. });
  198. return it("should run chktex", function() {
  199. return this.LatexRunner.runLatex
  200. .calledWith(`${this.project_id}-${this.user_id}`, {
  201. directory: this.compileDir,
  202. mainFile: this.rootResourcePath,
  203. compiler: this.compiler,
  204. timeout: this.timeout,
  205. image: this.image,
  206. flags: this.flags,
  207. environment: {'CHKTEX_OPTIONS': '-nall -e9 -e10 -w15 -w16', 'CHKTEX_EXIT_ON_ERROR':1, 'CHKTEX_ULIMIT_OPTIONS': '-t 5 -v 64000'}
  208. })
  209. .should.equal(true);
  210. });
  211. });
  212. return describe("with a knitr file and check options", function() {
  213. beforeEach(function() {
  214. this.request.rootResourcePath = "main.Rtex";
  215. this.request.check = "error";
  216. return this.CompileManager.doCompile(this.request, this.callback);
  217. });
  218. return it("should not run chktex", function() {
  219. return this.LatexRunner.runLatex
  220. .calledWith(`${this.project_id}-${this.user_id}`, {
  221. directory: this.compileDir,
  222. mainFile: "main.Rtex",
  223. compiler: this.compiler,
  224. timeout: this.timeout,
  225. image: this.image,
  226. flags: this.flags,
  227. environment: this.env
  228. })
  229. .should.equal(true);
  230. });
  231. });
  232. });
  233. describe("clearProject", function() {
  234. describe("succesfully", function() {
  235. beforeEach(function() {
  236. this.Settings.compileDir = "compiles";
  237. this.fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory(){ return true; }});
  238. this.proc = new EventEmitter();
  239. this.proc.stdout = new EventEmitter();
  240. this.proc.stderr = new EventEmitter();
  241. this.child_process.spawn = sinon.stub().returns(this.proc);
  242. this.CompileManager.clearProject(this.project_id, this.user_id, this.callback);
  243. return this.proc.emit("close", 0);
  244. });
  245. it("should remove the project directory", function() {
  246. return this.child_process.spawn
  247. .calledWith("rm", ["-r", `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`])
  248. .should.equal(true);
  249. });
  250. return it("should call the callback", function() {
  251. return this.callback.called.should.equal(true);
  252. });
  253. });
  254. return describe("with a non-success status code", function() {
  255. beforeEach(function() {
  256. this.Settings.compileDir = "compiles";
  257. this.fs.lstat = sinon.stub().callsArgWith(1, null,{isDirectory(){ return true; }});
  258. this.proc = new EventEmitter();
  259. this.proc.stdout = new EventEmitter();
  260. this.proc.stderr = new EventEmitter();
  261. this.child_process.spawn = sinon.stub().returns(this.proc);
  262. this.CompileManager.clearProject(this.project_id, this.user_id, this.callback);
  263. this.proc.stderr.emit("data", (this.error = "oops"));
  264. return this.proc.emit("close", 1);
  265. });
  266. it("should remove the project directory", function() {
  267. return this.child_process.spawn
  268. .calledWith("rm", ["-r", `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`])
  269. .should.equal(true);
  270. });
  271. return it("should call the callback with an error from the stderr", function() {
  272. this.callback
  273. .calledWith(new Error())
  274. .should.equal(true);
  275. return this.callback.args[0][0].message.should.equal(`rm -r ${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id} failed: ${this.error}`);
  276. });
  277. });
  278. });
  279. describe("syncing", function() {
  280. beforeEach(function() {
  281. this.page = 1;
  282. this.h = 42.23;
  283. this.v = 87.56;
  284. this.width = 100.01;
  285. this.height = 234.56;
  286. this.line = 5;
  287. this.column = 3;
  288. this.file_name = "main.tex";
  289. this.child_process.execFile = sinon.stub();
  290. return this.Settings.path.synctexBaseDir = project_id => `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`;
  291. });
  292. describe("syncFromCode", function() {
  293. beforeEach(function() {
  294. this.fs.stat = sinon.stub().callsArgWith(1, null,{isFile(){ return true; }});
  295. this.stdout = `NODE\t${this.page}\t${this.h}\t${this.v}\t${this.width}\t${this.height}\n`;
  296. this.CommandRunner.run = sinon.stub().callsArgWith(6, null, {stdout:this.stdout});
  297. return this.CompileManager.syncFromCode(this.project_id, this.user_id, this.file_name, this.line, this.column, this.callback);
  298. });
  299. it("should execute the synctex binary", function() {
  300. const bin_path = Path.resolve(__dirname + "/../../../bin/synctex");
  301. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`;
  302. const file_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}`;
  303. return this.CommandRunner.run
  304. .calledWith(
  305. `${this.project_id}-${this.user_id}`,
  306. ['/opt/synctex', 'code', synctex_path, file_path, this.line, this.column],
  307. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  308. this.Settings.clsi.docker.image,
  309. 60000,
  310. {}
  311. ).should.equal(true);
  312. });
  313. return it("should call the callback with the parsed output", function() {
  314. return this.callback
  315. .calledWith(null, [{
  316. page: this.page,
  317. h: this.h,
  318. v: this.v,
  319. height: this.height,
  320. width: this.width
  321. }])
  322. .should.equal(true);
  323. });
  324. });
  325. return describe("syncFromPdf", function() {
  326. beforeEach(function() {
  327. this.fs.stat = sinon.stub().callsArgWith(1, null,{isFile(){ return true; }});
  328. this.stdout = `NODE\t${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/${this.file_name}\t${this.line}\t${this.column}\n`;
  329. this.CommandRunner.run = sinon.stub().callsArgWith(6, null, {stdout:this.stdout});
  330. return this.CompileManager.syncFromPdf(this.project_id, this.user_id, this.page, this.h, this.v, this.callback);
  331. });
  332. it("should execute the synctex binary", function() {
  333. const bin_path = Path.resolve(__dirname + "/../../../bin/synctex");
  334. const synctex_path = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}/output.pdf`;
  335. return this.CommandRunner.run
  336. .calledWith(
  337. `${this.project_id}-${this.user_id}`,
  338. ['/opt/synctex', "pdf", synctex_path, this.page, this.h, this.v],
  339. `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`,
  340. this.Settings.clsi.docker.image,
  341. 60000,
  342. {}).should.equal(true);
  343. });
  344. return it("should call the callback with the parsed output", function() {
  345. return this.callback
  346. .calledWith(null, [{
  347. file: this.file_name,
  348. line: this.line,
  349. column: this.column
  350. }])
  351. .should.equal(true);
  352. });
  353. });
  354. });
  355. return describe("wordcount", function() {
  356. beforeEach(function() {
  357. this.CommandRunner.run = sinon.stub().callsArg(6);
  358. this.fs.readFile = sinon.stub().callsArgWith(2, null, (this.stdout = "Encoding: ascii\nWords in text: 2"));
  359. this.callback = sinon.stub();
  360. this.project_id;
  361. this.timeout = 60 * 1000;
  362. this.file_name = "main.tex";
  363. this.Settings.path.compilesDir = "/local/compile/directory";
  364. this.image = "example.com/image";
  365. return this.CompileManager.wordcount(this.project_id, this.user_id, this.file_name, this.image, this.callback);
  366. });
  367. it("should run the texcount command", function() {
  368. this.directory = `${this.Settings.path.compilesDir}/${this.project_id}-${this.user_id}`;
  369. this.file_path = `$COMPILE_DIR/${this.file_name}`;
  370. this.command =[ "texcount", "-nocol", "-inc", this.file_path, `-out=${this.file_path}.wc`];
  371. return this.CommandRunner.run
  372. .calledWith(`${this.project_id}-${this.user_id}`, this.command, this.directory, this.image, this.timeout, {})
  373. .should.equal(true);
  374. });
  375. return it("should call the callback with the parsed output", function() {
  376. return this.callback
  377. .calledWith(null, {
  378. encode: "ascii",
  379. textWords: 2,
  380. headWords: 0,
  381. outside: 0,
  382. headers: 0,
  383. elements: 0,
  384. mathInline: 0,
  385. mathDisplay: 0,
  386. errors: 0,
  387. messages: ""
  388. })
  389. .should.equal(true);
  390. });
  391. });
  392. });