ExampleDocumentTests.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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(project_id, example_dir, url, callback) {
  175. if (callback == null) {
  176. callback = function(error) {}
  177. }
  178. const writeStream = fs.createWriteStream(fixturePath(`tmp/${project_id}.pdf`))
  179. request.get(url).pipe(writeStream)
  180. console.log('writing file out', fixturePath(`tmp/${project_id}.pdf`))
  181. return writeStream.on('close', () => {
  182. return checkPdfInfo(`tmp/${project_id}.pdf`, (error, optimised) => {
  183. if (error != null) {
  184. throw error
  185. }
  186. optimised.should.equal(true)
  187. return comparePdf(project_id, example_dir, callback)
  188. })
  189. })
  190. }
  191. Client.runServer(4242, fixturePath('examples'))
  192. describe('Example Documents', function() {
  193. before(function(done) {
  194. ClsiApp.ensureRunning(done)
  195. })
  196. before(function(done) {
  197. fsExtra.remove(fixturePath('tmp'), done)
  198. })
  199. before(function(done) {
  200. fs.mkdir(fixturePath('tmp'), done)
  201. })
  202. after(function(done) {
  203. fsExtra.remove(fixturePath('tmp'), done)
  204. })
  205. return Array.from(fs.readdirSync(fixturePath('examples'))).map(example_dir =>
  206. (example_dir =>
  207. describe(example_dir, function() {
  208. before(function() {
  209. return (this.project_id = Client.randomId() + '_' + example_dir)
  210. })
  211. it('should generate the correct pdf', function(done) {
  212. this.timeout(MOCHA_LATEX_TIMEOUT)
  213. return Client.compileDirectory(
  214. this.project_id,
  215. fixturePath('examples'),
  216. example_dir,
  217. 4242,
  218. (error, res, body) => {
  219. if (
  220. error ||
  221. __guard__(
  222. body != null ? body.compile : undefined,
  223. x => x.status
  224. ) === 'failure'
  225. ) {
  226. console.log('DEBUG: error', error, 'body', JSON.stringify(body))
  227. return done(new Error("Compile failed"))
  228. }
  229. const pdf = Client.getOutputFile(body, 'pdf')
  230. return downloadAndComparePdf(
  231. this.project_id,
  232. example_dir,
  233. pdf.url,
  234. done
  235. )
  236. }
  237. )
  238. })
  239. return it('should generate the correct pdf on the second run as well', function(done) {
  240. this.timeout(MOCHA_LATEX_TIMEOUT)
  241. return Client.compileDirectory(
  242. this.project_id,
  243. fixturePath('examples'),
  244. example_dir,
  245. 4242,
  246. (error, res, body) => {
  247. if (
  248. error ||
  249. __guard__(
  250. body != null ? body.compile : undefined,
  251. x => x.status
  252. ) === 'failure'
  253. ) {
  254. console.log('DEBUG: error', error, 'body', JSON.stringify(body))
  255. return done(new Error("Compile failed"))
  256. }
  257. const pdf = Client.getOutputFile(body, 'pdf')
  258. return downloadAndComparePdf(
  259. this.project_id,
  260. example_dir,
  261. pdf.url,
  262. done
  263. )
  264. }
  265. )
  266. })
  267. }))(example_dir)
  268. )
  269. })
  270. function __guard__(value, transform) {
  271. return typeof value !== 'undefined' && value !== null
  272. ? transform(value)
  273. : undefined
  274. }