2014-02-12 12:27:43 -05:00
|
|
|
Client = require "./helpers/Client"
|
|
|
|
request = require "request"
|
|
|
|
require("chai").should()
|
|
|
|
fs = require "fs"
|
|
|
|
ChildProcess = require "child_process"
|
|
|
|
|
|
|
|
fixturePath = (path) -> __dirname + "/../fixtures/" + path
|
|
|
|
|
2014-04-02 16:55:14 -04:00
|
|
|
try
|
|
|
|
fs.mkdirSync(fixturePath("tmp"))
|
|
|
|
catch e
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
convertToPng = (pdfPath, pngPath, callback = (error) ->) ->
|
|
|
|
convert = ChildProcess.exec "convert #{fixturePath(pdfPath)} #{fixturePath(pngPath)}"
|
2017-09-28 06:50:33 -04:00
|
|
|
stdout = ""
|
|
|
|
convert.stdout.on "data", (chunk) -> console.log "STDOUT", chunk.toString()
|
|
|
|
convert.stderr.on "data", (chunk) -> console.log "STDERR", chunk.toString()
|
2014-02-12 12:27:43 -05:00
|
|
|
convert.on "exit", () ->
|
|
|
|
callback()
|
|
|
|
|
|
|
|
compare = (originalPath, generatedPath, callback = (error, same) ->) ->
|
2017-03-08 06:48:36 -05:00
|
|
|
diff_file = "#{fixturePath(generatedPath)}-diff.png"
|
|
|
|
proc = ChildProcess.exec "compare -metric mae #{fixturePath(originalPath)} #{fixturePath(generatedPath)} #{diff_file}"
|
2014-02-12 12:27:43 -05:00
|
|
|
stderr = ""
|
|
|
|
proc.stderr.on "data", (chunk) -> stderr += chunk
|
|
|
|
proc.on "exit", () ->
|
|
|
|
if stderr.trim() == "0 (0)"
|
2017-03-08 06:48:36 -05:00
|
|
|
fs.unlink diff_file # remove output diff if test matches expected image
|
2014-02-12 12:27:43 -05:00
|
|
|
callback null, true
|
|
|
|
else
|
2017-01-31 05:40:05 -05:00
|
|
|
console.log "compare result", stderr
|
2014-02-12 12:27:43 -05:00
|
|
|
callback null, false
|
|
|
|
|
2017-04-07 06:11:27 -04:00
|
|
|
checkPdfInfo = (pdfPath, callback = (error, output) ->) ->
|
|
|
|
proc = ChildProcess.exec "pdfinfo #{fixturePath(pdfPath)}"
|
|
|
|
stdout = ""
|
|
|
|
proc.stdout.on "data", (chunk) -> stdout += chunk
|
|
|
|
proc.stderr.on "data", (chunk) -> console.log "STDERR", chunk.toString()
|
|
|
|
proc.on "exit", () ->
|
|
|
|
if stdout.match(/Optimized:\s+yes/)
|
|
|
|
callback null, true
|
|
|
|
else
|
|
|
|
console.log "pdfinfo result", stdout
|
|
|
|
callback null, false
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
compareMultiplePages = (project_id, callback = (error) ->) ->
|
|
|
|
compareNext = (page_no, callback) ->
|
|
|
|
path = "tmp/#{project_id}-source-#{page_no}.png"
|
|
|
|
fs.stat fixturePath(path), (error, stat) ->
|
|
|
|
if error?
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
compare "tmp/#{project_id}-source-#{page_no}.png", "tmp/#{project_id}-generated-#{page_no}.png", (error, same) =>
|
|
|
|
throw error if error?
|
|
|
|
same.should.equal true
|
|
|
|
compareNext page_no + 1, callback
|
|
|
|
compareNext 0, callback
|
|
|
|
|
2017-04-07 06:11:27 -04:00
|
|
|
comparePdf = (project_id, example_dir, callback = (error) ->) ->
|
|
|
|
convertToPng "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png", (error) =>
|
|
|
|
throw error if error?
|
|
|
|
convertToPng "examples/#{example_dir}/output.pdf", "tmp/#{project_id}-source.png", (error) =>
|
|
|
|
throw error if error?
|
|
|
|
fs.stat fixturePath("tmp/#{project_id}-source-0.png"), (error, stat) =>
|
|
|
|
if error?
|
|
|
|
compare "tmp/#{project_id}-source.png", "tmp/#{project_id}-generated.png", (error, same) =>
|
|
|
|
throw error if error?
|
|
|
|
same.should.equal true
|
|
|
|
callback()
|
|
|
|
else
|
|
|
|
compareMultiplePages project_id, (error) ->
|
|
|
|
throw error if error?
|
|
|
|
callback()
|
|
|
|
|
2014-02-12 12:27:43 -05:00
|
|
|
downloadAndComparePdf = (project_id, example_dir, url, callback = (error) ->) ->
|
|
|
|
writeStream = fs.createWriteStream(fixturePath("tmp/#{project_id}.pdf"))
|
|
|
|
request.get(url).pipe(writeStream)
|
|
|
|
writeStream.on "close", () =>
|
2017-04-07 06:11:27 -04:00
|
|
|
checkPdfInfo "tmp/#{project_id}.pdf", (error, optimised) =>
|
2014-02-12 12:27:43 -05:00
|
|
|
throw error if error?
|
2017-04-07 06:11:27 -04:00
|
|
|
optimised.should.equal true
|
|
|
|
comparePdf project_id, example_dir, callback
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
Client.runServer(4242, fixturePath("examples"))
|
|
|
|
|
|
|
|
describe "Example Documents", ->
|
|
|
|
before (done) ->
|
|
|
|
ChildProcess.exec("rm test/acceptance/fixtures/tmp/*").on "exit", () -> done()
|
|
|
|
|
|
|
|
for example_dir in fs.readdirSync fixturePath("examples")
|
|
|
|
do (example_dir) ->
|
|
|
|
describe example_dir, ->
|
|
|
|
before ->
|
2017-03-08 06:48:36 -05:00
|
|
|
@project_id = Client.randomId() + "_" + example_dir
|
2014-02-12 12:27:43 -05:00
|
|
|
|
|
|
|
it "should generate the correct pdf", (done) ->
|
|
|
|
Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) =>
|
2017-09-08 09:06:04 -04:00
|
|
|
if error || body?.compile?.status is "failure"
|
2017-01-31 05:47:49 -05:00
|
|
|
console.log "DEBUG: error", error, "body", JSON.stringify(body)
|
2014-02-12 12:27:43 -05:00
|
|
|
pdf = Client.getOutputFile body, "pdf"
|
|
|
|
downloadAndComparePdf(@project_id, example_dir, pdf.url, done)
|
|
|
|
|
|
|
|
it "should generate the correct pdf on the second run as well", (done) ->
|
|
|
|
Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) =>
|
2017-09-08 09:06:04 -04:00
|
|
|
if error || body?.compile?.status is "failure"
|
2017-01-31 05:47:49 -05:00
|
|
|
console.log "DEBUG: error", error, "body", JSON.stringify(body)
|
2014-02-12 12:27:43 -05:00
|
|
|
pdf = Client.getOutputFile body, "pdf"
|
|
|
|
downloadAndComparePdf(@project_id, example_dir, pdf.url, done)
|
|
|
|
|
|
|
|
|