From 6a2370b5be2d1dd235dbbc10d3b48f091e005b47 Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 6 May 2014 12:54:26 +0100 Subject: [PATCH] Use docstore for CLSI request building --- .../Features/Compile/ClsiManager.coffee | 100 ++++++++---------- .../coffee/Compile/ClsiManagerTests.coffee | 98 +++++++++-------- 2 files changed, 93 insertions(+), 105 deletions(-) diff --git a/services/web/app/coffee/Features/Compile/ClsiManager.coffee b/services/web/app/coffee/Features/Compile/ClsiManager.coffee index 379fc5a58c..be39722037 100644 --- a/services/web/app/coffee/Features/Compile/ClsiManager.coffee +++ b/services/web/app/coffee/Features/Compile/ClsiManager.coffee @@ -3,24 +3,23 @@ async = require "async" Settings = require "settings-sharelatex" request = require('request') Project = require("../../models/Project").Project +ProjectEntityHandler = require("../Project/ProjectEntityHandler") logger = require "logger-sharelatex" url = require("url") module.exports = ClsiManager = sendRequest: (project_id, callback = (error, success) ->) -> - Project.findById project_id, (error, project) -> + ClsiManager._buildRequest project_id, (error, req) -> return callback(error) if error? - ClsiManager._buildRequest project, (error, req) -> + logger.log project_id: project_id, "sending compile to CLSI" + ClsiManager._postToClsi project_id, req, (error, response) -> return callback(error) if error? - logger.log project_id: project_id, "sending compile to CLSI" - ClsiManager._postToClsi project_id, req, (error, response) -> - return callback(error) if error? - logger.log project_id: project_id, response: response, "received compile response from CLSI" - callback( - null - (response?.compile?.status == "success") - ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles) - ) + logger.log project_id: project_id, response: response, "received compile response from CLSI" + callback( + null + (response?.compile?.status == "success") + ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles) + ) getLogLines: (project_id, callback = (error, lines) ->) -> request "#{Settings.apis.clsi.url}/project/#{project_id}/output/output.log", (error, response, body) -> @@ -44,54 +43,45 @@ module.exports = ClsiManager = return outputFiles VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"] - _buildRequest: (project, callback = (error, request) ->) -> - if project.compiler not in @VALID_COMPILERS - project.compiler = "pdflatex" + _buildRequest: (project_id, callback = (error, request) ->) -> + Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) -> + return callback(error) if error? + return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project? - resources = [] - rootResourcePath = null + if project.compiler not in ClsiManager.VALID_COMPILERS + project.compiler = "pdflatex" - addDoc = (basePath, doc, callback = (error) ->) -> - path = Path.join(basePath, doc.name) - resources.push - path: path - content: doc.lines.join("\n") - if doc._id.toString() == project.rootDoc_id.toString() - rootResourcePath = path - callback() + ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) -> + return callback(error) if error? + ProjectEntityHandler.getAllFiles project_id, (error, files = {}) -> + return callback(error) if error? - addFile = (basePath, file, callback = (error) ->) -> - resources.push - path: Path.join(basePath, file.name) - url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}" - modified: file.created?.getTime() - callback() + resources = [] + rootResourcePath = null - addFolder = (basePath, folder, callback = (error) ->) -> - jobs = [] - for doc in folder.docs - do (doc) -> - jobs.push (callback) -> addDoc(basePath, doc, callback) + for path, doc of docs + path = path.replace(/^\//, "") # Remove leading / + resources.push + path: path + content: doc.lines.join("\n") + if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString() + rootResourcePath = path - for file in folder.fileRefs - do (file) -> - jobs.push (callback) -> addFile(basePath, file, callback) + for path, file of files + path = path.replace(/^\//, "") # Remove leading / + resources.push + path: path + url: "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}" + modified: file.created?.getTime() - for childFolder in folder.folders - do (childFolder) -> - jobs.push (callback) -> addFolder(Path.join(basePath, childFolder.name), childFolder, callback) - - async.series jobs, callback - - addFolder "", project.rootFolder[0], (error) -> - if !rootResourcePath? - callback new Error("no root document exists") - else - callback null, { - compile: - options: - compiler: project.compiler - rootResourcePath: rootResourcePath - resources: resources - } + if !rootResourcePath? + callback new Error("no root document exists") + else + callback null, { + compile: + options: + compiler: project.compiler + rootResourcePath: rootResourcePath + resources: resources + } diff --git a/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee b/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee index 2d9deebbc5..b62e66493d 100644 --- a/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee +++ b/services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee @@ -16,13 +16,13 @@ describe "ClsiManager", -> clsi: url: "http://clsi.example.com" "../../models/Project": Project: @Project = {} + "../Project/ProjectEntityHandler": @ProjectEntityHandler = {} "logger-sharelatex": @logger = { log: sinon.stub() } @project_id = "project-id" @callback = sinon.stub() describe "sendRequest", -> beforeEach -> - @Project.findById = sinon.stub().callsArgWith(1, null, @project = "mock-project") @ClsiManager._buildRequest = sinon.stub().callsArgWith(1, null, @request = "mock-request") describe "with a successful compile", -> @@ -40,14 +40,9 @@ describe "ClsiManager", -> }) @ClsiManager.sendRequest @project_id, @callback - it "should look up the project", -> - @Project.findById - .calledWith(@project_id) - .should.equal true - - it "should convert the project to a request", -> + it "should build the request", -> @ClsiManager._buildRequest - .calledWith(@project) + .calledWith(@project_id) .should.equal true it "should send the request to the CLSI", -> @@ -82,52 +77,55 @@ describe "ClsiManager", -> _id: @project_id compiler: @compiler = "latex" rootDoc_id: "mock-doc-id-1" - rootFolder: [ - docs: [ - @doc_1 = { - name: "main.tex" - _id: "mock-doc-id-1" - lines: ["Hello", "world"] - } - ] - fileRefs: [] - folders: [ - @chapters_folder = { - name: "chapters" - docs: [ - @doc_2 = { - name: "chapter1.tex" - _id: "mock-doc-id-2" - lines: [ - "Chapter 1" - ] - } - ] - fileRefs: [] - folders: [] - }, - @images_folder = { - name: "images" - docs: [] - fileRefs: [ - @file_1 = { - name: "image.png" - _id: "mock-file-id-1" - created: new Date() - } - ] - folders: [] - } - ] - ] + @docs = { + "/main.tex": @doc_1 = { + name: "main.tex" + _id: "mock-doc-id-1" + lines: ["Hello", "world"] + }, + "/chapters/chapter1.tex": @doc_2 = { + name: "chapter1.tex" + _id: "mock-doc-id-2" + lines: [ + "Chapter 1" + ] + } + } + + @files = { + "/images/image.png": @file_1 = { + name: "image.png" + _id: "mock-file-id-1" + created: new Date() + } + } + + @Project.findById = sinon.stub().callsArgWith(2, null, @project) + @ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs) + @ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files) describe "with a valid project", -> beforeEach (done) -> - @ClsiManager._buildRequest @project, (error, request) => + @ClsiManager._buildRequest @project_id, (error, request) => @request = request done() + it "should get the project with the required fields", -> + @Project.findById + .calledWith(@project_id, {compiler:1, rootDoc_id: 1}) + .should.equal true + + it "should get all the docs", -> + @ProjectEntityHandler.getAllDocs + .calledWith(@project_id) + .should.equal true + + it "should get all the files", -> + @ProjectEntityHandler.getAllFiles + .calledWith(@project_id) + .should.equal true + it "should build up the CLSI request", -> expect(@request).to.deep.equal( compile: @@ -135,13 +133,13 @@ describe "ClsiManager", -> compiler: @compiler rootResourcePath: "main.tex" resources: [{ - path: @doc_1.name + path: "main.tex" content: @doc_1.lines.join("\n") }, { - path: "#{@chapters_folder.name}/#{@doc_2.name}" + path: "chapters/chapter1.tex" content: @doc_2.lines.join("\n") }, { - path: "#{@images_folder.name}/#{@file_1.name}" + path: "images/image.png" url: "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}" modified: @file_1.created.getTime() }]