Prettier: convert test/acceptance decaffeinated files to Prettier format

This commit is contained in:
Simon Detheridge 2019-12-16 10:42:41 +00:00
parent b8e7abd25e
commit e4b1106761
2 changed files with 364 additions and 322 deletions

View file

@ -13,67 +13,97 @@
* DS207: Consider shorter variations of null checks * DS207: Consider shorter variations of null checks
* 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 app = require('../../../app'); const app = require('../../../app')
require("logger-sharelatex").logger.level("info"); require('logger-sharelatex').logger.level('info')
const logger = require("logger-sharelatex"); const logger = require('logger-sharelatex')
const Settings = require("settings-sharelatex"); const Settings = require('settings-sharelatex')
const request = require('request'); const request = require('request')
const S3_TRIES = 30; const S3_TRIES = 30
module.exports = { module.exports = {
running: false, running: false,
initing: false, initing: false,
callbacks: [], callbacks: [],
ensureRunning(callback) { ensureRunning(callback) {
if (callback == null) { callback = function(error) {}; } if (callback == null) {
callback = function(error) {}
}
if (this.running) { if (this.running) {
return callback(); return callback()
} else if (this.initing) { } else if (this.initing) {
return this.callbacks.push(callback); return this.callbacks.push(callback)
} else { } else {
this.initing = true; this.initing = true
this.callbacks.push(callback); this.callbacks.push(callback)
return app.listen(__guard__(Settings.internal != null ? Settings.internal.filestore : undefined, x => x.port), "localhost", error => { return app.listen(
if (error != null) { throw error; } __guard__(
this.running = true; Settings.internal != null ? Settings.internal.filestore : undefined,
logger.log("filestore running in dev mode"); x => x.port
),
'localhost',
error => {
if (error != null) {
throw error
}
this.running = true
logger.log('filestore running in dev mode')
return (() => { return (() => {
const result = []; const result = []
for (callback of Array.from(this.callbacks)) { for (callback of Array.from(this.callbacks)) {
result.push(callback()); result.push(callback())
} }
return result; return result
})(); })()
}); }
)
} }
}, },
waitForS3(callback, tries) { waitForS3(callback, tries) {
if (!(Settings.filestore.s3 != null ? Settings.filestore.s3.endpoint : undefined)) { return callback(); } if (
if (!tries) { tries = 1; } !(Settings.filestore.s3 != null
? Settings.filestore.s3.endpoint
: undefined)
) {
return callback()
}
if (!tries) {
tries = 1
}
return request.get(`${Settings.filestore.s3.endpoint}/`, (err, response) => { return request.get(
console.log(err, response != null ? response.statusCode : undefined, tries); `${Settings.filestore.s3.endpoint}/`,
if (!err && [200, 404].includes(response != null ? response.statusCode : undefined)) { (err, response) => {
return callback(); console.log(
err,
response != null ? response.statusCode : undefined,
tries
)
if (
!err &&
[200, 404].includes(
response != null ? response.statusCode : undefined
)
) {
return callback()
} }
if (tries === S3_TRIES) { if (tries === S3_TRIES) {
return callback('timed out waiting for S3'); return callback('timed out waiting for S3')
} }
return setTimeout( return setTimeout(() => {
() => { return this.waitForS3(callback, tries + 1)
return this.waitForS3(callback, tries + 1); }, 1000)
}, }
1000 )
); }
});
} }
};
function __guard__(value, transform) { function __guard__(value, transform) {
return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined; return typeof value !== 'undefined' && value !== null
? transform(value)
: undefined
} }

View file

