ExampleDocumentTests.js 8.4 KB

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