From d7cddd14fa31e5689a924d1b4d3f460bb1b5d092 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Wed, 19 Mar 2025 09:31:32 +0000 Subject: [PATCH] Use first root .tex file as fallback main doc when importing from zip (#24302) GitOrigin-RevId: 51affe14b77aa4f774d5e5f0807f42e07842f807 --- .../Features/Project/ProjectRootDocManager.js | 25 +++++--- .../src/Project/ProjectRootDocManagerTests.js | 62 +++++++++++++++++-- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/services/web/app/src/Features/Project/ProjectRootDocManager.js b/services/web/app/src/Features/Project/ProjectRootDocManager.js index d822e86845..0bac5e35ed 100644 --- a/services/web/app/src/Features/Project/ProjectRootDocManager.js +++ b/services/web/app/src/Features/Project/ProjectRootDocManager.js @@ -113,6 +113,7 @@ module.exports = ProjectRootDocManager = { if (err != null) { return callback(err) } + let firstFileInRootFolder let doc = null return async.until( @@ -130,16 +131,26 @@ module.exports = ProjectRootDocManager = { if (DocumentHelper.contentHasDocumentclass(content)) { doc = { path: file, content } } - return cb(null) + + if (!firstFileInRootFolder && !file.includes('/')) { + firstFileInRootFolder = { path: file, content } + } + cb(null) } ) }, - err => - callback( - err, - doc != null ? doc.path : undefined, - doc != null ? doc.content : undefined - ) + err => { + if (err) { + return callback(err) + } + + // if no doc was found, use the first file in the root folder as the main doc + if (!doc && firstFileInRootFolder) { + doc = firstFileInRootFolder + } + + callback(null, doc?.path, doc?.content) + } ) } ), diff --git a/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js b/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js index 1e37ee1380..41b7516baa 100644 --- a/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js +++ b/services/web/test/unit/src/Project/ProjectRootDocManagerTests.js @@ -212,7 +212,7 @@ describe('ProjectRootDocManager', function () { '/foo', (error, path) => { expect(error).not.to.exist - expect(path).not.to.exist + expect(path).to.equal('a.tex') sinon.assert.callOrder( this.fs.readFile.withArgs('/foo/a.tex'), this.fs.readFile.withArgs('/foo/b.tex'), @@ -230,7 +230,7 @@ describe('ProjectRootDocManager', function () { '/foo', (error, path) => { expect(error).not.to.exist - expect(path).not.to.exist + expect(path).to.equal('c.tex') sinon.assert.callOrder( this.fs.readFile.withArgs('/foo/c.tex'), this.fs.readFile.withArgs('/foo/a.tex'), @@ -274,6 +274,58 @@ describe('ProjectRootDocManager', function () { }) }) + describe('when main.tex does not contain a line starting with \\documentclass', function () { + beforeEach(function () { + this.fs.readFile.withArgs('/foo/a.tex').callsArgWith(2, null, 'foo') + this.fs.readFile.withArgs('/foo/main.tex').callsArgWith(2, null, 'foo') + this.fs.readFile.withArgs('/foo/z.tex').callsArgWith(2, null, 'foo') + this.fs.readFile + .withArgs('/foo/nested/chapter1a.tex') + .callsArgWith(2, null, 'foo') + }) + + it('returns the first .tex file from the root folder', function (done) { + this.globbyFiles.splice( + 0, + this.globbyFiles.length, + 'a.tex', + 'z.tex', + 'nested/chapter1a.tex' + ) + + this.ProjectRootDocManager.findRootDocFileFromDirectory( + '/foo', + (error, path, content) => { + expect(error).not.to.exist + expect(path).to.equal('a.tex') + expect(content).to.equal('foo') + return done() + } + ) + }) + + it('returns main.tex file from the root folder', function (done) { + this.globbyFiles.splice( + 0, + this.globbyFiles.length, + 'a.tex', + 'z.tex', + 'main.tex', + 'nested/chapter1a.tex' + ) + + this.ProjectRootDocManager.findRootDocFileFromDirectory( + '/foo', + (error, path, content) => { + expect(error).not.to.exist + expect(path).to.equal('main.tex') + expect(content).to.equal('foo') + return done() + } + ) + }) + }) + describe('when a.tex contains a documentclass', function () { beforeEach(function () { return this.fs.readFile @@ -307,13 +359,11 @@ describe('ProjectRootDocManager', function () { }) describe('when there is no documentclass', function () { - it('returns null with no error', function (done) { + it('returns with no error', function (done) { return this.ProjectRootDocManager.findRootDocFileFromDirectory( '/foo', - (error, path, content) => { + error => { expect(error).not.to.exist - expect(path).not.to.exist - expect(content).not.to.exist return done() } )