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