decaffeinate: Convert BrokenLatexFileTests.coffee and 9 other files to JS

This commit is contained in:
decaffeinate
2020-02-19 12:16:00 +01:00
committed by mserranom
parent a2a3fddd54
commit 955749a7c4
10 changed files with 883 additions and 606 deletions

View File

@@ -1,48 +1,70 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS102: Remove unnecessary code created because of implicit returns
ClsiApp = require "./helpers/ClsiApp" * 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");
describe "Broken LaTeX file", -> describe("Broken LaTeX file", function() {
before (done)-> before(function(done){
@broken_request = this.broken_request = {
resources: [ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{articl % :( \\documentclass{articl % :(
\\begin{documen % :( \\begin{documen % :(
Broken Broken
\\end{documen % :( \\end{documen % :(\
''' `
}
] ]
@correct_request = };
resources: [ this.correct_request = {
path: "main.tex" resources: [{
content: ''' path: "main.tex",
\\documentclass{article} content: `\
\\begin{document} \\documentclass{article}
Hello world \\begin{document}
\\end{document} Hello world
''' \\end{document}\
`
}
] ]
ClsiApp.ensureRunning done };
return ClsiApp.ensureRunning(done);
});
describe "on first run", -> describe("on first run", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
Client.compile @project_id, @broken_request, (@error, @res, @body) => done() return Client.compile(this.project_id, this.broken_request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); });
});
it "should return a failure status", -> return it("should return a failure status", function() {
@body.compile.status.should.equal "failure" return this.body.compile.status.should.equal("failure");
});
});
describe "on second run", -> return describe("on second run", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
Client.compile @project_id, @correct_request, () => return Client.compile(this.project_id, this.correct_request, () => {
Client.compile @project_id, @broken_request, (@error, @res, @body) => return Client.compile(this.project_id, this.broken_request, (error, res, body) => {
done() this.error = error;
this.res = res;
this.body = body;
return done();
});
});
});
it "should return a failure status", -> return it("should return a failure status", function() {
@body.compile.status.should.equal "failure" return this.body.compile.status.should.equal("failure");
});
});
});

View File

@@ -1,36 +1,55 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS102: Remove unnecessary code created because of implicit returns
ClsiApp = require "./helpers/ClsiApp" * 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");
describe "Deleting Old Files", -> describe("Deleting Old Files", function() {
before (done)-> before(function(done){
@request = this.request = {
resources: [ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\begin{document} \\begin{document}
Hello world Hello world
\\end{document} \\end{document}\
''' `
}
] ]
ClsiApp.ensureRunning done };
return ClsiApp.ensureRunning(done);
});
describe "on first run", -> return describe("on first run", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
Client.compile @project_id, @request, (@error, @res, @body) => done() 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", -> it("should return a success status", function() {
@body.compile.status.should.equal "success" return this.body.compile.status.should.equal("success");
});
describe "after file has been deleted", -> return describe("after file has been deleted", function() {
before (done) -> before(function(done) {
@request.resources = [] this.request.resources = [];
Client.compile @project_id, @request, (@error, @res, @body) => return Client.compile(this.project_id, this.request, (error, res, body) => {
done() this.error = error;
this.res = res;
this.body = body;
return done();
});
});
it "should return a failure status", -> return it("should return a failure status", function() {
@body.compile.status.should.equal "failure" return this.body.compile.status.should.equal("failure");
});
});
});
});

View File

@@ -1,129 +1,182 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS101: Remove unnecessary use of Array.from
fs = require "fs" * DS102: Remove unnecessary code created because of implicit returns
ChildProcess = require "child_process" * DS103: Rewrite code to no longer use __guard__
ClsiApp = require "./helpers/ClsiApp" * DS207: Consider shorter variations of null checks
logger = require("logger-sharelatex") * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
Path = require("path") */
fixturePath = (path) -> Path.normalize(__dirname + "/../fixtures/" + path) const Client = require("./helpers/Client");
process = require "process" const request = require("request");
console.log process.pid, process.ppid, process.getuid(),process.getgroups(), "PID" require("chai").should();
try const fs = require("fs");
console.log "creating tmp directory", fixturePath("tmp") const ChildProcess = require("child_process");
fs.mkdirSync(fixturePath("tmp")) const ClsiApp = require("./helpers/ClsiApp");
catch err const logger = require("logger-sharelatex");
console.log err, fixturePath("tmp"), "unable to create fixture tmp path" 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"));
} catch (error) {
const err = error;
console.log(err, fixturePath("tmp"), "unable to create fixture tmp path");
}
MOCHA_LATEX_TIMEOUT = 60 * 1000 const MOCHA_LATEX_TIMEOUT = 60 * 1000;
convertToPng = (pdfPath, pngPath, callback = (error) ->) -> const convertToPng = function(pdfPath, pngPath, callback) {
command = "convert #{fixturePath(pdfPath)} #{fixturePath(pngPath)}" if (callback == null) { callback = function(error) {}; }
console.log "COMMAND" const command = `convert ${fixturePath(pdfPath)} ${fixturePath(pngPath)}`;
console.log command console.log("COMMAND");
convert = ChildProcess.exec command console.log(command);
stdout = "" const convert = ChildProcess.exec(command);
convert.stdout.on "data", (chunk) -> console.log "STDOUT", chunk.toString() const stdout = "";
convert.stderr.on "data", (chunk) -> console.log "STDERR", chunk.toString() convert.stdout.on("data", chunk => console.log("STDOUT", chunk.toString()));
convert.on "exit", () -> convert.stderr.on("data", chunk => console.log("STDERR", chunk.toString()));
callback() return convert.on("exit", () => callback());
};
compare = (originalPath, generatedPath, callback = (error, same) ->) -> const compare = function(originalPath, generatedPath, callback) {
diff_file = "#{fixturePath(generatedPath)}-diff.png" if (callback == null) { callback = function(error, same) {}; }
proc = ChildProcess.exec "compare -metric mae #{fixturePath(originalPath)} #{fixturePath(generatedPath)} #{diff_file}" const diff_file = `${fixturePath(generatedPath)}-diff.png`;
stderr = "" const proc = ChildProcess.exec(`compare -metric mae ${fixturePath(originalPath)} ${fixturePath(generatedPath)} ${diff_file}`);
proc.stderr.on "data", (chunk) -> stderr += chunk let stderr = "";
proc.on "exit", () -> proc.stderr.on("data", chunk => stderr += chunk);
if stderr.trim() == "0 (0)" return proc.on("exit", function() {
# remove output diff if test matches expected image if (stderr.trim() === "0 (0)") {
fs.unlink diff_file, (err) -> // remove output diff if test matches expected image
if err fs.unlink(diff_file, function(err) {
throw err if (err) {
callback null, true throw err;
else }
console.log "compare result", stderr });
callback null, false return callback(null, true);
} else {
console.log("compare result", stderr);
return callback(null, false);
}
});
};
checkPdfInfo = (pdfPath, callback = (error, output) ->) -> const checkPdfInfo = function(pdfPath, callback) {
proc = ChildProcess.exec "pdfinfo #{fixturePath(pdfPath)}" if (callback == null) { callback = function(error, output) {}; }
stdout = "" const proc = ChildProcess.exec(`pdfinfo ${fixturePath(pdfPath)}`);
proc.stdout.on "data", (chunk) -> stdout += chunk let stdout = "";
proc.stderr.on "data", (chunk) -> console.log "STDERR", chunk.toString() proc.stdout.on("data", chunk => stdout += chunk);
proc.on "exit", () -> proc.stderr.on("data", chunk => console.log("STDERR", chunk.toString()));
if stdout.match(/Optimized:\s+yes/) return proc.on("exit", function() {
callback null, true if (stdout.match(/Optimized:\s+yes/)) {
else return callback(null, true);
callback null, false } else {
return callback(null, false);
}
});
};
compareMultiplePages = (project_id, callback = (error) ->) -> const compareMultiplePages = function(project_id, callback) {
compareNext = (page_no, callback) -> if (callback == null) { callback = function(error) {}; }
path = "tmp/#{project_id}-source-#{page_no}.png" var compareNext = function(page_no, callback) {
fs.stat fixturePath(path), (error, stat) -> const path = `tmp/${project_id}-source-${page_no}.png`;
if error? return fs.stat(fixturePath(path), function(error, stat) {
callback() if (error != null) {
else return callback();
compare "tmp/#{project_id}-source-#{page_no}.png", "tmp/#{project_id}-generated-#{page_no}.png", (error, same) => } else {
throw error if error? return compare(`tmp/${project_id}-source-${page_no}.png`, `tmp/${project_id}-generated-${page_no}.png`, (error, same) => {
same.should.equal true if (error != null) { throw error; }
compareNext page_no + 1, callback same.should.equal(true);
compareNext 0, callback return compareNext(page_no + 1, callback);
});
}
});
};
return compareNext(0, callback);
};
comparePdf = (project_id, example_dir, callback = (error) ->) -> const comparePdf = function(project_id, example_dir, callback) {
console.log "CONVERT" if (callback == null) { callback = function(error) {}; }
console.log "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png" console.log("CONVERT");
convertToPng "tmp/#{project_id}.pdf", "tmp/#{project_id}-generated.png", (error) => console.log(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`);
throw error if error? return convertToPng(`tmp/${project_id}.pdf`, `tmp/${project_id}-generated.png`, error => {
convertToPng "examples/#{example_dir}/output.pdf", "tmp/#{project_id}-source.png", (error) => if (error != null) { throw error; }
throw error if error? return convertToPng(`examples/${example_dir}/output.pdf`, `tmp/${project_id}-source.png`, error => {
fs.stat fixturePath("tmp/#{project_id}-source-0.png"), (error, stat) => if (error != null) { throw error; }
if error? return fs.stat(fixturePath(`tmp/${project_id}-source-0.png`), (error, stat) => {
compare "tmp/#{project_id}-source.png", "tmp/#{project_id}-generated.png", (error, same) => if (error != null) {
throw error if error? return compare(`tmp/${project_id}-source.png`, `tmp/${project_id}-generated.png`, (error, same) => {
same.should.equal true if (error != null) { throw error; }
callback() same.should.equal(true);
else return callback();
compareMultiplePages project_id, (error) -> });
throw error if error? } else {
callback() return compareMultiplePages(project_id, function(error) {
if (error != null) { throw error; }
return callback();
});
}
});
});
});
};
downloadAndComparePdf = (project_id, example_dir, url, callback = (error) ->) -> const downloadAndComparePdf = function(project_id, example_dir, url, callback) {
writeStream = fs.createWriteStream(fixturePath("tmp/#{project_id}.pdf")) if (callback == null) { callback = function(error) {}; }
request.get(url).pipe(writeStream) const writeStream = fs.createWriteStream(fixturePath(`tmp/${project_id}.pdf`));
console.log("writing file out", fixturePath("tmp/#{project_id}.pdf")) request.get(url).pipe(writeStream);
writeStream.on "close", () => console.log("writing file out", fixturePath(`tmp/${project_id}.pdf`));
checkPdfInfo "tmp/#{project_id}.pdf", (error, optimised) => return writeStream.on("close", () => {
throw error if error? return checkPdfInfo(`tmp/${project_id}.pdf`, (error, optimised) => {
optimised.should.equal true if (error != null) { throw error; }
comparePdf project_id, example_dir, callback optimised.should.equal(true);
return comparePdf(project_id, example_dir, callback);
});
});
};
Client.runServer(4242, fixturePath("examples")) Client.runServer(4242, fixturePath("examples"));
describe "Example Documents", -> describe("Example Documents", function() {
before (done) -> before(done =>
ChildProcess.exec("rm test/acceptance/fixtures/tmp/*").on "exit", () -> ChildProcess.exec("rm test/acceptance/fixtures/tmp/*").on("exit", () => ClsiApp.ensureRunning(done))
ClsiApp.ensureRunning done );
for example_dir in fs.readdirSync fixturePath("examples") return Array.from(fs.readdirSync(fixturePath("examples"))).map((example_dir) =>
do (example_dir) -> (example_dir =>
describe example_dir, -> describe(example_dir, function() {
before -> before(function() {
@project_id = Client.randomId() + "_" + example_dir return this.project_id = Client.randomId() + "_" + example_dir;
});
it "should generate the correct pdf", (done) -> it("should generate the correct pdf", function(done) {
this.timeout(MOCHA_LATEX_TIMEOUT) this.timeout(MOCHA_LATEX_TIMEOUT);
Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) => return Client.compileDirectory(this.project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) => {
if error || body?.compile?.status is "failure" if (error || (__guard__(body != null ? body.compile : undefined, x => x.status) === "failure")) {
console.log "DEBUG: error", error, "body", JSON.stringify(body) console.log("DEBUG: error", error, "body", JSON.stringify(body));
pdf = Client.getOutputFile body, "pdf" }
downloadAndComparePdf(@project_id, example_dir, pdf.url, done) const pdf = Client.getOutputFile(body, "pdf");
return downloadAndComparePdf(this.project_id, example_dir, pdf.url, done);
});
});
it "should generate the correct pdf on the second run as well", (done) -> return it("should generate the correct pdf on the second run as well", function(done) {
this.timeout(MOCHA_LATEX_TIMEOUT) this.timeout(MOCHA_LATEX_TIMEOUT);
Client.compileDirectory @project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) => return Client.compileDirectory(this.project_id, fixturePath("examples"), example_dir, 4242, (error, res, body) => {
if error || body?.compile?.status is "failure" if (error || (__guard__(body != null ? body.compile : undefined, x => x.status) === "failure")) {
console.log "DEBUG: error", error, "body", JSON.stringify(body) console.log("DEBUG: error", error, "body", JSON.stringify(body));
pdf = Client.getOutputFile body, "pdf" }
downloadAndComparePdf(@project_id, example_dir, pdf.url, done) 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;
}

View File

@@ -1,41 +1,57 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS102: Remove unnecessary code created because of implicit returns
ClsiApp = require "./helpers/ClsiApp" * 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");
describe "Simple LaTeX file", -> describe("Simple LaTeX file", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
@request = this.request = {
resources: [ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\begin{document} \\begin{document}
Hello world Hello world
\\end{document} \\end{document}\
''' `
}
] ]
ClsiApp.ensureRunning => };
Client.compile @project_id, @request, (@error, @res, @body) => 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", -> it("should return the PDF", function() {
pdf = Client.getOutputFile(@body, "pdf") const pdf = Client.getOutputFile(this.body, "pdf");
pdf.type.should.equal "pdf" return pdf.type.should.equal("pdf");
});
it "should return the log", -> it("should return the log", function() {
log = Client.getOutputFile(@body, "log") const log = Client.getOutputFile(this.body, "log");
log.type.should.equal "log" return log.type.should.equal("log");
});
it "should provide the pdf for download", (done) -> it("should provide the pdf for download", function(done) {
pdf = Client.getOutputFile(@body, "pdf") const pdf = Client.getOutputFile(this.body, "pdf");
request.get pdf.url, (error, res, body) -> return request.get(pdf.url, function(error, res, body) {
res.statusCode.should.equal 200 res.statusCode.should.equal(200);
done() return done();
});
});
it "should provide the log for download", (done) -> return it("should provide the log for download", function(done) {
log = Client.getOutputFile(@body, "pdf") const log = Client.getOutputFile(this.body, "pdf");
request.get log.url, (error, res, body) -> return request.get(log.url, function(error, res, body) {
res.statusCode.should.equal 200 res.statusCode.should.equal(200);
done() return done();
});
});
});

View File

@@ -1,41 +1,58 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS102: Remove unnecessary code created because of implicit returns
expect = require("chai").expect * DS207: Consider shorter variations of null checks
ClsiApp = require "./helpers/ClsiApp" * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
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", -> describe("Syncing", function() {
before (done) -> before(function(done) {
content = ''' const content = `\
\\documentclass{article} \\documentclass{article}
\\begin{document} \\begin{document}
Hello world Hello world
\\end{document} \\end{document}\
''' `;
@request = this.request = {
resources: [ resources: [{
path: "main.tex" path: "main.tex",
content: content content
}
] ]
@project_id = Client.randomId() };
ClsiApp.ensureRunning => this.project_id = Client.randomId();
Client.compile @project_id, @request, (@error, @res, @body) => 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(); });
});
});
describe "from code to pdf", -> describe("from code to pdf", () =>
it "should return the correct location", (done) -> it("should return the correct location", function(done) {
Client.syncFromCode @project_id, "main.tex", 3, 5, (error, pdfPositions) -> return Client.syncFromCode(this.project_id, "main.tex", 3, 5, function(error, pdfPositions) {
throw error if error? if (error != null) { throw error; }
expect(pdfPositions).to.deep.equal( expect(pdfPositions).to.deep.equal({
pdf: [ { page: 1, h: 133.77, v: 134.76, height: 6.92, width: 343.71 } ] pdf: [ { page: 1, h: 133.77, v: 134.76, height: 6.92, width: 343.71 } ]
) });
done() return done();
});
})
);
describe "from pdf to code", -> return describe("from pdf to code", () =>
it "should return the correct location", (done) -> it("should return the correct location", function(done) {
Client.syncFromPdf @project_id, 1, 100, 200, (error, codePositions) => return Client.syncFromPdf(this.project_id, 1, 100, 200, (error, codePositions) => {
throw error if error? if (error != null) { throw error; }
expect(codePositions).to.deep.equal( expect(codePositions).to.deep.equal({
code: [ { file: 'main.tex', line: 3, column: -1 } ] code: [ { file: 'main.tex', line: 3, column: -1 } ]
) });
done() return done();
});
})
);
});

View File

@@ -1,34 +1,48 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS102: Remove unnecessary code created because of implicit returns
ClsiApp = require "./helpers/ClsiApp" * 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");
describe "Timed out compile", -> describe("Timed out compile", function() {
before (done) -> before(function(done) {
@request = this.request = {
options: options: {
timeout: 10 #seconds timeout: 10
resources: [ }, //seconds
path: "main.tex" resources: [{
content: ''' path: "main.tex",
\\documentclass{article} content: `\
\\begin{document} \\documentclass{article}
\\def\\x{Hello!\\par\\x} \\begin{document}
\\x \\def\\x{Hello!\\par\\x}
\\end{document} \\x
''' \\end{document}\
`
}
] ]
@project_id = Client.randomId() };
ClsiApp.ensureRunning => this.project_id = Client.randomId();
Client.compile @project_id, @request, (@error, @res, @body) => 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 a timeout error", -> it("should return a timeout error", function() {
@body.compile.error.should.equal "container timed out" return this.body.compile.error.should.equal("container timed out");
});
it "should return a timedout status", -> it("should return a timedout status", function() {
@body.compile.status.should.equal "timedout" return this.body.compile.status.should.equal("timedout");
});
it "should return the log output file name", -> return it("should return the log output file name", function() {
outputFilePaths = @body.compile.outputFiles.map((x) => x.path) const outputFilePaths = this.body.compile.outputFiles.map(x => x.path);
outputFilePaths.should.include('output.log') return outputFilePaths.should.include('output.log');
});
});