@ -12,303 +12,315 @@
* DS103: Rewrite code to no longer use __guard__ * DS103: Rewrite code to no longer use __guard__
* 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 { const { assert } = require('chai')
assert const sinon = require('sinon')
} = require("chai"); const chai = require('chai')
const sinon = require('sinon'); const should = chai.should()
const chai = require('chai'); const { expect } = chai
const should = chai.should(); const modulePath = '../../../app/js/LocalFileWriter.js'
const { const SandboxedModule = require('sandboxed-module')
expect const fs = require('fs')
} = chai; const request = require('request')
const modulePath = "../../../app/js/LocalFileWriter.js"; const settings = require('settings-sharelatex')
const SandboxedModule = require('sandboxed-module'); const FilestoreApp = require('./FilestoreApp')
const fs = require("fs"); const async = require('async')
const request = require("request");
const settings = require("settings-sharelatex");
const FilestoreApp = require("./FilestoreApp");
const async = require('async');
const getMetric = (filestoreUrl, metric, cb) =>
request.get(`${filestoreUrl}/metrics`, function(err, res) {
expect(res.statusCode).to.equal(200)
const metricRegex = new RegExp(`^${metric}{[^}]+} ([0-9]+)$`, 'm')
return cb(parseInt(__guard__(metricRegex.exec(res.body), x => x[1]) || '0'))
})
const getMetric = (filestoreUrl, metric, cb) => request.get(`${filestoreUrl}/metrics`, function(err, res) { describe('Filestore', function() {
expect(res.statusCode).to.equal(200);
const metricRegex = new RegExp(`^${metric}{[^}]+} ([0-9]+)$`, "m");
return cb(parseInt(__guard__(metricRegex.exec(res.body), x => x[1]) || '0'));
});
describe("Filestore", function() {
before(function(done) { before(function(done) {
this.localFileReadPath = "/tmp/filestore_acceptence_tests_file_read.txt"; this.localFileReadPath = '/tmp/filestore_acceptence_tests_file_read.txt'
this.localFileWritePath = "/tmp/filestore_acceptence_tests_file_write.txt"; this.localFileWritePath = '/tmp/filestore_acceptence_tests_file_write.txt'
this.constantFileContent = [ this.constantFileContent = [
"hello world", 'hello world',
`line 2 goes here ${Math.random()}`, `line 2 goes here ${Math.random()}`,
"there are 3 lines in all" 'there are 3 lines in all'
].join("\n"); ].join('\n')
this.filestoreUrl = `http://localhost:${settings.internal.filestore.port}`; this.filestoreUrl = `http://localhost:${settings.internal.filestore.port}`
return fs.writeFile(this.localFileReadPath, this.constantFileContent, function(err) { return fs.writeFile(
if (err) { return done(err); } this.localFileReadPath,
return FilestoreApp.waitForS3(done); this.constantFileContent,
}); function(err) {
}); if (err) {
return done(err)
}
return FilestoreApp.waitForS3(done)
}
)
})
beforeEach(function(done) { beforeEach(function(done) {
return FilestoreApp.ensureRunning(() => { return FilestoreApp.ensureRunning(() => {
return async.parallel([ return async.parallel(
[
cb => { cb => {
return fs.unlink(this.localFileWritePath, () => cb()); return fs.unlink(this.localFileWritePath, () => cb())
}, },
cb => { cb => {
return getMetric(this.filestoreUrl, 's3_egress', metric => { return getMetric(this.filestoreUrl, 's3_egress', metric => {
this.previousEgress = metric; this.previousEgress = metric
return cb(); return cb()
}); })
}, },
cb => { cb => {
return getMetric(this.filestoreUrl, 's3_ingress', metric => { return getMetric(this.filestoreUrl, 's3_ingress', metric => {
this.previousIngress = metric; this.previousIngress = metric
return cb(); return cb()
}); })
} }
], done); ],
}); done
}); )
})
})
it("should send a 200 for status endpoint", function(done){ it('should send a 200 for status endpoint', function(done) {
return request(`${this.filestoreUrl}/status`, function(err, response, body){ return request(`${this.filestoreUrl}/status`, function(
response.statusCode.should.equal(200); err,
body.indexOf("filestore").should.not.equal(-1); response,
body.indexOf("up").should.not.equal(-1); body
return done(); ) {
}); response.statusCode.should.equal(200)
}); body.indexOf('filestore').should.not.equal(-1)
body.indexOf('up').should.not.equal(-1)
describe("with a file on the server", function() { return done()
})
})
describe('with a file on the server', function() {
beforeEach(function(done) { beforeEach(function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
this.file_id = Math.random(); this.file_id = Math.random()
this.fileUrl = `${this.filestoreUrl}/project/acceptence_tests/file/${this.file_id}`; this.fileUrl = `${this.filestoreUrl}/project/acceptence_tests/file/${this.file_id}`
const writeStream = request.post(this.fileUrl); const writeStream = request.post(this.fileUrl)
writeStream.on("end", done); writeStream.on('end', done)
return fs.createReadStream(this.localFileReadPath).pipe(writeStream); return fs.createReadStream(this.localFileReadPath).pipe(writeStream)
}); })
it("should return 404 for a non-existant id", function(done) { it('should return 404 for a non-existant id', function(done) {
this.timeout(1000 * 20); this.timeout(1000 * 20)
const options = const options = { uri: this.fileUrl + '___this_is_clearly_wrong___' }
{uri: this.fileUrl + '___this_is_clearly_wrong___'};
return request.get(options, (err, response, body) => { return request.get(options, (err, response, body) => {
response.statusCode.should.equal(404); response.statusCode.should.equal(404)
return done(); return done()
}); })
}); })
it('should record an egress metric for the upload', function(done) { it('should record an egress metric for the upload', function(done) {
return getMetric(this.filestoreUrl, 's3_egress', metric => { return getMetric(this.filestoreUrl, 's3_egress', metric => {
expect(metric - this.previousEgress).to.equal(this.constantFileContent.length); expect(metric - this.previousEgress).to.equal(
return done(); this.constantFileContent.length
}); )
}); return done()
})
})
it("should return the file size on a HEAD request", function(done) { it('should return the file size on a HEAD request', function(done) {
const expectedLength = Buffer.byteLength(this.constantFileContent); const expectedLength = Buffer.byteLength(this.constantFileContent)
return request.head(this.fileUrl, (err, res) => { return request.head(this.fileUrl, (err, res) => {
expect(res.statusCode).to.equal(200); expect(res.statusCode).to.equal(200)
expect(res.headers['content-length']).to.equal(expectedLength.toString()); expect(res.headers['content-length']).to.equal(
return done(); expectedLength.toString()
}); )
}); return done()
})
})
it("should be able get the file back", function(done){ it('should be able get the file back', function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
return request.get(this.fileUrl, (err, response, body) => { return request.get(this.fileUrl, (err, response, body) => {
body.should.equal(this.constantFileContent); body.should.equal(this.constantFileContent)
return done(); return done()
}); })
}); })
it("should record an ingress metric when downloading the file", function(done){ it('should record an ingress metric when downloading the file', function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
return request.get(this.fileUrl, () => { return request.get(this.fileUrl, () => {
return getMetric(this.filestoreUrl, 's3_ingress', metric => { return getMetric(this.filestoreUrl, 's3_ingress', metric => {
expect(metric - this.previousIngress).to.equal(this.constantFileContent.length); expect(metric - this.previousIngress).to.equal(
return done(); this.constantFileContent.length
}); )
}); return done()
}); })
})
})
it("should be able to get back the first 9 bytes of the file", function(done) { it('should be able to get back the first 9 bytes of the file', function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
const options = { const options = {
uri: this.fileUrl, uri: this.fileUrl,
headers: { headers: {
'Range': 'bytes=0-8' Range: 'bytes=0-8'
}
} }
};
return request.get(options, (err, response, body) => { return request.get(options, (err, response, body) => {
body.should.equal('hello wor'); body.should.equal('hello wor')
return done(); return done()
}); })
}); })
it("should record an ingress metric for a partial download", function(done){ it('should record an ingress metric for a partial download', function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
const options = { const options = {
uri: this.fileUrl, uri: this.fileUrl,
headers: { headers: {
'Range': 'bytes=0-8' Range: 'bytes=0-8'
}
} }
};
return request.get(options, () => { return request.get(options, () => {
return getMetric(this.filestoreUrl, 's3_ingress', metric => { return getMetric(this.filestoreUrl, 's3_ingress', metric => {
expect(metric - this.previousIngress).to.equal(9); expect(metric - this.previousIngress).to.equal(9)
return done(); return done()
}); })
}); })
}); })
it("should be able to get back bytes 4 through 10 of the file", function(done) { it('should be able to get back bytes 4 through 10 of the file', function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
const options = { const options = {
uri: this.fileUrl, uri: this.fileUrl,
headers: { headers: {
'Range': 'bytes=4-10' Range: 'bytes=4-10'
}
} }
};
return request.get(options, (err, response, body) => { return request.get(options, (err, response, body) => {
body.should.equal('o world'); body.should.equal('o world')
return done(); return done()
}); })
}); })
it("should be able to delete the file", function(done){ it('should be able to delete the file', function(done) {
this.timeout(1000 * 20); this.timeout(1000 * 20)
return request.del(this.fileUrl, (err, response, body) => { return request.del(this.fileUrl, (err, response, body) => {
response.statusCode.should.equal(204); response.statusCode.should.equal(204)
return request.get(this.fileUrl, (err, response, body) => { return request.get(this.fileUrl, (err, response, body) => {
response.statusCode.should.equal(404); response.statusCode.should.equal(404)
return done(); return done()
}); })
}); })
}); })
return it("should be able to copy files", function(done){ return it('should be able to copy files', function(done) {
this.timeout(1000 * 20); this.timeout(1000 * 20)
const newProjectID = "acceptence_tests_copyied_project"; const newProjectID = 'acceptence_tests_copyied_project'
const newFileId = Math.random(); const newFileId = Math.random()
const newFileUrl = `${this.filestoreUrl}/project/${newProjectID}/file/${newFileId}`; const newFileUrl = `${this.filestoreUrl}/project/${newProjectID}/file/${newFileId}`
const opts = { const opts = {
method: 'put', method: 'put',
uri: newFileUrl, uri: newFileUrl,
json: { json: {
source: { source: {
project_id:"acceptence_tests", project_id: 'acceptence_tests',
file_id: this.file_id file_id: this.file_id
} }
} }
}; }
return request(opts, (err, response, body) => { return request(opts, (err, response, body) => {
response.statusCode.should.equal(200); response.statusCode.should.equal(200)
return request.del(this.fileUrl, (err, response, body) => { return request.del(this.fileUrl, (err, response, body) => {
response.statusCode.should.equal(204); response.statusCode.should.equal(204)
return request.get(newFileUrl, (err, response, body) => { return request.get(newFileUrl, (err, response, body) => {
body.should.equal(this.constantFileContent); body.should.equal(this.constantFileContent)
return done(); return done()
}); })
}); })
}); })
}); })
}); })
return describe("with a pdf file", function() {
return describe('with a pdf file', function() {
beforeEach(function(done) { beforeEach(function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
this.file_id = Math.random(); this.file_id = Math.random()
this.fileUrl = `${this.filestoreUrl}/project/acceptence_tests/file/${this.file_id}`; this.fileUrl = `${this.filestoreUrl}/project/acceptence_tests/file/${this.file_id}`
this.localFileReadPath = __dirname + '/../../fixtures/test.pdf'; this.localFileReadPath = __dirname + '/../../fixtures/test.pdf'
return fs.stat(this.localFileReadPath, (err, stat) => { return fs.stat(this.localFileReadPath, (err, stat) => {
this.localFileSize = stat.size; this.localFileSize = stat.size
const writeStream = request.post(this.fileUrl); const writeStream = request.post(this.fileUrl)
writeStream.on("end", done); writeStream.on('end', done)
return fs.createReadStream(this.localFileReadPath).pipe(writeStream); return fs.createReadStream(this.localFileReadPath).pipe(writeStream)
}); })
}); })
it('should record an egress metric for the upload', function(done) { it('should record an egress metric for the upload', function(done) {
return getMetric(this.filestoreUrl, 's3_egress', metric => { return getMetric(this.filestoreUrl, 's3_egress', metric => {
expect(metric - this.previousEgress).to.equal(this.localFileSize); expect(metric - this.previousEgress).to.equal(this.localFileSize)
return done(); return done()
}); })
}); })
it("should be able get the file back", function(done){ it('should be able get the file back', function(done) {
this.timeout(1000 * 10); this.timeout(1000 * 10)
return request.get(this.fileUrl, (err, response, body) => { return request.get(this.fileUrl, (err, response, body) => {
expect(body.substring(0, 8)).to.equal('%PDF-1.5'); expect(body.substring(0, 8)).to.equal('%PDF-1.5')
return done(); return done()
}); })
}); })
describe("getting the preview image", function() {
describe('getting the preview image', function() {
beforeEach(function() { beforeEach(function() {
return this.previewFileUrl = `${this.fileUrl}?style=preview`; return (this.previewFileUrl = `${this.fileUrl}?style=preview`)
}); })
it("should not time out", function(done) { it('should not time out', function(done) {
this.timeout(1000 * 20); this.timeout(1000 * 20)
return request.get(this.previewFileUrl, (err, response, body) => { return request.get(this.previewFileUrl, (err, response, body) => {
expect(response).to.not.equal(null); expect(response).to.not.equal(null)
return done(); return done()
}); })
}); })
return it("should respond with image data", function(done) { return it('should respond with image data', function(done) {
// note: this test relies of the imagemagick conversion working // note: this test relies of the imagemagick conversion working
this.timeout(1000 * 20); this.timeout(1000 * 20)
return request.get(this.previewFileUrl, (err, response, body) => { return request.get(this.previewFileUrl, (err, response, body) => {
expect(response.statusCode).to.equal(200); expect(response.statusCode).to.equal(200)
expect(body.length).to.be.greaterThan(400); expect(body.length).to.be.greaterThan(400)
return done(); return done()
}); })
}); })
}); })
return describe("warming the cache", function() {
return describe('warming the cache', function() {
beforeEach(function() { beforeEach(function() {
return this.fileUrl = this.fileUrl + '?style=preview&cacheWarm=true'; return (this.fileUrl = this.fileUrl + '?style=preview&cacheWarm=true')
}); })
it("should not time out", function(done) { it('should not time out', function(done) {
this.timeout(1000 * 20); this.timeout(1000 * 20)
return request.get(this.fileUrl, (err, response, body) => { return request.get(this.fileUrl, (err, response, body) => {
expect(response).to.not.equal(null); expect(response).to.not.equal(null)
return done(); return done()
}); })
}); })
return it("should respond with only an 'OK'", function(done) { return it("should respond with only an 'OK'", function(done) {
// note: this test relies of the imagemagick conversion working // note: this test relies of the imagemagick conversion working
this.timeout(1000 * 20); this.timeout(1000 * 20)
return request.get(this.fileUrl, (err, response, body) => { return request.get(this.fileUrl, (err, response, body) => {
expect(response.statusCode).to.equal(200); expect(response.statusCode).to.equal(200)
body.should.equal('OK'); body.should.equal('OK')
return done(); return done()
}); })
}); })
}); })
}); })
}); })
function __guard__(value, transform) { function __guard__(value, transform) {
return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined; return typeof value !== 'undefined' && value !== null
? transform(value)
: undefined
} }