ExampleDocumentTests.js 8.2 KB

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