View File

@@ -1,222 +1,280 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS102: Remove unnecessary code created because of implicit returns
sinon = require "sinon" * DS207: Consider shorter variations of null checks
ClsiApp = require "./helpers/ClsiApp" * 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");
host = "localhost" const host = "localhost";
Server = const Server = {
run: () -> run() {
express = require "express" const express = require("express");
app = express() const app = express();
staticServer = express.static __dirname + "/../fixtures/" const staticServer = express.static(__dirname + "/../fixtures/");
app.get "/:random_id/*", (req, res, next) => app.get("/:random_id/*", (req, res, next) => {
@getFile(req.url) this.getFile(req.url);
req.url = "/" + req.params[0] req.url = `/${req.params[0]}`;
staticServer(req, res, next) return staticServer(req, res, next);
});
app.listen 31415, host return app.listen(31415, host);
},
getFile: () -> getFile() {},
randomId: () -> randomId() {
Math.random().toString(16).slice(2) return Math.random().toString(16).slice(2);
}
};
Server.run() Server.run();
describe "Url Caching", -> describe("Url Caching", function() {
describe "Downloading an image for the first time", -> describe("Downloading an image for the first time", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
@file = "#{Server.randomId()}/lion.png" this.file = `${Server.randomId()}/lion.png`;
@request = this.request = {
resources: [{ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\usepackage{graphicx} \\usepackage{graphicx}
\\begin{document} \\begin{document}
\\includegraphics{lion.png} \\includegraphics{lion.png}
\\end{document} \\end{document}\
''' `
}, { }, {
path: "lion.png" path: "lion.png",
url: "http://#{host}:31415/#{@file}" url: `http://${host}:31415/${this.file}`
}] }]
};
sinon.spy Server, "getFile" sinon.spy(Server, "getFile");
ClsiApp.ensureRunning => return ClsiApp.ensureRunning(() => {
Client.compile @project_id, @request, (@error, @res, @body) => done() return Client.compile(this.project_id, this.request, (error, res, body) => { this.error = error; this.res = res; this.body = body; return done(); });
});
});
afterEach -> afterEach(() => Server.getFile.restore());
Server.getFile.restore()
it "should download the image", -> return it("should download the image", function() {
Server.getFile return Server.getFile
.calledWith("/" + @file) .calledWith(`/${this.file}`)
.should.equal true .should.equal(true);
});
});
describe "When an image is in the cache and the last modified date is unchanged", -> describe("When an image is in the cache and the last modified date is unchanged", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
@file = "#{Server.randomId()}/lion.png" this.file = `${Server.randomId()}/lion.png`;
@request = this.request = {
resources: [{ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\usepackage{graphicx} \\usepackage{graphicx}
\\begin{document} \\begin{document}
\\includegraphics{lion.png} \\includegraphics{lion.png}
\\end{document} \\end{document}\
''' `
}, @image_resource = { }, (this.image_resource = {
path: "lion.png" path: "lion.png",
url: "http://#{host}:31415/#{@file}" url: `http://${host}:31415/${this.file}`,
modified: Date.now() modified: Date.now()
}] })]
};
Client.compile @project_id, @request, (@error, @res, @body) => return Client.compile(this.project_id, this.request, (error, res, body) => {
sinon.spy Server, "getFile" this.error = error;
Client.compile @project_id, @request, (@error, @res, @body) => this.res = res;
done() 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 -> after(() => Server.getFile.restore());
Server.getFile.restore()
it "should not download the image again", -> return it("should not download the image again", () => Server.getFile.called.should.equal(false));
Server.getFile.called.should.equal false });
describe "When an image is in the cache and the last modified date is advanced", -> describe("When an image is in the cache and the last modified date is advanced", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
@file = "#{Server.randomId()}/lion.png" this.file = `${Server.randomId()}/lion.png`;
@request = this.request = {
resources: [{ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\usepackage{graphicx} \\usepackage{graphicx}
\\begin{document} \\begin{document}
\\includegraphics{lion.png} \\includegraphics{lion.png}
\\end{document} \\end{document}\
''' `
}, @image_resource = { }, (this.image_resource = {
path: "lion.png" path: "lion.png",
url: "http://#{host}:31415/#{@file}" url: `http://${host}:31415/${this.file}`,
modified: @last_modified = Date.now() modified: (this.last_modified = Date.now())
}] })]
};
Client.compile @project_id, @request, (@error, @res, @body) => return Client.compile(this.project_id, this.request, (error, res, body) => {
sinon.spy Server, "getFile" this.error = error;
@image_resource.modified = new Date(@last_modified + 3000) this.res = res;
Client.compile @project_id, @request, (@error, @res, @body) => this.body = body;
done() 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 -> afterEach(() => Server.getFile.restore());
Server.getFile.restore()
it "should download the image again", -> return it("should download the image again", () => Server.getFile.called.should.equal(true));
Server.getFile.called.should.equal true });
describe "When an image is in the cache and the last modified date is further in the past", -> describe("When an image is in the cache and the last modified date is further in the past", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
@file = "#{Server.randomId()}/lion.png" this.file = `${Server.randomId()}/lion.png`;
@request = this.request = {
resources: [{ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\usepackage{graphicx} \\usepackage{graphicx}
\\begin{document} \\begin{document}
\\includegraphics{lion.png} \\includegraphics{lion.png}
\\end{document} \\end{document}\
''' `
}, @image_resource = { }, (this.image_resource = {
path: "lion.png" path: "lion.png",
url: "http://#{host}:31415/#{@file}" url: `http://${host}:31415/${this.file}`,
modified: @last_modified = Date.now() modified: (this.last_modified = Date.now())
}] })]
};
Client.compile @project_id, @request, (@error, @res, @body) => return Client.compile(this.project_id, this.request, (error, res, body) => {
sinon.spy Server, "getFile" this.error = error;
@image_resource.modified = new Date(@last_modified - 3000) this.res = res;
Client.compile @project_id, @request, (@error, @res, @body) => this.body = body;
done() 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 -> afterEach(() => Server.getFile.restore());
Server.getFile.restore()
it "should not download the image again", -> return it("should not download the image again", () => Server.getFile.called.should.equal(false));
Server.getFile.called.should.equal false });
describe "When an image is in the cache and the last modified date is not specified", -> describe("When an image is in the cache and the last modified date is not specified", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
@file = "#{Server.randomId()}/lion.png" this.file = `${Server.randomId()}/lion.png`;
@request = this.request = {
resources: [{ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\usepackage{graphicx} \\usepackage{graphicx}
\\begin{document} \\begin{document}
\\includegraphics{lion.png} \\includegraphics{lion.png}
\\end{document} \\end{document}\
''' `
}, @image_resource = { }, (this.image_resource = {
path: "lion.png" path: "lion.png",
url: "http://#{host}:31415/#{@file}" url: `http://${host}:31415/${this.file}`,
modified: @last_modified = Date.now() modified: (this.last_modified = Date.now())
}] })]
};
Client.compile @project_id, @request, (@error, @res, @body) => return Client.compile(this.project_id, this.request, (error, res, body) => {
sinon.spy Server, "getFile" this.error = error;
delete @image_resource.modified this.res = res;
Client.compile @project_id, @request, (@error, @res, @body) => this.body = body;
done() 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 -> afterEach(() => Server.getFile.restore());
Server.getFile.restore()
it "should download the image again", -> return it("should download the image again", () => Server.getFile.called.should.equal(true));
Server.getFile.called.should.equal true });
describe "After clearing the cache", -> return describe("After clearing the cache", function() {
before (done) -> before(function(done) {
@project_id = Client.randomId() this.project_id = Client.randomId();
@file = "#{Server.randomId()}/lion.png" this.file = `${Server.randomId()}/lion.png`;
@request = this.request = {
resources: [{ resources: [{
path: "main.tex" path: "main.tex",
content: ''' content: `\
\\documentclass{article} \\documentclass{article}
\\usepackage{graphicx} \\usepackage{graphicx}
\\begin{document} \\begin{document}
\\includegraphics{lion.png} \\includegraphics{lion.png}
\\end{document} \\end{document}\
''' `
}, @image_resource = { }, (this.image_resource = {
path: "lion.png" path: "lion.png",
url: "http://#{host}:31415/#{@file}" url: `http://${host}:31415/${this.file}`,
modified: @last_modified = Date.now() modified: (this.last_modified = Date.now())
}] })]
};
Client.compile @project_id, @request, (error) => return Client.compile(this.project_id, this.request, error => {
throw error if error? if (error != null) { throw error; }
Client.clearCache @project_id, (error, res, body) => return Client.clearCache(this.project_id, (error, res, body) => {
throw error if error? if (error != null) { throw error; }
sinon.spy Server, "getFile" sinon.spy(Server, "getFile");
Client.compile @project_id, @request, (@error, @res, @body) => return Client.compile(this.project_id, this.request, (error1, res1, body1) => {
done() this.error = error1;
this.res = res1;
this.body = body1;
return done();
});
});
});
});
afterEach -> afterEach(() => Server.getFile.restore());
Server.getFile.restore()
it "should download the image again", -> return it("should download the image again", () => Server.getFile.called.should.equal(true));
Server.getFile.called.should.equal true });
});

