diff --git a/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee b/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee index b331c38aa6..973d2b2ed6 100644 --- a/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee +++ b/services/web/app/coffee/Features/Project/ProjectRootDocManager.coffee @@ -35,11 +35,15 @@ module.exports = ProjectRootDocManager = setRootDocFromName: (project_id, rootDocName, callback = (error) ->) -> ProjectEntityHandler.getAllDocPathsFromProjectById project_id, (error, docPaths) -> return callback(error) if error? + # strip off leading and trailing quotes from rootDocName + rootDocName = rootDocName.replace(/^\'|\'$/g,"") + # prepend a slash for the root folder if not present + rootDocName = "/#{rootDocName}" if rootDocName[0] isnt '/' # find the root doc from the filename root_doc_id = null for doc_id, path of docPaths # docpaths have a leading / so allow matching "folder/filename" and "/folder/filename" - if path == rootDocName or path == "/#{rootDocName}" + if path == rootDocName root_doc_id = doc_id # try a basename match if there was no match if !root_doc_id diff --git a/services/web/test/unit/coffee/Project/ProjectRootDocManagerTests.coffee b/services/web/test/unit/coffee/Project/ProjectRootDocManagerTests.coffee index d65b92043a..47c2973e9f 100644 --- a/services/web/test/unit/coffee/Project/ProjectRootDocManagerTests.coffee +++ b/services/web/test/unit/coffee/Project/ProjectRootDocManagerTests.coffee @@ -133,6 +133,25 @@ describe 'ProjectRootDocManager', -> @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-3") .should.equal true + describe "when there is a suitable root doc but the filename is in quotes", -> + beforeEach (done)-> + @docPaths = + "doc-id-1": "/chapter1.tex" + "doc-id-2": "/main.tex" + "doc-id-3": "/nested/chapter1a.tex" + "doc-id-4": "/nested/chapter1b.tex" + @ProjectEntityHandler.getAllDocPathsFromProjectById = sinon.stub().callsArgWith(1, null, @docPaths) + @ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2) + @ProjectRootDocManager.setRootDocFromName @project_id, "'main.tex'", done + + it "should check the docs of the project", -> + @ProjectEntityHandler.getAllDocPathsFromProjectById.calledWith(@project_id) + .should.equal true + + it "should set the root doc to main.tex", -> + @ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2") + .should.equal true + describe "when there is no suitable root doc", -> beforeEach (done)-> @docPaths =