ExampleDocumentTests.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 fsExtra = require('fs-extra')
  23. const ChildProcess = require('child_process')
  24. const ClsiApp = require('./helpers/ClsiApp')
  25. const logger = require('logger-sharelatex')
  26. const Path = require('path')
  27. const fixturePath = (path) => {
  28. if (path.slice(0, 3) === 'tmp') {
  29. return '/tmp/clsi_acceptance_tests' + path.slice(3)
  30. }
  31. return Path.normalize(__dirname + '/../fixtures/' + path)
  32. }
  33. const process = require('process')
  34. console.log(
  35. process.pid,
  36. process.ppid,
  37. process.getuid(),
  38. process.getgroups(),
  39. 'PID'
  40. )
  41. const MOCHA_LATEX_TIMEOUT = 60 * 1000
  42. const convertToPng = function (pdfPath, pngPath, callback) {
  43. if (callback == null) {
  44. callback = function (error) {}
  45. }
  46. const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`
  47. console.log('COMMAND')
  48. console.log(command)
  49. const convert = ChildProcess.exec(command)
  50. const stdout = ''
  51. convert.stdout.on('data', (chunk) => console.log('STDOUT', chunk.toString()))
  52. convert.stderr.on('data', (chunk) => console.log('STDERR', chunk.toString()))
  53. return convert.on('exit', () => callback())
  54. }
  55. const compare = function (originalPath, generatedPath, callback) {
  56. if (callback == null) {
  57. callback = function (error, same) {}
  58. }
  59. const diff_file = `${fixturePath(generatedPath)}-diff.png`
  60. const proc = ChildProcess.exec(
  61. `compare -metric mae ${fixturePath(originalPath)} ${fixturePath(
  62. generatedPath
  63. )} ${diff_file}`
  64. )
  65. let stderr = ''
  66. proc.stderr.on('data', (chunk) => (stderr += chunk))
  67. return proc.on('exit', () => {
  68. if (stderr.trim() === '0 (0)') {
  69. // remove output diff if test matches expected image
  70. fs.unlink(diff_file, (err) => {
  71. if (err) {
  72. throw err
  73. }
  74. })
  75. return callback(null, true)
  76. } else {
  77. console.log('compare result', stderr)
  78. return callback(null, false)
  79. }
  80. })
  81. }
  82. const checkPdfInfo = function (pdfPath, callback) {
  83. if (callback == null) {
  84. callback = function (error, output) {}
  85. }
  86. const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`)
  87. let stdout = ''
  88. proc.stdout.on('data', (chunk) => (stdout += chunk))
  89. proc.stderr.on('data', (chunk) => console.log('STDERR', chunk.toString()))
  90. return proc.on('exit', () => {
  91. if (stdout.match(/Optimized:\s+yes/)) {
  92. return callback(null, true)
  93. } else {
  94. return callback(null, false)
  95. }
  96. })
  97. }
  98. const compareMultiplePages = function (project_id, callback) {
  99. if (callback == null) {
  100. callback = function (error) {}
  101. }
  102. var compareNext = function (page_no, callback) {
  103. const path = `tmp/${project_id}-source-${page_no}.png`
  104. return fs.stat(fixturePath(path), (error, stat) => {
  105. if (error != null) {
  106. return callback()
  107. } else {
  108. return compare(
  109. `tmp/${project_id}-source-${page_no}.png`,
  110. `tmp/${project_id}-generated-${page_no}.png`,
  111. (error, same) => {
  112. if (error != null) {
  113. throw error
  114. }
  115. same.should.equal(true)
  116. return compareNext(page_no + 1, callback)
  117. }
  118. )
  119. }
  120. })
  121. }
  122. return compareNext(0, callback)
  123. }
  124. const comparePdf = function (project_id, example_dir, callback) {
  125. if (callback == null) {
  126. callback = function (error) {}
  127. }
  128. console.log('CONVERT')
  129. console.log(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`)
  130. return convertToPng(
  131. `tmp/${project_id}.pdf`,
  132. `tmp/${project_id}-generated.png`,
  133. (error) => {
  134. if (error != null) {
  135. throw error
  136. }
  137. return convertToPng(
  138. `examples/${example_dir}/output.pdf`,
  139. `tmp/${project_id}-source.png`,
  140. (error) => {
  141. if (error != null) {
  142. throw error
  143. }
  144. return fs.stat(
  145. fixturePath(`tmp/${project_id}-source-0.png`),
  146. (error, stat) => {
  147. if (error != null) {
  148. return compare(
  149. `tmp/${project_id}-source.png`,
  150. `tmp/${project_id}-generated.png`,
  151. (error, same) => {
  152. if (error != null) {
  153. throw error
  154. }
  155. same.should.equal(true)
  156. return callback()
  157. }
  158. )
  159. } else {
  160. return compareMultiplePages(project_id, (error) => {
  161. if (error != null) {
  162. throw error
  163. }
  164. return callback()
  165. })
  166. }
  167. }
  168. )
  169. }
  170. )
  171. }
  172. )
  173. }
  174. const downloadAndComparePdf = function (
  175. project_id,
  176. example_dir,
  177. url,
  178. callback
  179. ) {
  180. if (callback == null) {
  181. callback = function (error) {}
  182. }
  183. const writeStream = fs.createWriteStream(fixturePath(`tmp/${project_id}.pdf`))
  184. request.get(url).pipe(writeStream)
  185. console.log('writing file out', fixturePath(`tmp/${project_id}.pdf`))
  186. return writeStream.on('close', () => {
  187. return checkPdfInfo(`tmp/${project_id}.pdf`, (error, optimised) => {
  188. if (error != null) {
  189. throw error
  190. }
  191. optimised.should.equal(true)
  192. return comparePdf(project_id, example_dir, callback)
  193. })
  194. })
  195. }
  196. Client.runServer(4242, fixturePath('examples'))
  197. describe('Example Documents', function () {
  198. before(function (done) {
  199. ClsiApp.ensureRunning(done)
  200. })
  201. before(function (done) {
  202. fsExtra.remove(fixturePath('tmp'), done)
  203. })
  204. before(function (done) {
  205. fs.mkdir(fixturePath('tmp'), done)
  206. })
  207. after(function (done) {
  208. fsExtra.remove(fixturePath('tmp'), done)
  209. })
  210. return Array.from(fs.readdirSync(fixturePath('examples'))).map(
  211. (example_dir) =>
  212. ((example_dir) =>
  213. describe(example_dir, function () {
  214. before(function () {
  215. return (this.project_id = Client.randomId() + '_' + example_dir)
  216. })
  217. it('should generate the correct pdf', function (done) {
  218. this.timeout(MOCHA_LATEX_TIMEOUT)
  219. return Client.compileDirectory(
  220. this.project_id,
  221. fixturePath('examples'),
  222. example_dir,
  223. 4242,
  224. (error, res, body) => {
  225. if (
  226. error ||
  227. __guard__(
  228. body != null ? body.compile : undefined,
  229. (x) => x.status
  230. ) === 'failure'
  231. ) {
  232. console.log(
  233. 'DEBUG: error',
  234. error,
  235. 'body',
  236. JSON.stringify(body)
  237. )
  238. return done(new Error('Compile failed'))
  239. }
  240. const pdf = Client.getOutputFile(body, 'pdf')
  241. return downloadAndComparePdf(
  242. this.project_id,
  243. example_dir,
  244. pdf.url,
  245. done
  246. )
  247. }
  248. )
  249. })
  250. return it('should generate the correct pdf on the second run as well', function (done) {
  251. this.timeout(MOCHA_LATEX_TIMEOUT)
  252. return Client.compileDirectory(
  253. this.project_id,
  254. fixturePath('examples'),
  255. example_dir,
  256. 4242,
  257. (error, res, body) => {
  258. if (
  259. error ||
  260. __guard__(
  261. body != null ? body.compile : undefined,
  262. (x) => x.status
  263. ) === 'failure'
  264. ) {
  265. console.log(
  266. 'DEBUG: error',
  267. error,
  268. 'body',
  269. JSON.stringify(body)
  270. )
  271. return done(new Error('Compile failed'))
  272. }
  273. const pdf = Client.getOutputFile(body, 'pdf')
  274. return downloadAndComparePdf(
  275. this.project_id,
  276. example_dir,
  277. pdf.url,
  278. done
  279. )
  280. }
  281. )
  282. })
  283. }))(example_dir)
  284. )
  285. })
  286. function __guard__(value, transform) {
  287. return typeof value !== 'undefined' && value !== null
  288. ? transform(value)
  289. : undefined
  290. }