ExampleDocumentTests.coffee 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. Client = require "./helpers/Client"
  2. request = require "request"
  3. require("chai").should()
  4. fs = require "fs"
  5. ChildProcess = require "child_process"
  6. ClsiApp = require "./helpers/ClsiApp"
  7. logger = require("logger-sharelatex")
  8. Path = require("path")
  9. fixturePath = (path) -> Path.normalize(__dirname + "/../fixtures/" + path)
  10. process = require "process"
  11. console.log process.pid, process.ppid, process.getuid(),process.getgroups(), "PID"
  12. try
  13. console.log "creating tmp directory", fixturePath("tmp")
  14. fs.mkdirSync(fixturePath("tmp"))
  15. catch err
  16. console.log err, fixturePath("tmp"), "unable to create fixture tmp path"
  17. MOCHA_LATEX_TIMEOUT = 60 * 1000
  18. convertToPng = (pdfPath, pngPath, callback = (error) ->) ->
  19. command = "convert #{fixturePath(pdfPath)} #{fixturePath(pngPath)}"
  20. console.log "COMMAND"
  21. console.log command
  22. convert = ChildProcess.exec command
  23. stdout = ""
  24. convert.stdout.on "data", (chunk) -> console.log "STDOUT", chunk.toString()
  25. convert.stderr.on "data", (chunk) -> console.log "STDERR", chunk.toString()
  26. convert.on "exit", () ->
  27. callback()
  28. compare = (originalPath, generatedPath, callback = (error, same) ->) ->
  29. diff_file = "#{fixturePath(generatedPath)}-diff.png"
  30. proc = ChildProcess.exec "compare -metric mae #{fixturePath(originalPath)} #{fixturePath(generatedPath)} #{diff_file}"
  31. stderr = ""
  32. proc.stderr.on "data", (chunk) -> stderr += chunk
  33. proc.on "exit", () ->
  34. if stderr.trim() == "0 (0)"
  35. # remove output diff if test matches expected image
  36. fs.unlink diff_file, (err) ->
  37. if err
  38. throw err
  39. callback null, true
  40. else
  41. console.log "compare result", stderr
  42. callback null, false
  43. checkPdfInfo = (pdfPath, callback = (error, output) ->) ->
  44. proc = ChildProcess.exec "pdfinfo #{fixturePath(pdfPath)}"
  45. stdout = ""
  46. proc.stdout.on "data", (chunk) -> stdout += chunk
  47. proc.stderr.on "data", (chunk) -> console.log "STDERR", chunk.toString()
  48. proc.on "exit", () ->
  49. if stdout.match(/Optimized:\s+yes/)
  50. callback null, true
  51. else
  52. callback null, false
  53. compareMultiplePages = (project_id, callback = (error) ->) ->
  54. compareNext = (page_no, callback) ->
  55. path = "tmp/#{project_id}-source-#{page_no}.png"
  56. fs.stat fixturePath(path), (error, stat) ->
  57. if error?
  58. callback()
  59. else
  60. compare "tmp/#{project_id}-source-#{page_no}.png", "tmp/#{project_id}-generated-#{page_no}.png", (error, same) =>
  61. throw error if error?
  62. same.should.equal true
  63. compareNext page_no + 1, callback
  64. compareNext 0, callback
  65. comparePdf = (project_id, example_dir, callback = (error) ->) ->
  66. console.log "CONVERT"
  67. console.log "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png"
  68. convertToPng "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png", (error) =>
  69. throw error if error?
  70. convertToPng "examples/#{example_dir}/output.pdf", "tmp/#{project_id}-source.png", (error) =>
  71. throw error if error?
  72. fs.stat fixturePath("tmp/#{project_id}-source-0.png"), (error, stat) =>
  73. if error?
  74. compare "tmp/#{project_id}-source.png", "tmp/#{project_id}-generated.png", (error, same) =>
  75. throw error if error?
  76. same.should.equal true
  77. callback()
  78. else
  79. compareMultiplePages project_id, (error) ->
  80. throw error if error?
  81. callback()
  82. downloadAndComparePdf = (project_id, example_dir, url, callback = (error) ->) ->
  83. writeStream = fs.createWriteStream(fixturePath("tmp/#{project_id}.pdf"))
  84. request.get(url).pipe(writeStream)
  85. console.log("writing file out", fixturePath("tmp/#{project_id}.pdf"))
  86. writeStream.on "close", () =>
  87. checkPdfInfo "tmp/#{project_id}.pdf", (error, optimised) =>
  88. throw error if error?
  89. optimised.should.equal true
  90. comparePdf project_id, example_dir, callback
  91. Client.runServer(4242, fixturePath("examples"))
  92. describe "Example Documents", ->
  93. before (done) ->
  94. ChildProcess.exec("rm test/acceptance/fixtures/tmp/*").on "exit", () ->
  95. ClsiApp.ensureRunning done
  96. for example_dir in fs.readdirSync fixturePath("examples")
  97. do (example_dir) ->
  98. describe example_dir, ->
  99. before ->
  100. @project_id = Client.randomId() + "_" + example_dir
  101. it "should generate the correct pdf", (done) ->
  102. this.timeout(MOCHA_LATEX_TIMEOUT)
  103. Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) =>
  104. if error || body?.compile?.status is "failure"
  105. console.log "DEBUG: error", error, "body", JSON.stringify(body)
  106. pdf = Client.getOutputFile body, "pdf"
  107. downloadAndComparePdf(@project_id, example_dir, pdf.url, done)
  108. it "should generate the correct pdf on the second run as well", (done) ->
  109. this.timeout(MOCHA_LATEX_TIMEOUT)
  110. Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) =>
  111. if error || body?.compile?.status is "failure"
  112. console.log "DEBUG: error", error, "body", JSON.stringify(body)
  113. pdf = Client.getOutputFile body, "pdf"
  114. downloadAndComparePdf(@project_id, example_dir, pdf.url, done)