Merge pull request #12815 from overleaf/jpa-pipe-tests

[misc] tests: migrate to stream.pipeline or request({ body: stream })

GitOrigin-RevId: 5de077c9ba9c7951d6fec7b1d6da6fb694394610
This commit is contained in:
Jakob Ackermann 2023-04-26 14:40:14 +01:00 committed by Copybot
parent 80fdef796c
commit 5380999459

View file

@ -13,7 +13,8 @@
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/ */
const Client = require('./helpers/Client') const Client = require('./helpers/Client')
const request = require('request') const fetch = require('node-fetch')
const { pipeline } = require('stream')
const fs = require('fs') const fs = require('fs')
const fsExtra = require('fs-extra') const fsExtra = require('fs-extra')
const ChildProcess = require('child_process') const ChildProcess = require('child_process')
@ -175,21 +176,25 @@ const comparePdf = function (projectId, exampleDir, callback) {
} }
const downloadAndComparePdf = function (projectId, exampleDir, url, callback) { const downloadAndComparePdf = function (projectId, exampleDir, url, callback) {
if (callback == null) { fetch(url)
callback = function () {} .then(res => {
} if (!res.ok) {
const writeStream = fs.createWriteStream(fixturePath(`tmp/${projectId}.pdf`)) return callback(new Error('non success response: ' + res.statusText))
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) const dest = fs.createWriteStream(fixturePath(`tmp/${projectId}.pdf`))
pipeline(res.body, dest, err => {
if (err) return callback(err)
checkPdfInfo(`tmp/${projectId}.pdf`, (err, optimised) => {
if (err) return callback(err)
optimised.should.equal(true)
comparePdf(projectId, exampleDir, callback)
})
})
}) })
}) .catch(callback)
} }
Client.runServer(4242, fixturePath('examples')) Client.runServer(4242, fixturePath('examples'))