ExampleDocumentTests.js 8.3 KB

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