diff --git a/.gitignore b/.gitignore index 0ec1b43c3..76a8c126f 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,6 @@ test/fixtures/multiroot/ test/fixtures/**/*.dvi test/fixtures/**/*.log test/fixtures/**/*.synctex* -test/fixtures/**/*.aux test/fixtures/**/*.fdb_latexmk test/fixtures/**/*.blg test/fixtures/**/*.out diff --git a/src/core/cache.ts b/src/core/cache.ts index 2e6c46fbd..bc6289da1 100644 --- a/src/core/cache.ts +++ b/src/core/cache.ts @@ -43,11 +43,10 @@ export const cache = { refreshCacheAggressive, loadFlsFile, _test: { + caches, canCache, isExcluded, - updateChildren, - updateAST, - updateElements + updateAST } } @@ -590,9 +589,9 @@ async function loadFlsFile(filePath: string): Promise { for (const outputFile of ioFiles.output) { if (path.extname(outputFile) === '.aux' && await lw.file.exists(outputFile)) { - logger.log(`Found .aux ${filePath} from .fls ${flsPath} , parsing.`) + logger.log(`Found .aux ${outputFile} from .fls ${flsPath} , parsing.`) await parseAuxFile(outputFile, path.dirname(outputFile).replace(outDir, rootDir)) - logger.log(`Parsed .aux ${filePath} .`) + logger.log(`Parsed .aux ${outputFile} .`) } } logger.log(`Parsed .fls ${flsPath} .`) @@ -703,10 +702,12 @@ async function parseAuxFile(filePath: string, srcDir: string) { * bibliography files. Defaults to the root file path if not provided. * @param {string[]} [includedBib=[]] - An array to accumulate the bibliography * files found. + * @param {string[]} [checkedTeX=[]] - An array to store the paths of TeX files + * already checked. * @returns {string[]} - An array of unique bibliography file paths included in * the specified file and its children. */ -function getIncludedBib(filePath?: string, includedBib: string[] = []): string[] { +function getIncludedBib(filePath?: string, includedBib: string[] = [], checkedTeX: string[] = []): string[] { filePath = filePath ?? lw.root.file.path if (filePath === undefined) { return [] @@ -715,14 +716,14 @@ function getIncludedBib(filePath?: string, includedBib: string[] = []): string[] if (fileCache === undefined) { return [] } - const checkedTeX = [ filePath ] + checkedTeX.push(filePath) includedBib.push(...fileCache.bibfiles) for (const child of fileCache.children) { if (checkedTeX.includes(child.filePath)) { // Already parsed continue } - getIncludedBib(child.filePath, includedBib) + getIncludedBib(child.filePath, includedBib, checkedTeX) } // Make sure to return an array with unique entries return Array.from(new Set(includedBib)) @@ -765,7 +766,7 @@ function getIncludedTeX(filePath?: string, includedTeX: string[] = [], cachedOnl } getIncludedTeX(child.filePath, includedTeX, cachedOnly) } - return includedTeX + return Array.from(new Set(includedTeX)) } /** diff --git a/test/fixtures/unittest/03_core_cache/included_bib/another.tex b/test/fixtures/unittest/03_core_cache/included_bib/another.tex new file mode 100644 index 000000000..1629f02ad --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_bib/another.tex @@ -0,0 +1 @@ +\input{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_bib/circular_1.tex b/test/fixtures/unittest/03_core_cache/included_bib/circular_1.tex new file mode 100644 index 000000000..885ed70db --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_bib/circular_1.tex @@ -0,0 +1 @@ +\input{circular_2} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_bib/circular_2.tex b/test/fixtures/unittest/03_core_cache/included_bib/circular_2.tex new file mode 100644 index 000000000..6d470e612 --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_bib/circular_2.tex @@ -0,0 +1,2 @@ +\input{circular_1} +\bibliography{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_bib/duplicate_1.tex b/test/fixtures/unittest/03_core_cache/included_bib/duplicate_1.tex new file mode 100644 index 000000000..221fbd635 --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_bib/duplicate_1.tex @@ -0,0 +1,2 @@ +\input{duplicate_2} +\bibliography{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_bib/duplicate_2.tex b/test/fixtures/unittest/03_core_cache/included_bib/duplicate_2.tex new file mode 100644 index 000000000..dfcb23ccb --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_bib/duplicate_2.tex @@ -0,0 +1 @@ +\bibliography{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_bib/main.bib b/test/fixtures/unittest/03_core_cache/included_bib/main.bib new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/unittest/03_core_cache/included_bib/main.tex b/test/fixtures/unittest/03_core_cache/included_bib/main.tex new file mode 100644 index 000000000..dfcb23ccb --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_bib/main.tex @@ -0,0 +1 @@ +\bibliography{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_tex/another.tex b/test/fixtures/unittest/03_core_cache/included_tex/another.tex new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/unittest/03_core_cache/included_tex/circular_1.tex b/test/fixtures/unittest/03_core_cache/included_tex/circular_1.tex new file mode 100644 index 000000000..885ed70db --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_tex/circular_1.tex @@ -0,0 +1 @@ +\input{circular_2} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_tex/circular_2.tex b/test/fixtures/unittest/03_core_cache/included_tex/circular_2.tex new file mode 100644 index 000000000..100ca310a --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_tex/circular_2.tex @@ -0,0 +1 @@ +\input{circular_1} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_tex/duplicate_1.tex b/test/fixtures/unittest/03_core_cache/included_tex/duplicate_1.tex new file mode 100644 index 000000000..8172548dd --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_tex/duplicate_1.tex @@ -0,0 +1,2 @@ +\input{duplicate_2} +\input{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_tex/duplicate_2.tex b/test/fixtures/unittest/03_core_cache/included_tex/duplicate_2.tex new file mode 100644 index 000000000..1629f02ad --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_tex/duplicate_2.tex @@ -0,0 +1 @@ +\input{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/included_tex/main.tex b/test/fixtures/unittest/03_core_cache/included_tex/main.tex new file mode 100644 index 000000000..ca5fd699c --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/included_tex/main.tex @@ -0,0 +1 @@ +\input{another} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/load_aux_file/main.aux b/test/fixtures/unittest/03_core_cache/load_aux_file/main.aux new file mode 100644 index 000000000..fcd4e7120 --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/load_aux_file/main.aux @@ -0,0 +1 @@ +\bibdata{main} \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/load_aux_file/main.bib b/test/fixtures/unittest/03_core_cache/load_aux_file/main.bib new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/unittest/03_core_cache/load_aux_file/main.fls b/test/fixtures/unittest/03_core_cache/load_aux_file/main.fls new file mode 100644 index 000000000..ae44b0e89 --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/load_aux_file/main.fls @@ -0,0 +1 @@ +OUTPUT ./main.aux \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/load_aux_file/main.tex b/test/fixtures/unittest/03_core_cache/load_aux_file/main.tex new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/unittest/03_core_cache/load_aux_file/nothing.aux b/test/fixtures/unittest/03_core_cache/load_aux_file/nothing.aux new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/unittest/03_core_cache/load_aux_file/nothing.fls b/test/fixtures/unittest/03_core_cache/load_aux_file/nothing.fls new file mode 100644 index 000000000..4ad77f7fb --- /dev/null +++ b/test/fixtures/unittest/03_core_cache/load_aux_file/nothing.fls @@ -0,0 +1 @@ +OUTPUT ./nothing.aux \ No newline at end of file diff --git a/test/fixtures/unittest/03_core_cache/load_aux_file/nothing.tex b/test/fixtures/unittest/03_core_cache/load_aux_file/nothing.tex new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/unittest/03_core_cache/load_fls_file/main.aux b/test/fixtures/unittest/03_core_cache/load_fls_file/main.aux new file mode 100644 index 000000000..e69de29bb diff --git a/test/units/03_core_cache.test.ts b/test/units/03_core_cache.test.ts index 9342f8da1..0b785a2c0 100644 --- a/test/units/03_core_cache.test.ts +++ b/test/units/03_core_cache.test.ts @@ -607,4 +607,121 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { assert.ok(!lw.watcher.src.has(getPath(fixture, 'load_fls_file', 'main.out'))) }) }) + + describe('lw.cache.parseAuxFile', () => { + it('should do nothing if no \\bibdata is found', async () => { + const toParse = getPath(fixture, 'load_aux_file', 'nothing.tex') + setRoot(fixture, 'load_aux_file', 'nothing.tex') + await lw.cache.refreshCache(toParse) + await lw.cache.loadFlsFile(toParse) + assert.strictEqual(lw.cache.get(toParse)?.bibfiles.size, 0) + }) + + it('should add \\bibdata from .aux file', async () => { + const toParse = getPath(fixture, 'load_aux_file', 'main.tex') + setRoot(fixture, 'load_aux_file', 'main.tex') + await lw.cache.refreshCache(toParse) + await lw.cache.loadFlsFile(toParse) + assert.strictEqual(lw.cache.get(toParse)?.bibfiles.size, 1) + }) + + it('should not add \\bibdata if the bib is excluded', async () => { + await setConfig('latex.watch.files.ignore', ['**/main.bib']) + const toParse = getPath(fixture, 'load_aux_file', 'main.tex') + setRoot(fixture, 'load_aux_file', 'main.tex') + await lw.cache.refreshCache(toParse) + await lw.cache.loadFlsFile(toParse) + assert.strictEqual(lw.cache.get(toParse)?.bibfiles.size, 0) + }) + + it('should watch bib files if added', async () => { + const toParse = getPath(fixture, 'load_aux_file', 'main.tex') + setRoot(fixture, 'load_aux_file', 'main.tex') + await lw.cache.refreshCache(toParse) + await lw.cache.loadFlsFile(toParse) + assert.ok(lw.watcher.bib.has(getPath(fixture, 'load_aux_file', 'main.bib'))) + }) + }) + + describe('lw.cache.getIncludedBib', () => { + it('should return an empty list if no file path is given', () => { + assert.strictEqual(lw.cache.getIncludedBib().length, 0) + }) + + it('should return an empty list if the given file is not cached', () => { + const toParse = getPath(fixture, 'included_bib', 'main.tex') + assert.strictEqual(lw.cache.getIncludedBib(toParse).length, 0) + }) + + it('should return a list of included .bib files', async () => { + const toParse = getPath(fixture, 'included_bib', 'main.tex') + await lw.cache.refreshCache(toParse) + assert.strictEqual(lw.cache.getIncludedBib(toParse).length, 1) + }) + + it('should return a list of included .bib files with \\input', async () => { + const toParse = getPath(fixture, 'included_bib', 'another.tex') + await lw.cache.refreshCache(toParse) + assert.strictEqual(lw.cache.getIncludedBib(toParse).length, 1) + }) + + it('should return a list of included .bib files with circular inclusions', async () => { + const toParse = getPath(fixture, 'included_bib', 'circular_1.tex') + await lw.cache.refreshCache(toParse) + assert.strictEqual(lw.cache.getIncludedBib(toParse).length, 1) + }) + + it('should return a list of de-duplicated .bib files', async () => { + const toParse = getPath(fixture, 'included_bib', 'duplicate_1.tex') + await lw.cache.refreshCache(toParse) + assert.strictEqual(lw.cache.getIncludedBib(toParse).length, 1) + }) + }) + + describe('lw.cache.getIncludedTeX', () => { + it('should return an empty list if no file path is given', () => { + assert.strictEqual(lw.cache.getIncludedTeX().length, 0) + }) + + it('should return an empty list if the given file is not cached', () => { + const toParse = getPath(fixture, 'included_tex', 'main.tex') + assert.strictEqual(lw.cache.getIncludedTeX(toParse).length, 0) + }) + + it('should return a list of included .tex files', async () => { + const toParse = getPath(fixture, 'included_tex', 'main.tex') + await lw.cache.refreshCache(toParse) + assert.strictEqual(lw.cache.getIncludedTeX(toParse).length, 2) + }) + + it('should return a list of included .tex files, but only cached ones with `cachedOnly` flag', async () => { + const toParse = getPath(fixture, 'included_tex', 'main.tex') + await lw.cache.refreshCache(toParse) + lw.cache._test.caches.delete(getPath(fixture, 'included_tex', 'another.tex')) + assert.strictEqual(lw.cache.getIncludedTeX(toParse, [], true).length, 1) + }) + + it('should return a list of included .bib files with circular inclusions', async () => { + const toParse = getPath(fixture, 'included_tex', 'circular_1.tex') + await lw.cache.refreshCache(toParse) + assert.strictEqual(lw.cache.getIncludedTeX(toParse).length, 2) + }) + + it('should return a list of de-duplicated .tex files', async () => { + const toParse = getPath(fixture, 'included_tex', 'duplicate_1.tex') + await lw.cache.refreshCache(toParse) + assert.strictEqual(lw.cache.getIncludedTeX(toParse).length, 3) + }) + }) + + describe('lw.cache.getFlsChildren', () => { + it('should return an empty list if no .fls is found', async () => { + assert.strictEqual((await lw.cache.getFlsChildren(texPathAnother)).length, 0) + }) + + it('should return a list of input files in the .fls file', async () => { + const toParse = getPath(fixture, 'load_fls_file', 'include_main.tex') + assert.strictEqual((await lw.cache.getFlsChildren(toParse)).length, 1) + }) + }) })