From 778f2a84201d36781bd883180cb528f05bd1dc17 Mon Sep 17 00:00:00 2001 From: mserranom Date: Wed, 19 Feb 2020 12:16:14 +0100 Subject: [PATCH] prettier: convert test/acceptance decaffeinated files to Prettier format --- .../acceptance/js/BrokenLatexFileTests.js | 115 ++-- .../test/acceptance/js/DeleteOldFilesTest.js | 93 ++-- .../acceptance/js/ExampleDocumentTests.js | 390 ++++++++------ .../acceptance/js/SimpleLatexFileTests.js | 97 ++-- .../clsi/test/acceptance/js/SynctexTests.js | 116 ++-- .../clsi/test/acceptance/js/TimeoutTests.js | 75 +-- .../test/acceptance/js/UrlCachingTests.js | 503 ++++++++++-------- .../clsi/test/acceptance/js/WordcountTests.js | 102 ++-- .../clsi/test/acceptance/js/helpers/Client.js | 306 ++++++----- .../test/acceptance/js/helpers/ClsiApp.js | 79 +-- 10 files changed, 1105 insertions(+), 771 deletions(-) diff --git a/services/clsi/test/acceptance/js/BrokenLatexFileTests.js b/services/clsi/test/acceptance/js/BrokenLatexFileTests.js index 2db36c1414..b34d23cad5 100644 --- a/services/clsi/test/acceptance/js/BrokenLatexFileTests.js +++ b/services/clsi/test/acceptance/js/BrokenLatexFileTests.js @@ -8,68 +8,81 @@ * DS102: Remove unnecessary code created because of implicit returns * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ -const Client = require("./helpers/Client"); -const request = require("request"); -require("chai").should(); -const ClsiApp = require("./helpers/ClsiApp"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const ClsiApp = require('./helpers/ClsiApp') -describe("Broken LaTeX file", function() { - before(function(done){ - this.broken_request = { - resources: [{ - path: "main.tex", - content: `\ +describe('Broken LaTeX file', function() { + before(function(done) { + this.broken_request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{articl % :( \\begin{documen % :( Broken \\end{documen % :(\ ` - } - ] - }; - this.correct_request = { - resources: [{ - path: "main.tex", - content: `\ + } + ] + } + this.correct_request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\begin{document} Hello world \\end{document}\ ` - } - ] - }; - return ClsiApp.ensureRunning(done); - }); - - describe("on first run", function() { - before(function(done) { - this.project_id = Client.randomId(); - return Client.compile(this.project_id, this.broken_request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); }); - }); + } + ] + } + return ClsiApp.ensureRunning(done) + }) - return it("should return a failure status", function() { - return this.body.compile.status.should.equal("failure"); - }); - }); + describe('on first run', function() { + before(function(done) { + this.project_id = Client.randomId() + return Client.compile( + this.project_id, + this.broken_request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) - return describe("on second run", function() { - before(function(done) { - this.project_id = Client.randomId(); - return Client.compile(this.project_id, this.correct_request, () => { - return Client.compile(this.project_id, this.broken_request, (error, res, body) => { - this.error = error; - this.res = res; - this.body = body; - return done(); - }); - }); - }); + return it('should return a failure status', function() { + return this.body.compile.status.should.equal('failure') + }) + }) - return it("should return a failure status", function() { - return this.body.compile.status.should.equal("failure"); - }); - }); -}); - - + return describe('on second run', function() { + before(function(done) { + this.project_id = Client.randomId() + return Client.compile(this.project_id, this.correct_request, () => { + return Client.compile( + this.project_id, + this.broken_request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) + }) + + return it('should return a failure status', function() { + return this.body.compile.status.should.equal('failure') + }) + }) +}) diff --git a/services/clsi/test/acceptance/js/DeleteOldFilesTest.js b/services/clsi/test/acceptance/js/DeleteOldFilesTest.js index 720b90f2ba..83d7c96d0d 100644 --- a/services/clsi/test/acceptance/js/DeleteOldFilesTest.js +++ b/services/clsi/test/acceptance/js/DeleteOldFilesTest.js @@ -8,53 +8,66 @@ * DS102: Remove unnecessary code created because of implicit returns * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ -const Client = require("./helpers/Client"); -const request = require("request"); -require("chai").should(); -const ClsiApp = require("./helpers/ClsiApp"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const ClsiApp = require('./helpers/ClsiApp') -describe("Deleting Old Files", function() { - before(function(done){ - this.request = { - resources: [{ - path: "main.tex", - content: `\ +describe('Deleting Old Files', function() { + before(function(done) { + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\begin{document} Hello world \\end{document}\ ` - } - ] - }; - return ClsiApp.ensureRunning(done); - }); + } + ] + } + return ClsiApp.ensureRunning(done) + }) - return describe("on first run", function() { - before(function(done) { - this.project_id = Client.randomId(); - return Client.compile(this.project_id, this.request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); }); - }); + return describe('on first run', function() { + before(function(done) { + this.project_id = Client.randomId() + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) - it("should return a success status", function() { - return this.body.compile.status.should.equal("success"); - }); + it('should return a success status', function() { + return this.body.compile.status.should.equal('success') + }) - return describe("after file has been deleted", function() { - before(function(done) { - this.request.resources = []; - return Client.compile(this.project_id, this.request, (error, res, body) => { - this.error = error; - this.res = res; - this.body = body; - return done(); - }); - }); - - return it("should return a failure status", function() { - return this.body.compile.status.should.equal("failure"); - }); - }); - }); -}); + return describe('after file has been deleted', function() { + before(function(done) { + this.request.resources = [] + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) + return it('should return a failure status', function() { + return this.body.compile.status.should.equal('failure') + }) + }) + }) +}) diff --git a/services/clsi/test/acceptance/js/ExampleDocumentTests.js b/services/clsi/test/acceptance/js/ExampleDocumentTests.js index 4c3080f3a5..110b5d6f9a 100644 --- a/services/clsi/test/acceptance/js/ExampleDocumentTests.js +++ b/services/clsi/test/acceptance/js/ExampleDocumentTests.js @@ -15,176 +15,266 @@ * 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"); -require("chai").should(); -const fs = require("fs"); -const ChildProcess = require("child_process"); -const ClsiApp = require("./helpers/ClsiApp"); -const logger = require("logger-sharelatex"); -const Path = require("path"); -const fixturePath = path => Path.normalize(__dirname + "/../fixtures/" + path); -const process = require("process"); -console.log(process.pid, process.ppid, process.getuid(),process.getgroups(), "PID"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const fs = require('fs') +const ChildProcess = require('child_process') +const ClsiApp = require('./helpers/ClsiApp') +const logger = require('logger-sharelatex') +const Path = require('path') +const fixturePath = path => Path.normalize(__dirname + '/../fixtures/' + path) +const process = require('process') +console.log( + process.pid, + process.ppid, + process.getuid(), + process.getgroups(), + 'PID' +) try { - console.log("creating tmp directory", fixturePath("tmp")); - fs.mkdirSync(fixturePath("tmp")); + console.log('creating tmp directory', fixturePath('tmp')) + fs.mkdirSync(fixturePath('tmp')) } catch (error) { - const err = error; - console.log(err, fixturePath("tmp"), "unable to create fixture tmp path"); + const err = error + console.log(err, fixturePath('tmp'), 'unable to create fixture tmp path') } -const MOCHA_LATEX_TIMEOUT = 60 * 1000; +const MOCHA_LATEX_TIMEOUT = 60 * 1000 const convertToPng = function(pdfPath, pngPath, callback) { - if (callback == null) { callback = function(error) {}; } - 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()); -}; + if (callback == null) { + callback = function(error) {} + } + 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(error, same) {}; } - const diff_file = `${fixturePath(generatedPath)}-diff.png`; - const proc = ChildProcess.exec(`compare -metric mae ${fixturePath(originalPath)} ${fixturePath(generatedPath)} ${diff_file}`); - 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(diff_file, (err) => { - if (err) { - throw err; - } - }); - return callback(null, true); - } else { - console.log("compare result", stderr); - return callback(null, false); - } - }); -}; + if (callback == null) { + callback = function(error, same) {} + } + const diff_file = `${fixturePath(generatedPath)}-diff.png` + const proc = ChildProcess.exec( + `compare -metric mae ${fixturePath(originalPath)} ${fixturePath( + generatedPath + )} ${diff_file}` + ) + 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(diff_file, 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(error, output) {}; } - 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); - } - }); -}; + if (callback == null) { + callback = function(error, output) {} + } + 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(project_id, callback) { - if (callback == null) { callback = function(error) {}; } - var compareNext = function(page_no, callback) { - const path = `tmp/${project_id}-source-${page_no}.png`; - return fs.stat(fixturePath(path), (error, stat) => { - if (error != null) { - return callback(); - } else { - return compare(`tmp/${project_id}-source-${page_no}.png`, `tmp/${project_id}-generated-${page_no}.png`, (error, same) => { - if (error != null) { throw error; } - same.should.equal(true); - return compareNext(page_no + 1, callback); - }); - } - }); - }; - return compareNext(0, callback); -}; + if (callback == null) { + callback = function(error) {} + } + var compareNext = function(page_no, callback) { + const path = `tmp/${project_id}-source-${page_no}.png` + return fs.stat(fixturePath(path), (error, stat) => { + if (error != null) { + return callback() + } else { + return compare( + `tmp/${project_id}-source-${page_no}.png`, + `tmp/${project_id}-generated-${page_no}.png`, + (error, same) => { + if (error != null) { + throw error + } + same.should.equal(true) + return compareNext(page_no + 1, callback) + } + ) + } + }) + } + return compareNext(0, callback) +} const comparePdf = function(project_id, example_dir, callback) { - if (callback == null) { callback = function(error) {}; } - console.log("CONVERT"); - console.log(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`); - return convertToPng(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`, error => { - if (error != null) { throw error; } - return convertToPng(`examples/${example_dir}/output.pdf`, `tmp/${project_id}-source.png`, error => { - if (error != null) { throw error; } - return fs.stat(fixturePath(`tmp/${project_id}-source-0.png`), (error, stat) => { - if (error != null) { - return compare(`tmp/${project_id}-source.png`, `tmp/${project_id}-generated.png`, (error, same) => { - if (error != null) { throw error; } - same.should.equal(true); - return callback(); - }); - } else { - return compareMultiplePages(project_id, (error) => { - if (error != null) { throw error; } - return callback(); - }); - } - }); - }); - }); -}; + if (callback == null) { + callback = function(error) {} + } + console.log('CONVERT') + console.log(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`) + return convertToPng( + `tmp/${project_id}.pdf`, + `tmp/${project_id}-generated.png`, + error => { + if (error != null) { + throw error + } + return convertToPng( + `examples/${example_dir}/output.pdf`, + `tmp/${project_id}-source.png`, + error => { + if (error != null) { + throw error + } + return fs.stat( + fixturePath(`tmp/${project_id}-source-0.png`), + (error, stat) => { + if (error != null) { + return compare( + `tmp/${project_id}-source.png`, + `tmp/${project_id}-generated.png`, + (error, same) => { + if (error != null) { + throw error + } + same.should.equal(true) + return callback() + } + ) + } else { + return compareMultiplePages(project_id, error => { + if (error != null) { + throw error + } + return callback() + }) + } + } + ) + } + ) + } + ) +} const downloadAndComparePdf = function(project_id, example_dir, url, callback) { - if (callback == null) { callback = function(error) {}; } - const writeStream = fs.createWriteStream(fixturePath(`tmp/${project_id}.pdf`)); - request.get(url).pipe(writeStream); - console.log("writing file out", fixturePath(`tmp/${project_id}.pdf`)); - return writeStream.on("close", () => { - return checkPdfInfo(`tmp/${project_id}.pdf`, (error, optimised) => { - if (error != null) { throw error; } - optimised.should.equal(true); - return comparePdf(project_id, example_dir, callback); - }); - }); -}; + if (callback == null) { + callback = function(error) {} + } + const writeStream = fs.createWriteStream(fixturePath(`tmp/${project_id}.pdf`)) + request.get(url).pipe(writeStream) + console.log('writing file out', fixturePath(`tmp/${project_id}.pdf`)) + return writeStream.on('close', () => { + return checkPdfInfo(`tmp/${project_id}.pdf`, (error, optimised) => { + if (error != null) { + throw error + } + optimised.should.equal(true) + return comparePdf(project_id, example_dir, callback) + }) + }) +} -Client.runServer(4242, fixturePath("examples")); +Client.runServer(4242, fixturePath('examples')) -describe("Example Documents", function() { - before(function(done) { return ChildProcess.exec("rm test/acceptance/fixtures/tmp/*").on("exit", () => ClsiApp.ensureRunning(done)); } - ); +describe('Example Documents', function() { + before(function(done) { + return ChildProcess.exec('rm test/acceptance/fixtures/tmp/*').on( + 'exit', + () => ClsiApp.ensureRunning(done) + ) + }) + return Array.from(fs.readdirSync(fixturePath('examples'))).map(example_dir => + (example_dir => + describe(example_dir, function() { + before(function() { + return (this.project_id = Client.randomId() + '_' + example_dir) + }) - return Array.from(fs.readdirSync(fixturePath("examples"))).map((example_dir) => - (example_dir => - describe(example_dir, function() { - before(function() { - return this.project_id = Client.randomId() + "_" + example_dir; - }); - - it("should generate the correct pdf", function(done) { - this.timeout(MOCHA_LATEX_TIMEOUT); - return Client.compileDirectory(this.project_id, fixturePath("examples"), example_dir, 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)); - } - const pdf = Client.getOutputFile(body, "pdf"); - return downloadAndComparePdf(this.project_id, example_dir, 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"), example_dir, 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)); - } - const pdf = Client.getOutputFile(body, "pdf"); - return downloadAndComparePdf(this.project_id, example_dir, pdf.url, done); - }); - }); - }) - )(example_dir)); -}); - + it('should generate the correct pdf', function(done) { + this.timeout(MOCHA_LATEX_TIMEOUT) + return Client.compileDirectory( + this.project_id, + fixturePath('examples'), + example_dir, + 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)) + } + const pdf = Client.getOutputFile(body, 'pdf') + return downloadAndComparePdf( + this.project_id, + example_dir, + 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'), + example_dir, + 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)) + } + const pdf = Client.getOutputFile(body, 'pdf') + return downloadAndComparePdf( + this.project_id, + example_dir, + pdf.url, + done + ) + } + ) + }) + }))(example_dir) + ) +}) function __guard__(value, transform) { - return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined; -} \ No newline at end of file + return typeof value !== 'undefined' && value !== null + ? transform(value) + : undefined +} diff --git a/services/clsi/test/acceptance/js/SimpleLatexFileTests.js b/services/clsi/test/acceptance/js/SimpleLatexFileTests.js index d774301dd0..447e1b635a 100644 --- a/services/clsi/test/acceptance/js/SimpleLatexFileTests.js +++ b/services/clsi/test/acceptance/js/SimpleLatexFileTests.js @@ -8,55 +8,64 @@ * DS102: Remove unnecessary code created because of implicit returns * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ -const Client = require("./helpers/Client"); -const request = require("request"); -require("chai").should(); -const ClsiApp = require("./helpers/ClsiApp"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const ClsiApp = require('./helpers/ClsiApp') -describe("Simple LaTeX file", function() { - before(function(done) { - this.project_id = Client.randomId(); - this.request = { - resources: [{ - path: "main.tex", - content: `\ +describe('Simple LaTeX file', function() { + before(function(done) { + this.project_id = Client.randomId() + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\begin{document} Hello world \\end{document}\ ` - } - ] - }; - return ClsiApp.ensureRunning(() => { - return Client.compile(this.project_id, this.request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); }); - }); - }); + } + ] + } + return ClsiApp.ensureRunning(() => { + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) + }) - it("should return the PDF", function() { - const pdf = Client.getOutputFile(this.body, "pdf"); - return pdf.type.should.equal("pdf"); - }); - - it("should return the log", function() { - const log = Client.getOutputFile(this.body, "log"); - return log.type.should.equal("log"); - }); + it('should return the PDF', function() { + const pdf = Client.getOutputFile(this.body, 'pdf') + return pdf.type.should.equal('pdf') + }) - it("should provide the pdf for download", function(done) { - const pdf = Client.getOutputFile(this.body, "pdf"); - return request.get(pdf.url, (error, res, body) => { - res.statusCode.should.equal(200); - return done(); - }); - }); - - return it("should provide the log for download", function(done) { - const log = Client.getOutputFile(this.body, "pdf"); - return request.get(log.url, (error, res, body) => { - res.statusCode.should.equal(200); - return done(); - }); - }); -}); - + it('should return the log', function() { + const log = Client.getOutputFile(this.body, 'log') + return log.type.should.equal('log') + }) + + it('should provide the pdf for download', function(done) { + const pdf = Client.getOutputFile(this.body, 'pdf') + return request.get(pdf.url, (error, res, body) => { + res.statusCode.should.equal(200) + return done() + }) + }) + + return it('should provide the log for download', function(done) { + const log = Client.getOutputFile(this.body, 'pdf') + return request.get(log.url, (error, res, body) => { + res.statusCode.should.equal(200) + return done() + }) + }) +}) diff --git a/services/clsi/test/acceptance/js/SynctexTests.js b/services/clsi/test/acceptance/js/SynctexTests.js index d8879ebc0f..4860c6040c 100644 --- a/services/clsi/test/acceptance/js/SynctexTests.js +++ b/services/clsi/test/acceptance/js/SynctexTests.js @@ -9,53 +9,83 @@ * 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"); -require("chai").should(); -const { expect } = require("chai"); -const ClsiApp = require("./helpers/ClsiApp"); -const crypto = require("crypto"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const { expect } = require('chai') +const ClsiApp = require('./helpers/ClsiApp') +const crypto = require('crypto') -describe("Syncing", function() { - before(function(done) { - const content = `\ +describe('Syncing', function() { + before(function(done) { + const content = `\ \\documentclass{article} \\begin{document} Hello world \\end{document}\ -`; - this.request = { - resources: [{ - path: "main.tex", - content - } - ] - }; - this.project_id = Client.randomId(); - return ClsiApp.ensureRunning(() => { - return Client.compile(this.project_id, this.request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); }); - }); - }); +` + this.request = { + resources: [ + { + path: 'main.tex', + content + } + ] + } + this.project_id = Client.randomId() + return ClsiApp.ensureRunning(() => { + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) + }) - describe("from code to pdf", function() { return it("should return the correct location", function(done) { - return Client.syncFromCode(this.project_id, "main.tex", 3, 5, (error, pdfPositions) => { - if (error != null) { throw error; } - expect(pdfPositions).to.deep.equal({ - pdf: [ { page: 1, h: 133.77, v: 134.76, height: 6.92, width: 343.71 } ] - }); - return done(); - }); - }); } - ); + describe('from code to pdf', function() { + return it('should return the correct location', function(done) { + return Client.syncFromCode( + this.project_id, + 'main.tex', + 3, + 5, + (error, pdfPositions) => { + if (error != null) { + throw error + } + expect(pdfPositions).to.deep.equal({ + pdf: [ + { page: 1, h: 133.77, v: 134.76, height: 6.92, width: 343.71 } + ] + }) + return done() + } + ) + }) + }) - return describe("from pdf to code", function() { return it("should return the correct location", function(done) { - return Client.syncFromPdf(this.project_id, 1, 100, 200, (error, codePositions) => { - if (error != null) { throw error; } - expect(codePositions).to.deep.equal({ - code: [ { file: 'main.tex', line: 3, column: -1 } ] - }); - return done(); - }); - }); } - ); -}); + return describe('from pdf to code', function() { + return it('should return the correct location', function(done) { + return Client.syncFromPdf( + this.project_id, + 1, + 100, + 200, + (error, codePositions) => { + if (error != null) { + throw error + } + expect(codePositions).to.deep.equal({ + code: [{ file: 'main.tex', line: 3, column: -1 }] + }) + return done() + } + ) + }) + }) +}) diff --git a/services/clsi/test/acceptance/js/TimeoutTests.js b/services/clsi/test/acceptance/js/TimeoutTests.js index 7f8f84835a..f6812e8c6e 100644 --- a/services/clsi/test/acceptance/js/TimeoutTests.js +++ b/services/clsi/test/acceptance/js/TimeoutTests.js @@ -8,46 +8,55 @@ * DS102: Remove unnecessary code created because of implicit returns * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ -const Client = require("./helpers/Client"); -const request = require("request"); -require("chai").should(); -const ClsiApp = require("./helpers/ClsiApp"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const ClsiApp = require('./helpers/ClsiApp') - -describe("Timed out compile", function() { - before(function(done) { - this.request = { - options: { - timeout: 10 - }, // seconds - resources: [{ - path: "main.tex", - content: `\ +describe('Timed out compile', function() { + before(function(done) { + this.request = { + options: { + timeout: 10 + }, // seconds + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\begin{document} \\def\\x{Hello!\\par\\x} \\x \\end{document}\ ` - } - ] - }; - this.project_id = Client.randomId(); - return ClsiApp.ensureRunning(() => { - return Client.compile(this.project_id, this.request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); }); - }); - }); + } + ] + } + this.project_id = Client.randomId() + return ClsiApp.ensureRunning(() => { + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) + }) - it("should return a timeout error", function() { - return this.body.compile.error.should.equal("container timed out"); - }); + it('should return a timeout error', function() { + return this.body.compile.error.should.equal('container timed out') + }) - it("should return a timedout status", function() { - return this.body.compile.status.should.equal("timedout"); - }); + it('should return a timedout status', function() { + return this.body.compile.status.should.equal('timedout') + }) - return it("should return the log output file name", function() { - const outputFilePaths = this.body.compile.outputFiles.map(x => x.path); - return outputFilePaths.should.include('output.log'); - }); -}); + return it('should return the log output file name', function() { + const outputFilePaths = this.body.compile.outputFiles.map(x => x.path) + return outputFilePaths.should.include('output.log') + }) +}) diff --git a/services/clsi/test/acceptance/js/UrlCachingTests.js b/services/clsi/test/acceptance/js/UrlCachingTests.js index 7bb0a2055e..4d6249784c 100644 --- a/services/clsi/test/acceptance/js/UrlCachingTests.js +++ b/services/clsi/test/acceptance/js/UrlCachingTests.js @@ -10,277 +10,364 @@ * 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"); -require("chai").should(); -const sinon = require("sinon"); -const ClsiApp = require("./helpers/ClsiApp"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const sinon = require('sinon') +const ClsiApp = require('./helpers/ClsiApp') -const host = "localhost"; +const host = 'localhost' const Server = { - run() { - const express = require("express"); - const app = express(); + run() { + const express = require('express') + const app = express() - const staticServer = express.static(__dirname + "/../fixtures/"); - app.get("/:random_id/*", (req, res, next) => { - this.getFile(req.url); - req.url = `/${req.params[0]}`; - return staticServer(req, res, next); - }); + const staticServer = express.static(__dirname + '/../fixtures/') + app.get('/:random_id/*', (req, res, next) => { + this.getFile(req.url) + req.url = `/${req.params[0]}` + return staticServer(req, res, next) + }) - return app.listen(31415, host); - }, + return app.listen(31415, host) + }, - getFile() {}, + getFile() {}, - randomId() { - return Math.random().toString(16).slice(2); - } -}; + randomId() { + return Math.random() + .toString(16) + .slice(2) + } +} -Server.run(); +Server.run() -describe("Url Caching", function() { - describe("Downloading an image for the first time", function() { - before(function(done) { - this.project_id = Client.randomId(); - this.file = `${Server.randomId()}/lion.png`; - this.request = { - resources: [{ - path: "main.tex", - content: `\ +describe('Url Caching', function() { + describe('Downloading an image for the first time', function() { + before(function(done) { + this.project_id = Client.randomId() + this.file = `${Server.randomId()}/lion.png` + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\usepackage{graphicx} \\begin{document} \\includegraphics{lion.png} \\end{document}\ ` - }, { - path: "lion.png", - url: `http://${host}:31415/${this.file}` - }] - }; + }, + { + path: 'lion.png', + url: `http://${host}:31415/${this.file}` + } + ] + } - sinon.spy(Server, "getFile"); - return ClsiApp.ensureRunning(() => { - return Client.compile(this.project_id, this.request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); }); - }); - }); + sinon.spy(Server, 'getFile') + return ClsiApp.ensureRunning(() => { + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) + }) - afterEach(function() { return Server.getFile.restore(); }); + afterEach(function() { + return Server.getFile.restore() + }) - return it("should download the image", function() { - return Server.getFile - .calledWith(`/${this.file}`) - .should.equal(true); - }); - }); - - describe("When an image is in the cache and the last modified date is unchanged", function() { - before(function(done) { - this.project_id = Client.randomId(); - this.file = `${Server.randomId()}/lion.png`; - this.request = { - resources: [{ - path: "main.tex", - content: `\ + return it('should download the image', function() { + return Server.getFile.calledWith(`/${this.file}`).should.equal(true) + }) + }) + + describe('When an image is in the cache and the last modified date is unchanged', function() { + before(function(done) { + this.project_id = Client.randomId() + this.file = `${Server.randomId()}/lion.png` + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\usepackage{graphicx} \\begin{document} \\includegraphics{lion.png} \\end{document}\ ` - }, (this.image_resource = { - path: "lion.png", - url: `http://${host}:31415/${this.file}`, - modified: Date.now() - })] - }; + }, + (this.image_resource = { + path: 'lion.png', + url: `http://${host}:31415/${this.file}`, + modified: Date.now() + }) + ] + } - return Client.compile(this.project_id, this.request, (error, res, body) => { - this.error = error; - this.res = res; - this.body = body; - sinon.spy(Server, "getFile"); - return Client.compile(this.project_id, this.request, (error1, res1, body1) => { - this.error = error1; - this.res = res1; - this.body = body1; - return done(); - }); - }); - }); + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + sinon.spy(Server, 'getFile') + return Client.compile( + this.project_id, + this.request, + (error1, res1, body1) => { + this.error = error1 + this.res = res1 + this.body = body1 + return done() + } + ) + } + ) + }) - after(function() { return Server.getFile.restore(); }); + after(function() { + return Server.getFile.restore() + }) - return it("should not download the image again", function() { return Server.getFile.called.should.equal(false); }); - }); + return it('should not download the image again', function() { + return Server.getFile.called.should.equal(false) + }) + }) - describe("When an image is in the cache and the last modified date is advanced", function() { - before(function(done) { - this.project_id = Client.randomId(); - this.file = `${Server.randomId()}/lion.png`; - this.request = { - resources: [{ - path: "main.tex", - content: `\ + describe('When an image is in the cache and the last modified date is advanced', function() { + before(function(done) { + this.project_id = Client.randomId() + this.file = `${Server.randomId()}/lion.png` + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\usepackage{graphicx} \\begin{document} \\includegraphics{lion.png} \\end{document}\ ` - }, (this.image_resource = { - path: "lion.png", - url: `http://${host}:31415/${this.file}`, - modified: (this.last_modified = Date.now()) - })] - }; + }, + (this.image_resource = { + path: 'lion.png', + url: `http://${host}:31415/${this.file}`, + modified: (this.last_modified = Date.now()) + }) + ] + } - return Client.compile(this.project_id, this.request, (error, res, body) => { - this.error = error; - this.res = res; - this.body = body; - sinon.spy(Server, "getFile"); - this.image_resource.modified = new Date(this.last_modified + 3000); - return Client.compile(this.project_id, this.request, (error1, res1, body1) => { - this.error = error1; - this.res = res1; - this.body = body1; - return done(); - }); - }); - }); + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + sinon.spy(Server, 'getFile') + this.image_resource.modified = new Date(this.last_modified + 3000) + return Client.compile( + this.project_id, + this.request, + (error1, res1, body1) => { + this.error = error1 + this.res = res1 + this.body = body1 + return done() + } + ) + } + ) + }) - afterEach(function() { return Server.getFile.restore(); }); + afterEach(function() { + return Server.getFile.restore() + }) - return it("should download the image again", function() { return Server.getFile.called.should.equal(true); }); - }); + return it('should download the image again', function() { + return Server.getFile.called.should.equal(true) + }) + }) - describe("When an image is in the cache and the last modified date is further in the past", function() { - before(function(done) { - this.project_id = Client.randomId(); - this.file = `${Server.randomId()}/lion.png`; - this.request = { - resources: [{ - path: "main.tex", - content: `\ + describe('When an image is in the cache and the last modified date is further in the past', function() { + before(function(done) { + this.project_id = Client.randomId() + this.file = `${Server.randomId()}/lion.png` + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\usepackage{graphicx} \\begin{document} \\includegraphics{lion.png} \\end{document}\ ` - }, (this.image_resource = { - path: "lion.png", - url: `http://${host}:31415/${this.file}`, - modified: (this.last_modified = Date.now()) - })] - }; + }, + (this.image_resource = { + path: 'lion.png', + url: `http://${host}:31415/${this.file}`, + modified: (this.last_modified = Date.now()) + }) + ] + } - return Client.compile(this.project_id, this.request, (error, res, body) => { - this.error = error; - this.res = res; - this.body = body; - sinon.spy(Server, "getFile"); - this.image_resource.modified = new Date(this.last_modified - 3000); - return Client.compile(this.project_id, this.request, (error1, res1, body1) => { - this.error = error1; - this.res = res1; - this.body = body1; - return done(); - }); - }); - }); + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + sinon.spy(Server, 'getFile') + this.image_resource.modified = new Date(this.last_modified - 3000) + return Client.compile( + this.project_id, + this.request, + (error1, res1, body1) => { + this.error = error1 + this.res = res1 + this.body = body1 + return done() + } + ) + } + ) + }) - afterEach(function() { return Server.getFile.restore(); }); + afterEach(function() { + return Server.getFile.restore() + }) - return it("should not download the image again", function() { return Server.getFile.called.should.equal(false); }); - }); + return it('should not download the image again', function() { + return Server.getFile.called.should.equal(false) + }) + }) - describe("When an image is in the cache and the last modified date is not specified", function() { - before(function(done) { - this.project_id = Client.randomId(); - this.file = `${Server.randomId()}/lion.png`; - this.request = { - resources: [{ - path: "main.tex", - content: `\ + describe('When an image is in the cache and the last modified date is not specified', function() { + before(function(done) { + this.project_id = Client.randomId() + this.file = `${Server.randomId()}/lion.png` + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\usepackage{graphicx} \\begin{document} \\includegraphics{lion.png} \\end{document}\ ` - }, (this.image_resource = { - path: "lion.png", - url: `http://${host}:31415/${this.file}`, - modified: (this.last_modified = Date.now()) - })] - }; + }, + (this.image_resource = { + path: 'lion.png', + url: `http://${host}:31415/${this.file}`, + modified: (this.last_modified = Date.now()) + }) + ] + } - return Client.compile(this.project_id, this.request, (error, res, body) => { - this.error = error; - this.res = res; - this.body = body; - sinon.spy(Server, "getFile"); - delete this.image_resource.modified; - return Client.compile(this.project_id, this.request, (error1, res1, body1) => { - this.error = error1; - this.res = res1; - this.body = body1; - return done(); - }); - }); - }); + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + sinon.spy(Server, 'getFile') + delete this.image_resource.modified + return Client.compile( + this.project_id, + this.request, + (error1, res1, body1) => { + this.error = error1 + this.res = res1 + this.body = body1 + return done() + } + ) + } + ) + }) - afterEach(function() { return Server.getFile.restore(); }); + afterEach(function() { + return Server.getFile.restore() + }) - return it("should download the image again", function() { return Server.getFile.called.should.equal(true); }); - }); - - return describe("After clearing the cache", function() { - before(function(done) { - this.project_id = Client.randomId(); - this.file = `${Server.randomId()}/lion.png`; - this.request = { - resources: [{ - path: "main.tex", - content: `\ + return it('should download the image again', function() { + return Server.getFile.called.should.equal(true) + }) + }) + + return describe('After clearing the cache', function() { + before(function(done) { + this.project_id = Client.randomId() + this.file = `${Server.randomId()}/lion.png` + this.request = { + resources: [ + { + path: 'main.tex', + content: `\ \\documentclass{article} \\usepackage{graphicx} \\begin{document} \\includegraphics{lion.png} \\end{document}\ ` - }, (this.image_resource = { - path: "lion.png", - url: `http://${host}:31415/${this.file}`, - modified: (this.last_modified = Date.now()) - })] - }; + }, + (this.image_resource = { + path: 'lion.png', + url: `http://${host}:31415/${this.file}`, + modified: (this.last_modified = Date.now()) + }) + ] + } - return Client.compile(this.project_id, this.request, error => { - if (error != null) { throw error; } - return Client.clearCache(this.project_id, (error, res, body) => { - if (error != null) { throw error; } - sinon.spy(Server, "getFile"); - return Client.compile(this.project_id, this.request, (error1, res1, body1) => { - this.error = error1; - this.res = res1; - this.body = body1; - return done(); - }); - }); - }); - }); + return Client.compile(this.project_id, this.request, error => { + if (error != null) { + throw error + } + return Client.clearCache(this.project_id, (error, res, body) => { + if (error != null) { + throw error + } + sinon.spy(Server, 'getFile') + return Client.compile( + this.project_id, + this.request, + (error1, res1, body1) => { + this.error = error1 + this.res = res1 + this.body = body1 + return done() + } + ) + }) + }) + }) - afterEach(function() { return Server.getFile.restore(); }); + afterEach(function() { + return Server.getFile.restore() + }) - return it("should download the image again", function() { return Server.getFile.called.should.equal(true); }); - }); -}); - - + return it('should download the image again', function() { + return Server.getFile.called.should.equal(true) + }) + }) +}) diff --git a/services/clsi/test/acceptance/js/WordcountTests.js b/services/clsi/test/acceptance/js/WordcountTests.js index 2f81e1398d..8721857030 100644 --- a/services/clsi/test/acceptance/js/WordcountTests.js +++ b/services/clsi/test/acceptance/js/WordcountTests.js @@ -9,48 +9,64 @@ * 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"); -require("chai").should(); -const { expect } = require("chai"); -const path = require("path"); -const fs = require("fs"); -const ClsiApp = require("./helpers/ClsiApp"); +const Client = require('./helpers/Client') +const request = require('request') +require('chai').should() +const { expect } = require('chai') +const path = require('path') +const fs = require('fs') +const ClsiApp = require('./helpers/ClsiApp') -describe("Syncing", function() { - before(function(done) { - this.request = { - resources: [{ - path: "main.tex", - content: fs.readFileSync(path.join(__dirname,"../fixtures/naugty_strings.txt"),"utf-8") - } - ] - }; - this.project_id = Client.randomId(); - return ClsiApp.ensureRunning(() => { - return Client.compile(this.project_id, this.request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); }); - }); - }); +describe('Syncing', function() { + before(function(done) { + this.request = { + resources: [ + { + path: 'main.tex', + content: fs.readFileSync( + path.join(__dirname, '../fixtures/naugty_strings.txt'), + 'utf-8' + ) + } + ] + } + this.project_id = Client.randomId() + return ClsiApp.ensureRunning(() => { + return Client.compile( + this.project_id, + this.request, + (error, res, body) => { + this.error = error + this.res = res + this.body = body + return done() + } + ) + }) + }) - return describe("wordcount file", function() { return it("should return wordcount info", function(done) { - return Client.wordcount(this.project_id, "main.tex", (error, result) => { - if (error != null) { throw error; } - expect(result).to.deep.equal({ - texcount: { - encode: "utf8", - textWords: 2281, - headWords: 2, - outside: 0, - headers: 2, - elements: 0, - mathInline: 6, - mathDisplay: 0, - errors: 0, - messages: "" - } - }); - return done(); - }); - }); } - ); -}); + return describe('wordcount file', function() { + return it('should return wordcount info', function(done) { + return Client.wordcount(this.project_id, 'main.tex', (error, result) => { + if (error != null) { + throw error + } + expect(result).to.deep.equal({ + texcount: { + encode: 'utf8', + textWords: 2281, + headWords: 2, + outside: 0, + headers: 2, + elements: 0, + mathInline: 6, + mathDisplay: 0, + errors: 0, + messages: '' + } + }) + return done() + }) + }) + }) +}) diff --git a/services/clsi/test/acceptance/js/helpers/Client.js b/services/clsi/test/acceptance/js/helpers/Client.js index 50e75d6148..9f430e3535 100644 --- a/services/clsi/test/acceptance/js/helpers/Client.js +++ b/services/clsi/test/acceptance/js/helpers/Client.js @@ -12,143 +12,197 @@ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ -let Client; -const request = require("request"); -const fs = require("fs"); -const Settings = require("settings-sharelatex"); +let Client +const request = require('request') +const fs = require('fs') +const Settings = require('settings-sharelatex') -const host = "localhost"; +const host = 'localhost' -module.exports = (Client = { - host: Settings.apis.clsi.url, +module.exports = Client = { + host: Settings.apis.clsi.url, - randomId() { - return Math.random().toString(16).slice(2); - }, + randomId() { + return Math.random() + .toString(16) + .slice(2) + }, - compile(project_id, data, callback) { - if (callback == null) { callback = function(error, res, body) {}; } - return request.post({ - url: `${this.host}/project/${project_id}/compile`, - json: { - compile: data - } - }, callback); - }, + compile(project_id, data, callback) { + if (callback == null) { + callback = function(error, res, body) {} + } + return request.post( + { + url: `${this.host}/project/${project_id}/compile`, + json: { + compile: data + } + }, + callback + ) + }, - clearCache(project_id, callback) { - if (callback == null) { callback = function(error, res, body) {}; } - return request.del(`${this.host}/project/${project_id}`, callback); - }, + clearCache(project_id, callback) { + if (callback == null) { + callback = function(error, res, body) {} + } + return request.del(`${this.host}/project/${project_id}`, callback) + }, - getOutputFile(response, type) { - for (const file of Array.from(response.compile.outputFiles)) { - if ((file.type === type) && file.url.match(`output.${type}`)) { - return file; - } - } - return null; - }, + getOutputFile(response, type) { + for (const file of Array.from(response.compile.outputFiles)) { + if (file.type === type && file.url.match(`output.${type}`)) { + return file + } + } + return null + }, - runServer(port, directory) { - const express = require("express"); - const app = express(); - app.use(express.static(directory)); - console.log("starting test server on", port, host); - return app.listen(port, host).on("error", (error) => { - console.error("error starting server:", error.message); - return process.exit(1); - }); - }, + runServer(port, directory) { + const express = require('express') + const app = express() + app.use(express.static(directory)) + console.log('starting test server on', port, host) + return app.listen(port, host).on('error', error => { + console.error('error starting server:', error.message) + return process.exit(1) + }) + }, + syncFromCode(project_id, file, line, column, callback) { + if (callback == null) { + callback = function(error, pdfPositions) {} + } + return request.get( + { + url: `${this.host}/project/${project_id}/sync/code`, + qs: { + file, + line, + column + } + }, + (error, response, body) => { + if (error != null) { + return callback(error) + } + return callback(null, JSON.parse(body)) + } + ) + }, - syncFromCode(project_id, file, line, column, callback) { - if (callback == null) { callback = function(error, pdfPositions) {}; } - return request.get({ - url: `${this.host}/project/${project_id}/sync/code`, - qs: { - file, - line, - column - } - }, (error, response, body) => { - if (error != null) { return callback(error); } - return callback(null, JSON.parse(body)); - }); - }, + syncFromPdf(project_id, page, h, v, callback) { + if (callback == null) { + callback = function(error, pdfPositions) {} + } + return request.get( + { + url: `${this.host}/project/${project_id}/sync/pdf`, + qs: { + page, + h, + v + } + }, + (error, response, body) => { + if (error != null) { + return callback(error) + } + return callback(null, JSON.parse(body)) + } + ) + }, - syncFromPdf(project_id, page, h, v, callback) { - if (callback == null) { callback = function(error, pdfPositions) {}; } - return request.get({ - url: `${this.host}/project/${project_id}/sync/pdf`, - qs: { - page, - h, v - } - }, (error, response, body) => { - if (error != null) { return callback(error); } - return callback(null, JSON.parse(body)); - }); - }, + compileDirectory(project_id, baseDirectory, directory, serverPort, callback) { + if (callback == null) { + callback = function(error, res, body) {} + } + const resources = [] + let entities = fs.readdirSync(`${baseDirectory}/${directory}`) + let rootResourcePath = 'main.tex' + while (entities.length > 0) { + var entity = entities.pop() + const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`) + if (stat.isDirectory()) { + entities = entities.concat( + fs + .readdirSync(`${baseDirectory}/${directory}/${entity}`) + .map(subEntity => { + if (subEntity === 'main.tex') { + rootResourcePath = `${entity}/${subEntity}` + } + return `${entity}/${subEntity}` + }) + ) + } else if (stat.isFile() && entity !== 'output.pdf') { + const extension = entity.split('.').pop() + if ( + [ + 'tex', + 'bib', + 'cls', + 'sty', + 'pdf_tex', + 'Rtex', + 'ist', + 'md', + 'Rmd' + ].indexOf(extension) > -1 + ) { + resources.push({ + path: entity, + content: fs + .readFileSync(`${baseDirectory}/${directory}/${entity}`) + .toString() + }) + } else if ( + ['eps', 'ttf', 'png', 'jpg', 'pdf', 'jpeg'].indexOf(extension) > -1 + ) { + resources.push({ + path: entity, + url: `http://${host}:${serverPort}/${directory}/${entity}`, + modified: stat.mtime + }) + } + } + } - compileDirectory(project_id, baseDirectory, directory, serverPort, callback) { - if (callback == null) { callback = function(error, res, body) {}; } - const resources = []; - let entities = fs.readdirSync(`${baseDirectory}/${directory}`); - let rootResourcePath = "main.tex"; - while (entities.length > 0) { - var entity = entities.pop(); - const stat = fs.statSync(`${baseDirectory}/${directory}/${entity}`); - if (stat.isDirectory()) { - entities = entities.concat(fs.readdirSync(`${baseDirectory}/${directory}/${entity}`).map((subEntity) => { - if (subEntity === "main.tex") { - rootResourcePath = `${entity}/${subEntity}`; - } - return `${entity}/${subEntity}`; - }) - ); - } else if (stat.isFile() && (entity !== "output.pdf")) { - const extension = entity.split(".").pop(); - if (["tex", "bib", "cls", "sty", "pdf_tex", "Rtex", "ist", "md", "Rmd"].indexOf(extension) > -1) { - resources.push({ - path: entity, - content: fs.readFileSync(`${baseDirectory}/${directory}/${entity}`).toString() - }); - } else if (["eps", "ttf", "png", "jpg", "pdf", "jpeg"].indexOf(extension) > -1) { - resources.push({ - path: entity, - url: `http://${host}:${serverPort}/${directory}/${entity}`, - modified: stat.mtime - }); - } - } - } + return fs.readFile( + `${baseDirectory}/${directory}/options.json`, + (error, body) => { + const req = { + resources, + rootResourcePath + } - return fs.readFile(`${baseDirectory}/${directory}/options.json`, (error, body) => { - const req = { - resources, - rootResourcePath - }; + if (error == null) { + body = JSON.parse(body) + req.options = body + } - if ((error == null)) { - body = JSON.parse(body); - req.options = body; - } + return this.compile(project_id, req, callback) + } + ) + }, - return this.compile(project_id, req, callback); - }); - }, - - wordcount(project_id, file, callback) { - if (callback == null) { callback = function(error, pdfPositions) {}; } - return request.get({ - url: `${this.host}/project/${project_id}/wordcount`, - qs: { - file - } - }, (error, response, body) => { - if (error != null) { return callback(error); } - return callback(null, JSON.parse(body)); - }); - } -}); + wordcount(project_id, file, callback) { + if (callback == null) { + callback = function(error, pdfPositions) {} + } + return request.get( + { + url: `${this.host}/project/${project_id}/wordcount`, + qs: { + file + } + }, + (error, response, body) => { + if (error != null) { + return callback(error) + } + return callback(null, JSON.parse(body)) + } + ) + } +} diff --git a/services/clsi/test/acceptance/js/helpers/ClsiApp.js b/services/clsi/test/acceptance/js/helpers/ClsiApp.js index bd3222df6c..f8038460cf 100644 --- a/services/clsi/test/acceptance/js/helpers/ClsiApp.js +++ b/services/clsi/test/acceptance/js/helpers/ClsiApp.js @@ -12,40 +12,53 @@ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ -const app = require('../../../../app'); -require("logger-sharelatex").logger.level("info"); -const logger = require("logger-sharelatex"); -const Settings = require("settings-sharelatex"); +const app = require('../../../../app') +require('logger-sharelatex').logger.level('info') +const logger = require('logger-sharelatex') +const Settings = require('settings-sharelatex') module.exports = { - running: false, - initing: false, - callbacks: [], - ensureRunning(callback) { - if (callback == null) { callback = function(error) {}; } - if (this.running) { - return callback(); - } else if (this.initing) { - return this.callbacks.push(callback); - } else { - this.initing = true; - this.callbacks.push(callback); - return app.listen(__guard__(Settings.internal != null ? Settings.internal.clsi : undefined, x => x.port), "localhost", error => { - if (error != null) { throw error; } - this.running = true; - logger.log("clsi running in dev mode"); + running: false, + initing: false, + callbacks: [], + ensureRunning(callback) { + if (callback == null) { + callback = function(error) {} + } + if (this.running) { + return callback() + } else if (this.initing) { + return this.callbacks.push(callback) + } else { + this.initing = true + this.callbacks.push(callback) + return app.listen( + __guard__( + Settings.internal != null ? Settings.internal.clsi : undefined, + x => x.port + ), + 'localhost', + error => { + if (error != null) { + throw error + } + this.running = true + logger.log('clsi running in dev mode') - return (() => { - const result = []; - for (callback of Array.from(this.callbacks)) { - result.push(callback()); - } - return result; - })(); - }); - } - } -}; + return (() => { + const result = [] + for (callback of Array.from(this.callbacks)) { + result.push(callback()) + } + return result + })() + } + ) + } + } +} function __guard__(value, transform) { - return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined; -} \ No newline at end of file + return typeof value !== 'undefined' && value !== null + ? transform(value) + : undefined +}