ExampleDocumentTests.coffee 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Client = require "./helpers/Client"
  2. request = require "request"
  3. require("chai").should()
  4. fs = require "fs"
  5. ChildProcess = require "child_process"
  6. fixturePath = (path) -> __dirname + "/../fixtures/" + path
  7. try
  8. fs.mkdirSync(fixturePath("tmp"))
  9. catch e
  10. convertToPng = (pdfPath, pngPath, callback = (error) ->) ->
  11. convert = ChildProcess.exec "convert #{fixturePath(pdfPath)} #{fixturePath(pngPath)}"
  12. convert.on "exit", () ->
  13. callback()
  14. compare = (originalPath, generatedPath, callback = (error, same) ->) ->
  15. proc = ChildProcess.exec "compare -metric mae #{fixturePath(originalPath)} #{fixturePath(generatedPath)} #{fixturePath("tmp/diff.png")}"
  16. stderr = ""
  17. proc.stderr.on "data", (chunk) -> stderr += chunk
  18. proc.on "exit", () ->
  19. if stderr.trim() == "0 (0)"
  20. callback null, true
  21. else
  22. console.log stderr
  23. callback null, false
  24. compareMultiplePages = (project_id, callback = (error) ->) ->
  25. compareNext = (page_no, callback) ->
  26. path = "tmp/#{project_id}-source-#{page_no}.png"
  27. fs.stat fixturePath(path), (error, stat) ->
  28. if error?
  29. callback()
  30. else
  31. compare "tmp/#{project_id}-source-#{page_no}.png", "tmp/#{project_id}-generated-#{page_no}.png", (error, same) =>
  32. throw error if error?
  33. same.should.equal true
  34. compareNext page_no + 1, callback
  35. compareNext 0, callback
  36. downloadAndComparePdf = (project_id, example_dir, url, callback = (error) ->) ->
  37. writeStream = fs.createWriteStream(fixturePath("tmp/#{project_id}.pdf"))
  38. request.get(url).pipe(writeStream)
  39. writeStream.on "close", () =>
  40. convertToPng "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png", (error) =>
  41. throw error if error?
  42. convertToPng "examples/#{example_dir}/output.pdf", "tmp/#{project_id}-source.png", (error) =>
  43. throw error if error?
  44. fs.stat fixturePath("tmp/#{project_id}-source-0.png"), (error, stat) =>
  45. if error?
  46. compare "tmp/#{project_id}-source.png", "tmp/#{project_id}-generated.png", (error, same) =>
  47. throw error if error?
  48. same.should.equal true
  49. callback()
  50. else
  51. compareMultiplePages project_id, (error) ->
  52. throw error if error?
  53. callback()
  54. Client.runServer(4242, fixturePath("examples"))
  55. describe "Example Documents", ->
  56. before (done) ->
  57. ChildProcess.exec("rm test/acceptance/fixtures/tmp/*").on "exit", () -> done()
  58. for example_dir in fs.readdirSync fixturePath("examples")
  59. do (example_dir) ->
  60. describe example_dir, ->
  61. before ->
  62. @project_id = Client.randomId()
  63. it "should generate the correct pdf", (done) ->
  64. Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) =>
  65. pdf = Client.getOutputFile body, "pdf"
  66. downloadAndComparePdf(@project_id, example_dir, pdf.url, done)
  67. it "should generate the correct pdf on the second run as well", (done) ->
  68. Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) =>
  69. pdf = Client.getOutputFile body, "pdf"
  70. downloadAndComparePdf(@project_id, example_dir, pdf.url, done)