View File

@@ -1,38 +1,52 @@
Client = require "./helpers/Client" /*
request = require "request" * decaffeinate suggestions:
require("chai").should() * DS102: Remove unnecessary code created because of implicit returns
expect = require("chai").expect * DS207: Consider shorter variations of null checks
path = require("path") * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
fs = require("fs") */
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", -> describe("Syncing", function() {
before (done) -> before(function(done) {
@request = this.request = {
resources: [ resources: [{
path: "main.tex" path: "main.tex",
content: fs.readFileSync(path.join(__dirname,"../fixtures/naugty_strings.txt"),"utf-8") content: fs.readFileSync(path.join(__dirname,"../fixtures/naugty_strings.txt"),"utf-8")
}
] ]
@project_id = Client.randomId() };
ClsiApp.ensureRunning => this.project_id = Client.randomId();
Client.compile @project_id, @request, (@error, @res, @body) => 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(); });
});
});
describe "wordcount file", -> return describe("wordcount file", () =>
it "should return wordcount info", (done) -> it("should return wordcount info", function(done) {
Client.wordcount @project_id, "main.tex", (error, result) -> return Client.wordcount(this.project_id, "main.tex", function(error, result) {
throw error if error? if (error != null) { throw error; }
expect(result).to.deep.equal( expect(result).to.deep.equal({
texcount: { texcount: {
encode: "utf8" encode: "utf8",
textWords: 2281 textWords: 2281,
headWords: 2 headWords: 2,
outside: 0 outside: 0,
headers: 2 headers: 2,
elements: 0 elements: 0,
mathInline: 6 mathInline: 6,
mathDisplay: 0 mathDisplay: 0,
errors: 0 errors: 0,
messages: "" messages: ""
} }
) });
done() return done();
});
})
);
});

