ExampleDocumentTests.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* eslint-disable
  2. no-return-assign,
  3. no-unused-vars,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS103: Rewrite code to no longer use __guard__
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. const Client = require('./helpers/Client')
  16. const request = require('request')
  17. const fs = require('fs')
  18. const fsExtra = require('fs-extra')
  19. const ChildProcess = require('child_process')
  20. const ClsiApp = require('./helpers/ClsiApp')
  21. const logger = require('@overleaf/logger')
  22. const Path = require('path')
  23. const fixturePath = path => {
  24. if (path.slice(0, 3) === 'tmp') {
  25. return '/tmp/clsi_acceptance_tests' + path.slice(3)
  26. }
  27. return Path.join(__dirname, '../fixtures/', path)
  28. }
  29. const process = require('process')
  30. console.log(
  31. process.pid,
  32. process.ppid,
  33. process.getuid(),
  34. process.getgroups(),
  35. 'PID'
  36. )
  37. const MOCHA_LATEX_TIMEOUT = 60 * 1000
  38. const convertToPng = function (pdfPath, pngPath, callback) {
  39. if (callback == null) {
  40. callback = function () {}
  41. }
  42. const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`
  43. console.log('COMMAND')
  44. console.log(command)
  45. const convert = ChildProcess.exec(command)
  46. const stdout = ''
  47. convert.stdout.on('data', chunk => console.log('STDOUT', chunk.toString()))
  48. convert.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
  49. return convert.on('exit', () => callback())
  50. }
  51. const compare = function (originalPath, generatedPath, callback) {
  52. if (callback == null) {
  53. callback = function () {}
  54. }
  55. const diffFile = `${fixturePath(generatedPath)}-diff.png`
  56. const proc = ChildProcess.exec(
  57. `compare -metric mae ${fixturePath(originalPath)} ${fixturePath(
  58. generatedPath
  59. )} ${diffFile}`
  60. )
  61. let stderr = ''
  62. proc.stderr.on('data', chunk => (stderr += chunk))
  63. return proc.on('exit', () => {
  64. if (stderr.trim() === '0 (0)') {
  65. // remove output diff if test matches expected image
  66. fs.unlink(diffFile, err => {
  67. if (err) {
  68. throw err
  69. }
  70. })
  71. return callback(null, true)
  72. } else {
  73. console.log('compare result', stderr)
  74. return callback(null, false)
  75. }
  76. })
  77. }
  78. const checkPdfInfo = function (pdfPath, callback) {
  79. if (callback == null) {
  80. callback = function () {}
  81. }
  82. const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`)
  83. let stdout = ''
  84. proc.stdout.on('data', chunk => (stdout += chunk))
  85. proc.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
  86. return proc.on('exit', () => {
  87. if (stdout.match(/Optimized:\s+yes/)) {
  88. return callback(null, true)
  89. } else {
  90. return callback(null, false)
  91. }
  92. })
  93. }
  94. const compareMultiplePages = function (projectId, callback) {
  95. if (callback == null) {
  96. callback = function () {}
  97. }
  98. function compareNext(pageNo, callback) {
  99. const path = `tmp/${projectId}-source-${pageNo}.png`
  100. return fs.stat(fixturePath(path), (error, stat) => {
  101. if (error != null) {
  102. return callback()
  103. } else {
  104. return compare(
  105. `tmp/${projectId}-source-${pageNo}.png`,
  106. `tmp/${projectId}-generated-${pageNo}.png`,
  107. (error, same) => {
  108. if (error != null) {
  109. throw error
  110. }
  111. same.should.equal(true)
  112. return compareNext(pageNo + 1, callback)
  113. }
  114. )
  115. }
  116. })
  117. }
  118. return compareNext(0, callback)
  119. }
  120. const comparePdf = function (projectId, exampleDir, callback) {
  121. if (callback == null) {
  122. callback = function () {}
  123. }
  124. console.log('CONVERT')
  125. console.log(`tmp/${projectId}.pdf`, `tmp/${projectId}-generated.png`)
  126. return convertToPng(
  127. `tmp/${projectId}.pdf`,
  128. `tmp/${projectId}-generated.png`,
  129. error => {
  130. if (error != null) {
  131. throw error
  132. }
  133. return convertToPng(
  134. `examples/${exampleDir}/output.pdf`,
  135. `tmp/${projectId}-source.png`,
  136. error => {
  137. if (error != null) {
  138. throw error
  139. }
  140. return fs.stat(
  141. fixturePath(`tmp/${projectId}-source-0.png`),
  142. (error, stat) => {
  143. if (error != null) {
  144. return compare(
  145. `tmp/${projectId}-source.png`,
  146. `tmp/${projectId}-generated.png`,
  147. (error, same) => {
  148. if (error != null) {
  149. throw error
  150. }
  151. same.should.equal(true)
  152. return callback()
  153. }
  154. )
  155. } else {
  156. return compareMultiplePages(projectId, error => {
  157. if (error != null) {
  158. throw error
  159. }
  160. return callback()
  161. })
  162. }
  163. }
  164. )
  165. }
  166. )
  167. }
  168. )
  169. }
  170. const downloadAndComparePdf = function (projectId, exampleDir, url, callback) {
  171. if (callback == null) {
  172. callback = function () {}
  173. }
  174. const writeStream = fs.createWriteStream(fixturePath(`tmp/${projectId}.pdf`))
  175. request.get(url).pipe(writeStream)
  176. console.log('writing file out', fixturePath(`tmp/${projectId}.pdf`))
  177. return writeStream.on('close', () => {
  178. return checkPdfInfo(`tmp/${projectId}.pdf`, (error, optimised) => {
  179. if (error != null) {
  180. throw error
  181. }
  182. optimised.should.equal(true)
  183. return comparePdf(projectId, exampleDir, callback)
  184. })
  185. })
  186. }
  187. Client.runServer(4242, fixturePath('examples'))
  188. describe('Example Documents', function () {
  189. before(function (done) {
  190. ClsiApp.ensureRunning(done)
  191. })
  192. before(function (done) {
  193. fsExtra.remove(fixturePath('tmp'), done)
  194. })
  195. before(function (done) {
  196. fs.mkdir(fixturePath('tmp'), done)
  197. })
  198. after(function (done) {
  199. fsExtra.remove(fixturePath('tmp'), done)
  200. })
  201. return Array.from(fs.readdirSync(fixturePath('examples'))).map(exampleDir =>
  202. (exampleDir =>
  203. describe(exampleDir, function () {
  204. before(function () {
  205. return (this.project_id = Client.randomId() + '_' + exampleDir)
  206. })
  207. it('should generate the correct pdf', function (done) {
  208. this.timeout(MOCHA_LATEX_TIMEOUT)
  209. return Client.compileDirectory(
  210. this.project_id,
  211. fixturePath('examples'),
  212. exampleDir,
  213. 4242,
  214. (error, res, body) => {
  215. if (
  216. error ||
  217. __guard__(
  218. body != null ? body.compile : undefined,
  219. x => x.status
  220. ) === 'failure'
  221. ) {
  222. console.log('DEBUG: error', error, 'body', JSON.stringify(body))
  223. return done(new Error('Compile failed'))
  224. }
  225. const pdf = Client.getOutputFile(body, 'pdf')
  226. return downloadAndComparePdf(
  227. this.project_id,
  228. exampleDir,
  229. pdf.url,
  230. done
  231. )
  232. }
  233. )
  234. })
  235. return it('should generate the correct pdf on the second run as well', function (done) {
  236. this.timeout(MOCHA_LATEX_TIMEOUT)
  237. return Client.compileDirectory(
  238. this.project_id,
  239. fixturePath('examples'),
  240. exampleDir,
  241. 4242,
  242. (error, res, body) => {
  243. if (
  244. error ||
  245. __guard__(
  246. body != null ? body.compile : undefined,
  247. x => x.status
  248. ) === 'failure'
  249. ) {
  250. console.log('DEBUG: error', error, 'body', JSON.stringify(body))
  251. return done(new Error('Compile failed'))
  252. }
  253. const pdf = Client.getOutputFile(body, 'pdf')
  254. return downloadAndComparePdf(
  255. this.project_id,
  256. exampleDir,
  257. pdf.url,
  258. done
  259. )
  260. }
  261. )
  262. })
  263. }))(exampleDir)
  264. )
  265. })
  266. function __guard__(value, transform) {
  267. return typeof value !== 'undefined' && value !== null
  268. ? transform(value)
  269. : undefined
  270. }