CompileManagerTests.js 14 KB

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