View File

@@ -1,105 +1,147 @@
request = require "request" /*
fs = require "fs" * decaffeinate suggestions:
Settings = require "settings-sharelatex" * DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* 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");
host = "localhost" const host = "localhost";
module.exports = Client = module.exports = (Client = {
host: Settings.apis.clsi.url host: Settings.apis.clsi.url,
randomId: () -> randomId() {
Math.random().toString(16).slice(2) return Math.random().toString(16).slice(2);
},
compile: (project_id, data, callback = (error, res, body) ->) -> compile(project_id, data, callback) {
request.post { if (callback == null) { callback = function(error, res, body) {}; }
url: "#{@host}/project/#{project_id}/compile" return request.post({
json: url: `${this.host}/project/${project_id}/compile`,
json: {
compile: data compile: data
}, callback
clearCache: (project_id, callback = (error, res, body) ->) ->
request.del "#{@host}/project/#{project_id}", callback
getOutputFile: (response, type) ->
for file in response.compile.outputFiles
if file.type == type and file.url.match("output.#{type}")
return file
return null
runServer: (port, directory) ->
express = require("express")
app = express()
app.use express.static(directory)
console.log("starting test server on", port, host)
app.listen(port, host).on "error", (error) ->
console.error "error starting server:", error.message
process.exit(1)
syncFromCode: (project_id, file, line, column, callback = (error, pdfPositions) ->) ->
request.get {
url: "#{@host}/project/#{project_id}/sync/code"
qs: {
file: file
line: line
column: column
} }
}, (error, response, body) -> }, callback);
return callback(error) if error? },
callback null, JSON.parse(body)
syncFromPdf: (project_id, page, h, v, callback = (error, pdfPositions) ->) -> clearCache(project_id, callback) {
request.get { if (callback == null) { callback = function(error, res, body) {}; }
url: "#{@host}/project/#{project_id}/sync/pdf" return request.del(`${this.host}/project/${project_id}`, callback);
qs: { },
page: page,
h: h, v: v getOutputFile(response, type) {
for (let file of Array.from(response.compile.outputFiles)) {
if ((file.type === type) && file.url.match(`output.${type}`)) {
return file;
} }
}, (error, response, body) -> }
return callback(error) if error? return null;
callback null, JSON.parse(body) },
compileDirectory: (project_id, baseDirectory, directory, serverPort, callback = (error, res, body) ->) -> runServer(port, directory) {
resources = [] const express = require("express");
entities = fs.readdirSync("#{baseDirectory}/#{directory}") const app = express();
rootResourcePath = "main.tex" app.use(express.static(directory));
while (entities.length > 0) console.log("starting test server on", port, host);
entity = entities.pop() return app.listen(port, host).on("error", function(error) {
stat = fs.statSync("#{baseDirectory}/#{directory}/#{entity}") console.error("error starting server:", error.message);
if stat.isDirectory() return process.exit(1);
entities = entities.concat fs.readdirSync("#{baseDirectory}/#{directory}/#{entity}").map (subEntity) -> });
if subEntity == "main.tex" },
rootResourcePath = "#{entity}/#{subEntity}"
return "#{entity}/#{subEntity}"
else if stat.isFile() and entity != "output.pdf" syncFromCode(project_id, file, line, column, callback) {
extension = entity.split(".").pop() if (callback == null) { callback = function(error, pdfPositions) {}; }
if ["tex", "bib", "cls", "sty", "pdf_tex", "Rtex", "ist", "md", "Rmd"].indexOf(extension) > -1 return request.get({
resources.push url: `${this.host}/project/${project_id}/sync/code`,
path: entity qs: {
content: fs.readFileSync("#{baseDirectory}/#{directory}/#{entity}").toString() file,
else if ["eps", "ttf", "png", "jpg", "pdf", "jpeg"].indexOf(extension) > -1 line,
resources.push column
path: entity }
url: "http://#{host}:#{serverPort}/#{directory}/#{entity}" }, function(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
}
}, function(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(function(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 modified: stat.mtime
});
fs.readFile "#{baseDirectory}/#{directory}/options.json", (error, body) => }
req =
resources: resources
rootResourcePath: rootResourcePath
if !error?
body = JSON.parse body
req.options = body
@compile project_id, req, callback
wordcount: (project_id, file, callback = (error, pdfPositions) ->) ->
request.get {
url: "#{@host}/project/#{project_id}/wordcount"
qs: {
file: file
} }
}, (error, response, body) -> }
return callback(error) if error?
callback null, JSON.parse(body) return fs.readFile(`${baseDirectory}/${directory}/options.json`, (error, body) => {
const req = {
resources,
rootResourcePath
};
if ((error == null)) {
body = JSON.parse(body);
req.options = body;
}
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
}
}, function(error, response, body) {
if (error != null) { return callback(error); }
return callback(null, JSON.parse(body));
});
}
});

View File

@@ -1,24 +1,46 @@
app = require('../../../../app') /*
require("logger-sharelatex").logger.level("info") * decaffeinate suggestions:
logger = require("logger-sharelatex") * DS101: Remove unnecessary use of Array.from
Settings = require("settings-sharelatex") * DS102: Remove unnecessary code created because of implicit returns
* DS103: Rewrite code to no longer use __guard__
* DS205: Consider reworking code to avoid use of IIFEs
* 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");
module.exports = module.exports = {
running: false running: false,
initing: false initing: false,
callbacks: [] callbacks: [],
ensureRunning: (callback = (error) ->) -> ensureRunning(callback) {
if @running if (callback == null) { callback = function(error) {}; }
return callback() if (this.running) {
else if @initing return callback();
@callbacks.push callback } else if (this.initing) {
else return this.callbacks.push(callback);
@initing = true } else {
@callbacks.push callback this.initing = true;
app.listen Settings.internal?.clsi?.port, "localhost", (error) => this.callbacks.push(callback);
throw error if error? return app.listen(__guard__(Settings.internal != null ? Settings.internal.clsi : undefined, x => x.port), "localhost", error => {
@running = true if (error != null) { throw error; }
logger.log("clsi running in dev mode") this.running = true;
logger.log("clsi running in dev mode");
for callback in @callbacks return (() => {
callback() 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;
}