| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- /* eslint-disable
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS101: Remove unnecessary use of Array.from
- * DS102: Remove unnecessary code created because of implicit returns
- * DS103: Rewrite code to no longer use __guard__
- * DS207: Consider shorter variations of null checks
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const Client = require('./helpers/Client')
- const request = require('request')
- const fs = require('fs')
- const fsExtra = require('fs-extra')
- const ChildProcess = require('child_process')
- const ClsiApp = require('./helpers/ClsiApp')
- const logger = require('@overleaf/logger')
- const Path = require('path')
- const fixturePath = path => {
- if (path.slice(0, 3) === 'tmp') {
- return '/tmp/clsi_acceptance_tests' + path.slice(3)
- }
- return Path.join(__dirname, '../fixtures/', path)
- }
- const process = require('process')
- console.log(
- process.pid,
- process.ppid,
- process.getuid(),
- process.getgroups(),
- 'PID'
- )
- const MOCHA_LATEX_TIMEOUT = 60 * 1000
- const convertToPng = function (pdfPath, pngPath, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`
- console.log('COMMAND')
- console.log(command)
- const convert = ChildProcess.exec(command)
- const stdout = ''
- convert.stdout.on('data', chunk => console.log('STDOUT', chunk.toString()))
- convert.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
- return convert.on('exit', () => callback())
- }
- const compare = function (originalPath, generatedPath, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const diffFile = `${fixturePath(generatedPath)}-diff.png`
- const proc = ChildProcess.exec(
- `compare -metric mae ${fixturePath(originalPath)} ${fixturePath(
- generatedPath
- )} ${diffFile}`
- )
- let stderr = ''
- proc.stderr.on('data', chunk => (stderr += chunk))
- return proc.on('exit', () => {
- if (stderr.trim() === '0 (0)') {
- // remove output diff if test matches expected image
- fs.unlink(diffFile, err => {
- if (err) {
- throw err
- }
- })
- return callback(null, true)
- } else {
- console.log('compare result', stderr)
- return callback(null, false)
- }
- })
- }
- const checkPdfInfo = function (pdfPath, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`)
- let stdout = ''
- proc.stdout.on('data', chunk => (stdout += chunk))
- proc.stderr.on('data', chunk => console.log('STDERR', chunk.toString()))
- return proc.on('exit', () => {
- if (stdout.match(/Optimized:\s+yes/)) {
- return callback(null, true)
- } else {
- return callback(null, false)
- }
- })
- }
- const compareMultiplePages = function (projectId, callback) {
- if (callback == null) {
- callback = function () {}
- }
- function compareNext(pageNo, callback) {
- const path = `tmp/${projectId}-source-${pageNo}.png`
- return fs.stat(fixturePath(path), (error, stat) => {
- if (error != null) {
- return callback()
- } else {
- return compare(
- `tmp/${projectId}-source-${pageNo}.png`,
- `tmp/${projectId}-generated-${pageNo}.png`,
- (error, same) => {
- if (error != null) {
- throw error
- }
- same.should.equal(true)
- return compareNext(pageNo + 1, callback)
- }
- )
- }
- })
- }
- return compareNext(0, callback)
- }
- const comparePdf = function (projectId, exampleDir, callback) {
- if (callback == null) {
- callback = function () {}
- }
- console.log('CONVERT')
- console.log(`tmp/${projectId}.pdf`, `tmp/${projectId}-generated.png`)
- return convertToPng(
- `tmp/${projectId}.pdf`,
- `tmp/${projectId}-generated.png`,
- error => {
- if (error != null) {
- throw error
- }
- return convertToPng(
- `examples/${exampleDir}/output.pdf`,
- `tmp/${projectId}-source.png`,
- error => {
- if (error != null) {
- throw error
- }
- return fs.stat(
- fixturePath(`tmp/${projectId}-source-0.png`),
- (error, stat) => {
- if (error != null) {
- return compare(
- `tmp/${projectId}-source.png`,
- `tmp/${projectId}-generated.png`,
- (error, same) => {
- if (error != null) {
- throw error
- }
- same.should.equal(true)
- return callback()
- }
- )
- } else {
- return compareMultiplePages(projectId, error => {
- if (error != null) {
- throw error
- }
- return callback()
- })
- }
- }
- )
- }
- )
- }
- )
- }
- const downloadAndComparePdf = function (projectId, exampleDir, url, callback) {
- if (callback == null) {
- callback = function () {}
- }
- const writeStream = fs.createWriteStream(fixturePath(`tmp/${projectId}.pdf`))
- request.get(url).pipe(writeStream)
- console.log('writing file out', fixturePath(`tmp/${projectId}.pdf`))
- return writeStream.on('close', () => {
- return checkPdfInfo(`tmp/${projectId}.pdf`, (error, optimised) => {
- if (error != null) {
- throw error
- }
- optimised.should.equal(true)
- return comparePdf(projectId, exampleDir, callback)
- })
- })
- }
- Client.runServer(4242, fixturePath('examples'))
- describe('Example Documents', function () {
- before(function (done) {
- ClsiApp.ensureRunning(done)
- })
- before(function (done) {
- fsExtra.remove(fixturePath('tmp'), done)
- })
- before(function (done) {
- fs.mkdir(fixturePath('tmp'), done)
- })
- after(function (done) {
- fsExtra.remove(fixturePath('tmp'), done)
- })
- return Array.from(fs.readdirSync(fixturePath('examples'))).map(exampleDir =>
- (exampleDir =>
- describe(exampleDir, function () {
- before(function () {
- return (this.project_id = Client.randomId() + '_' + exampleDir)
- })
- it('should generate the correct pdf', function (done) {
- this.timeout(MOCHA_LATEX_TIMEOUT)
- return Client.compileDirectory(
- this.project_id,
- fixturePath('examples'),
- exampleDir,
- 4242,
- (error, res, body) => {
- if (
- error ||
- __guard__(
- body != null ? body.compile : undefined,
- x => x.status
- ) === 'failure'
- ) {
- console.log('DEBUG: error', error, 'body', JSON.stringify(body))
- return done(new Error('Compile failed'))
- }
- const pdf = Client.getOutputFile(body, 'pdf')
- return downloadAndComparePdf(
- this.project_id,
- exampleDir,
- pdf.url,
- done
- )
- }
- )
- })
- return it('should generate the correct pdf on the second run as well', function (done) {
- this.timeout(MOCHA_LATEX_TIMEOUT)
- return Client.compileDirectory(
- this.project_id,
- fixturePath('examples'),
- exampleDir,
- 4242,
- (error, res, body) => {
- if (
- error ||
- __guard__(
- body != null ? body.compile : undefined,
- x => x.status
- ) === 'failure'
- ) {
- console.log('DEBUG: error', error, 'body', JSON.stringify(body))
- return done(new Error('Compile failed'))
- }
- const pdf = Client.getOutputFile(body, 'pdf')
- return downloadAndComparePdf(
- this.project_id,
- exampleDir,
- pdf.url,
- done
- )
- }
- )
- })
- }))(exampleDir)
- )
- })
- function __guard__(value, transform) {
- return typeof value !== 'undefined' && value !== null
- ? transform(value)
- : undefined
- }
|