ExampleDocumentTests.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  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. * DS103: Rewrite code to no longer use __guard__
  15. * DS207: Consider shorter variations of null checks
  16. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  17. */
  18. const Client = require("./helpers/Client");
  19. const request = require("request");
  20. require("chai").should();
  21. const fs = require("fs");
  22. const ChildProcess = require("child_process");
  23. const ClsiApp = require("./helpers/ClsiApp");
  24. const logger = require("logger-sharelatex");
  25. const Path = require("path");
  26. const fixturePath = path => Path.normalize(__dirname + "/../fixtures/" + path);
  27. const process = require("process");
  28. console.log(process.pid, process.ppid, process.getuid(),process.getgroups(), "PID");
  29. try {
  30. console.log("creating tmp directory", fixturePath("tmp"));
  31. fs.mkdirSync(fixturePath("tmp"));
  32. } catch (error) {
  33. const err = error;
  34. console.log(err, fixturePath("tmp"), "unable to create fixture tmp path");
  35. }
  36. const MOCHA_LATEX_TIMEOUT = 60 * 1000;
  37. const convertToPng = function(pdfPath, pngPath, callback) {
  38. if (callback == null) { callback = function(error) {}; }
  39. const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`;
  40. console.log("COMMAND");
  41. console.log(command);
  42. const convert = ChildProcess.exec(command);
  43. const stdout = "";
  44. convert.stdout.on("data", chunk => console.log("STDOUT", chunk.toString()));
  45. convert.stderr.on("data", chunk => console.log("STDERR", chunk.toString()));
  46. return convert.on("exit", () => callback());
  47. };
  48. const compare = function(originalPath, generatedPath, callback) {
  49. if (callback == null) { callback = function(error, same) {}; }
  50. const diff_file = `${fixturePath(generatedPath)}-diff.png`;
  51. const proc = ChildProcess.exec(`compare -metric mae ${fixturePath(originalPath)} ${fixturePath(generatedPath)} ${diff_file}`);
  52. let stderr = "";
  53. proc.stderr.on("data", chunk => stderr += chunk);
  54. return proc.on("exit", () => {
  55. if (stderr.trim() === "0 (0)") {
  56. // remove output diff if test matches expected image
  57. fs.unlink(diff_file, (err) => {
  58. if (err) {
  59. throw err;
  60. }
  61. });
  62. return callback(null, true);
  63. } else {
  64. console.log("compare result", stderr);
  65. return callback(null, false);
  66. }
  67. });
  68. };
  69. const checkPdfInfo = function(pdfPath, callback) {
  70. if (callback == null) { callback = function(error, output) {}; }
  71. const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`);
  72. let stdout = "";
  73. proc.stdout.on("data", chunk => stdout += chunk);
  74. proc.stderr.on("data", chunk => console.log("STDERR", chunk.toString()));
  75. return proc.on("exit", () => {
  76. if (stdout.match(/Optimized:\s+yes/)) {
  77. return callback(null, true);
  78. } else {
  79. return callback(null, false);
  80. }
  81. });
  82. };
  83. const compareMultiplePages = function(project_id, callback) {
  84. if (callback == null) { callback = function(error) {}; }
  85. var compareNext = function(page_no, callback) {
  86. const path = `tmp/${project_id}-source-${page_no}.png`;
  87. return fs.stat(fixturePath(path), (error, stat) => {
  88. if (error != null) {
  89. return callback();
  90. } else {
  91. return compare(`tmp/${project_id}-source-${page_no}.png`, `tmp/${project_id}-generated-${page_no}.png`, (error, same) => {
  92. if (error != null) { throw error; }
  93. same.should.equal(true);
  94. return compareNext(page_no + 1, callback);
  95. });
  96. }
  97. });
  98. };
  99. return compareNext(0, callback);
  100. };
  101. const comparePdf = function(project_id, example_dir, callback) {
  102. if (callback == null) { callback = function(error) {}; }
  103. console.log("CONVERT");
  104. console.log(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`);
  105. return convertToPng(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`, error => {
  106. if (error != null) { throw error; }
  107. return convertToPng(`examples/${example_dir}/output.pdf`, `tmp/${project_id}-source.png`, error => {
  108. if (error != null) { throw error; }
  109. return fs.stat(fixturePath(`tmp/${project_id}-source-0.png`), (error, stat) => {
  110. if (error != null) {
  111. return compare(`tmp/${project_id}-source.png`, `tmp/${project_id}-generated.png`, (error, same) => {
  112. if (error != null) { throw error; }
  113. same.should.equal(true);
  114. return callback();
  115. });
  116. } else {
  117. return compareMultiplePages(project_id, (error) => {
  118. if (error != null) { throw error; }
  119. return callback();
  120. });
  121. }
  122. });
  123. });
  124. });
  125. };
  126. const downloadAndComparePdf = function(project_id, example_dir, url, callback) {
  127. if (callback == null) { callback = function(error) {}; }
  128. const writeStream = fs.createWriteStream(fixturePath(`tmp/${project_id}.pdf`));
  129. request.get(url).pipe(writeStream);
  130. console.log("writing file out", fixturePath(`tmp/${project_id}.pdf`));
  131. return writeStream.on("close", () => {
  132. return checkPdfInfo(`tmp/${project_id}.pdf`, (error, optimised) => {
  133. if (error != null) { throw error; }
  134. optimised.should.equal(true);
  135. return comparePdf(project_id, example_dir, callback);
  136. });
  137. });
  138. };
  139. Client.runServer(4242, fixturePath("examples"));
  140. describe("Example Documents", function() {
  141. before(function(done) { return ChildProcess.exec("rm test/acceptance/fixtures/tmp/*").on("exit", () => ClsiApp.ensureRunning(done)); }
  142. );
  143. return Array.from(fs.readdirSync(fixturePath("examples"))).map((example_dir) =>
  144. (example_dir =>
  145. describe(example_dir, function() {
  146. before(function() {
  147. return this.project_id = Client.randomId() + "_" + example_dir;
  148. });
  149. it("should generate the correct pdf", function(done) {
  150. this.timeout(MOCHA_LATEX_TIMEOUT);
  151. return Client.compileDirectory(this.project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) => {
  152. if (error || (__guard__(body != null ? body.compile : undefined, x => x.status) === "failure")) {
  153. console.log("DEBUG: error", error, "body", JSON.stringify(body));
  154. }
  155. const pdf = Client.getOutputFile(body, "pdf");
  156. return downloadAndComparePdf(this.project_id, example_dir, pdf.url, done);
  157. });
  158. });
  159. return it("should generate the correct pdf on the second run as well", function(done) {
  160. this.timeout(MOCHA_LATEX_TIMEOUT);
  161. return Client.compileDirectory(this.project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) => {
  162. if (error || (__guard__(body != null ? body.compile : undefined, x => x.status) === "failure")) {
  163. console.log("DEBUG: error", error, "body", JSON.stringify(body));
  164. }
  165. const pdf = Client.getOutputFile(body, "pdf");
  166. return downloadAndComparePdf(this.project_id, example_dir, pdf.url, done);
  167. });
  168. });
  169. })
  170. )(example_dir));
  171. });
  172. function __guard__(value, transform) {
  173. return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
  174. }