Merge pull request #65 from sharelatex/bg-fix-main-doc-selection

fix main doc selection (connects to overleaf/sharelatex#222)
This commit is contained in:
Brian Gough 2017-10-18 13:11:10 +01:00 committed by GitHub
commit 5d116e8ce9
4 changed files with 67 additions and 5 deletions

View file

@ -29,7 +29,11 @@ module.exports = ClsiManager =
sendRequestOnce: (project_id, user_id, options = {}, callback = (error, status, outputFiles, clsiServerId, validationProblems) ->) -> sendRequestOnce: (project_id, user_id, options = {}, callback = (error, status, outputFiles, clsiServerId, validationProblems) ->) ->
ClsiManager._buildRequest project_id, options, (error, req) -> ClsiManager._buildRequest project_id, options, (error, req) ->
return callback(error) if error? if error?
if error.message is "no main file specified"
return callback(null, "validation-problems", null, null, {mainFile:error.message})
else
return callback(error)
logger.log project_id: project_id, "sending compile to CLSI" logger.log project_id: project_id, "sending compile to CLSI"
ClsiFormatChecker.checkRecoursesForProblems req.compile?.resources, (err, validationProblems)-> ClsiFormatChecker.checkRecoursesForProblems req.compile?.resources, (err, validationProblems)->
if err? if err?
@ -180,7 +184,7 @@ module.exports = ClsiManager =
# present in the docupdater. This allows finaliseRequest to # present in the docupdater. This allows finaliseRequest to
# identify the root doc. # identify the root doc.
possibleRootDocIds = [options.rootDoc_id, project.rootDoc_id] possibleRootDocIds = [options.rootDoc_id, project.rootDoc_id]
for rootDoc_id in possibleRootDocIds when rootDoc_id? for rootDoc_id in possibleRootDocIds when rootDoc_id? and rootDoc_id of docPath
path = docPath[rootDoc_id] path = docPath[rootDoc_id]
docs[path] ?= {_id: rootDoc_id, path: path} docs[path] ?= {_id: rootDoc_id, path: path}
ClsiManager._finaliseRequest project_id, options, project, docs, [], callback ClsiManager._finaliseRequest project_id, options, project, docs, [], callback
@ -206,9 +210,12 @@ module.exports = ClsiManager =
resources = [] resources = []
rootResourcePath = null rootResourcePath = null
rootResourcePathOverride = null rootResourcePathOverride = null
hasMainFile = false
numberOfDocsInProject = 0
for path, doc of docs for path, doc of docs
path = path.replace(/^\//, "") # Remove leading / path = path.replace(/^\//, "") # Remove leading /
numberOfDocsInProject++
if doc.lines? # add doc to resources unless it is just a stub entry if doc.lines? # add doc to resources unless it is just a stub entry
resources.push resources.push
path: path path: path
@ -217,11 +224,20 @@ module.exports = ClsiManager =
rootResourcePath = path rootResourcePath = path
if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString() if options.rootDoc_id? and doc._id.toString() == options.rootDoc_id.toString()
rootResourcePathOverride = path rootResourcePathOverride = path
if path is "main.tex"
hasMainFile = true
rootResourcePath = rootResourcePathOverride if rootResourcePathOverride? rootResourcePath = rootResourcePathOverride if rootResourcePathOverride?
if !rootResourcePath? if !rootResourcePath?
if hasMainFile
logger.warn {project_id}, "no root document found, setting to main.tex" logger.warn {project_id}, "no root document found, setting to main.tex"
rootResourcePath = "main.tex" rootResourcePath = "main.tex"
else if numberOfDocsInProject is 1 # only one file, must be the main document
for path, doc of docs
rootResourcePath = path.replace(/^\//, "") # Remove leading /
logger.warn {project_id, rootResourcePath: rootResourcePath}, "no root document found, single document in project"
else
return callback new Error("no main file specified")
for path, file of files for path, file of files
path = path.replace(/^\//, "") # Remove leading / path = path.replace(/^\//, "") # Remove leading /

View file

@ -317,6 +317,9 @@ div.full-size.pdf(ng-controller="PdfController")
div div
li(ng-repeat="entry in pdf.validation.conflictedPaths") {{ '/'+entry['path'] }} li(ng-repeat="entry in pdf.validation.conflictedPaths") {{ '/'+entry['path'] }}
.alert.alert-danger(ng-show="pdf.validation.mainFile")
strong #{translate("main_file_not_found")}
span #{translate("please_set_main_file")}
.pdf-errors(ng-switch-when="errors") .pdf-errors(ng-switch-when="errors")

View file

@ -231,6 +231,7 @@ define [
else if response.status == "validation-problems" else if response.status == "validation-problems"
$scope.pdf.view = "validation-problems" $scope.pdf.view = "validation-problems"
$scope.pdf.validation = response.validationProblems $scope.pdf.validation = response.validationProblems
$scope.shouldShowLogs = false
else if response.status == "compile-in-progress" else if response.status == "compile-in-progress"
$scope.pdf.view = 'errors' $scope.pdf.view = 'errors'
$scope.pdf.compileInProgress = true $scope.pdf.compileInProgress = true

View file

@ -347,6 +347,48 @@ describe "ClsiManager", ->
it "should set to main.tex", -> it "should set to main.tex", ->
@request.compile.rootResourcePath.should.equal "main.tex" @request.compile.rootResourcePath.should.equal "main.tex"
describe "when there is no valid root document and no main.tex document", ->
beforeEach () ->
@project.rootDoc_id = "not-valid"
@docs = {
"/other.tex": @doc_1 = {
name: "other.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"
]
}
}
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ClsiManager._buildRequest @project, null, @callback
it "should report an error", ->
@callback.calledWith(new Error("no main file specified")).should.equal true
describe "when there is no valid root document and a single document which is not main.tex", ->
beforeEach (done) ->
@project.rootDoc_id = "not-valid"
@docs = {
"/other.tex": @doc_1 = {
name: "other.tex"
_id: "mock-doc-id-1"
lines: ["Hello", "world"]
}
}
@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
@ClsiManager._buildRequest @project, null, (@error, @request) =>
done()
it "should set io to the only file", ->
@request.compile.rootResourcePath.should.equal "other.tex"
describe "with the draft option", -> describe "with the draft option", ->
it "should add the draft option into the request", (done) -> it "should add the draft option into the request", (done) ->
@ClsiManager._buildRequest @project_id, {timeout:100, draft: true}, (error, request) => @ClsiManager._buildRequest @project_id, {timeout:100, draft: true}, (error, request) =>