diff --git a/test/README.md b/test/README.md index ed3796006..5569f23c2 100644 --- a/test/README.md +++ b/test/README.md @@ -2,39 +2,21 @@ ## Executing tests -We start a new VS Code instance for each `fixture` directory in `fixtures/*/`, which includes a TeX file for tests, and execute an appropriate test defined in `*.test.ts` while skipping other tests not related to the directory. For tests of building a LaTeX file, we try to build a LaTeX file in the directory. If a PDF file is not generated, the test fails. With this approach, we can debug the extension by opening a TeX file in the `fixture` directory if the test fails. - -### How tests executed - -1. `runTest.ts` starts a new VS Code instance for each `fixture` directory and executes `*.index.ts`. -2. `*.index.ts` runs all the tests defined in `*.test.ts`. -3. Tests in `*.test.ts` are executed through `runTestWithFixture`. -4. `runTestWithFixture` skip tests in `*.test.ts` if they are not related to the current `fixture` directory. - -### Unit tests - -Unit tests are under the `unittests` directory. - ---- - -## New Tests - -We start a new VS Code instance for each `fixture` directory in `fixtures/*/`, which includes a series of TeX-related files for tests, and execute appropriate tests defined in `suites/*.test.ts` while skipping other tests not related to the directory. +We start a new VS Code instance for `testground`, `multiroot`, and `viewer` directories in `test/fixtures/`, which includes a series of TeX-related files for tests, and execute appropriate tests defined in `suites/*.test.ts` (`viewer.test.ts` for `viewer` fixture) while skipping other tests not related to the directory. For tests of building a LaTeX file, we try to build a LaTeX file in the directory. If a PDF file is not generated, the test fails. -With this approach, we can debug the extension by opening a TeX file in the `fixture` directory if the test fails. +The TeX files related are automatically created before the test and removed after. ### How tests are executed via CLI -1. `runTest.ts` starts a new VS Code instance for each `fixture` directory and executes `suites/index.ts`. -2. `index.ts` runs all the tests defined in `*.test.ts`. -3. Tests in `*.test.ts` are executed through `runTest` defined in `suites/utils.ts`, which skip tests in `*.test.ts` if they are not related to the current `fixture` directory. +1. `runTest.ts` starts a new VS Code instance for each `fixture` directory and executes `suites/index.ts` (`viewer.test.ts` for `viewer` fixture). +2. Tests in `*.test.ts` are executed through `runTest` defined in `suites/utils.ts`, which skip tests in `*.test.ts` if they are not related to the current `fixture` directory. ### How tests are executed via VS Code launch We have a `Run Tests` launch configuration in `.vscode/launch.json`. -In the config item, the first `args` passed to `code` defines the workspace to open, which is just the `fixture` directory to execute. -Additionally, the `LATEXWORKSHOP_CISUITE` envvar defines the suites to be executed, separated by commas and all if left empty. +In the config item, the first `args` passed to `code` defines the workspace to open: `testground` typically, and `multiroot/resource.code-workspace` for the multi-root workspace tests. +Additionally, the `LATEXWORKSHOP_SUITE` envvar defines the suites to be executed, separated by commas and all if left empty. ## Executing Tests on GitHub Actions diff --git a/test/suites/01_build.test.ts b/test/suites/01_build.test.ts index 9bec3ad56..ae6267757 100644 --- a/test/suites/01_build.test.ts +++ b/test/suites/01_build.test.ts @@ -1,23 +1,20 @@ import * as vscode from 'vscode' import * as path from 'path' import rimraf from 'rimraf' -import * as assert from 'assert' -import { Extension, activate } from '../../src/main' -import { assertBuild, runTest, waitBuild, writeTeX } from './utils' +import { Extension } from '../../src/main' +import { assertBuild, getExtension, runTest, waitBuild, writeTeX } from './utils' import { sleep } from '../utils/ciutils' suite('Build TeX files test suite', () => { - let extension: Extension | undefined + let extension: Extension const suiteName = path.basename(__filename).replace('.test.js', '') let fixture = path.resolve(__dirname, '../../../test/fixtures/testground') const fixtureName = 'testground' suiteSetup(async () => { - await vscode.commands.executeCommand('latex-workshop.activate') - extension = vscode.extensions.getExtension>('James-Yu.latex-workshop')?.exports.extension - assert.ok(extension) + extension = await getExtension() fixture = path.resolve(extension.extensionRoot, 'test/fixtures/testground') }) @@ -28,7 +25,6 @@ suite('Build TeX files test suite', () => { teardown(async () => { await vscode.commands.executeCommand('workbench.action.closeAllEditors') if (extension) { - extension.manager.invalidateCache() extension.manager.rootFile = undefined } @@ -42,8 +38,7 @@ suite('Build TeX files test suite', () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.exclude', undefined) if (path.basename(fixture) === 'testground') { - await sleep(250) - rimraf(fixture + '/*', (e) => {if (e) {console.error(e)}}) + rimraf(fixture + '/{*,.vscode}', (e) => {if (e) {console.error(e)}}) await sleep(500) // Required for pooling } }) @@ -67,14 +62,14 @@ suite('Build TeX files test suite', () => { await assertBuild({fixture, texFileName: 'main.tex', pdfFileName: 'main.pdf', extension}) }) - runTest({suiteName, fixtureName, testName: 'auto-detect subfile root 1'}, async () => { + runTest({suiteName, fixtureName, testName: 'auto-detect subfile root and build 1'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', true) await writeTeX('subfile', fixture) await assertBuild({fixture, texFileName: 'sub/s.tex', pdfFileName: 'sub/s.pdf', extension}) }) - runTest({suiteName, fixtureName, testName: 'auto-detect subfile root 2'}, async () => { + runTest({suiteName, fixtureName, testName: 'auto-detect subfile root and build 2'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', false) await writeTeX('subfile', fixture) @@ -87,20 +82,6 @@ suite('Build TeX files test suite', () => { await assertBuild({fixture, texFileName: 'main.tex', pdfFileName: 'out/main.pdf', extension}) }) - runTest({suiteName, fixtureName, testName: 'detect root with search.rootFiles.include'}, async () => { - await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) - await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.include', ['alt/*.tex']) - await writeTeX('subfiletwomain', fixture) - await assertBuild({fixture, texFileName: 'sub/s.tex', pdfFileName: 'alt/main.pdf', extension}) - }) - - runTest({suiteName, fixtureName, testName: 'detect root with search.rootFiles.exclude'}, async () => { - await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) - await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.exclude', ['*.tex']) - await writeTeX('subfiletwomain', fixture) - await assertBuild({fixture, texFileName: 'sub/s.tex', pdfFileName: 'alt/main.pdf', extension}) - }) - runTest({suiteName, fixtureName, testName: 'basic build with spaces in names'}, async () => { await writeTeX('main', fixture, {fileName: 'main space/main.tex'}) await assertBuild({fixture, texFileName: 'main space/main.tex', pdfFileName: 'main space/main.pdf', extension}) @@ -112,11 +93,6 @@ suite('Build TeX files test suite', () => { await assertBuild({fixture, texFileName: 'main.tex', pdfFileName: 'out space/main.pdf', extension}) }) - runTest({suiteName, fixtureName, testName: 'auto-detect root with verbatim'}, async () => { - await writeTeX('subfileverbatim', fixture) - await assertBuild({fixture, texFileName: 'sub/s.tex', pdfFileName: 'main.pdf', extension}) - }) - runTest({suiteName, fixtureName, testName: 'build with magic comment'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.recipes', []) await vscode.workspace.getConfiguration('latex-workshop').update('latex.build.forceRecipeUsage', false) diff --git a/test/suites/02_autobuild.test.ts b/test/suites/02_autobuild.test.ts index 3d77d9c18..c0a6b8a3b 100644 --- a/test/suites/02_autobuild.test.ts +++ b/test/suites/02_autobuild.test.ts @@ -1,23 +1,20 @@ import * as vscode from 'vscode' import * as path from 'path' -import * as assert from 'assert' import rimraf from 'rimraf' -import { Extension, activate } from '../../src/main' -import { assertAutoBuild, assertBuild, runTest, writeTeX } from './utils' +import { Extension } from '../../src/main' +import { assertAutoBuild, assertBuild, getExtension, runTest, writeTeX } from './utils' import { sleep } from '../utils/ciutils' suite('Auto-build test suite', () => { - let extension: Extension | undefined + let extension: Extension const suiteName = path.basename(__filename).replace('.test.js', '') let fixture = path.resolve(__dirname, '../../../test/fixtures/testground') const fixtureName = 'testground' suiteSetup(async () => { - await vscode.commands.executeCommand('latex-workshop.activate') - extension = vscode.extensions.getExtension>('James-Yu.latex-workshop')?.exports.extension - assert.ok(extension) + extension = await getExtension() fixture = path.resolve(extension.extensionRoot, 'test/fixtures/testground') }) @@ -30,7 +27,6 @@ suite('Auto-build test suite', () => { teardown(async () => { await vscode.commands.executeCommand('workbench.action.closeAllEditors') if (extension) { - extension.manager.invalidateCache() extension.manager.rootFile = undefined } @@ -43,8 +39,7 @@ suite('Auto-build test suite', () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.watch.files.ignore', undefined) if (path.basename(fixture) === 'testground') { - await sleep(250) - rimraf(fixture + '/*', (e) => {if (e) {console.error(e)}}) + rimraf(fixture + '/{*,.vscode}', (e) => {if (e) {console.error(e)}}) await sleep(500) // Required for pooling } }) diff --git a/test/suites/03_findroot.test.ts b/test/suites/03_findroot.test.ts index ec6d365f6..4ba7a280f 100644 --- a/test/suites/03_findroot.test.ts +++ b/test/suites/03_findroot.test.ts @@ -3,21 +3,19 @@ import * as path from 'path' import rimraf from 'rimraf' import * as assert from 'assert' -import { Extension, activate } from '../../src/main' -import { runTest, writeTeX } from './utils' +import { Extension } from '../../src/main' +import { assertRoot, getExtension, runTest, writeTeX } from './utils' import { sleep } from '../utils/ciutils' suite('Find root file test suite', () => { - let extension: Extension | undefined + let extension: Extension const suiteName = path.basename(__filename).replace('.test.js', '') let fixture = path.resolve(__dirname, '../../../test/fixtures/testground') const fixtureName = 'testground' suiteSetup(async () => { - await vscode.commands.executeCommand('latex-workshop.activate') - extension = vscode.extensions.getExtension>('James-Yu.latex-workshop')?.exports.extension - assert.ok(extension) + extension = await getExtension() fixture = path.resolve(extension.extensionRoot, 'test/fixtures/testground') }) @@ -33,27 +31,45 @@ suite('Find root file test suite', () => { } if (path.basename(fixture) === 'testground') { - await sleep(250) - rimraf(fixture + '/*', (e) => {if (e) {console.error(e)}}) + rimraf(fixture + '/{*,.vscode}', (e) => {if (e) {console.error(e)}}) await sleep(500) // Required for pooling } }) + + runTest({suiteName, fixtureName, testName: 'detect root with search.rootFiles.include'}, async () => { + await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) + await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.include', ['alt/*.tex']) + await writeTeX('subfiletwomain', fixture) + assert.ok(extension) + await assertRoot({fixture, openName: 'sub/s.tex', rootName: 'alt/main.tex', extension}) + }) + + runTest({suiteName, fixtureName, testName: 'detect root with search.rootFiles.exclude'}, async () => { + await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) + await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.exclude', ['*.tex']) + await writeTeX('subfiletwomain', fixture) + assert.ok(extension) + await assertRoot({fixture, openName: 'sub/s.tex', rootName: 'alt/main.tex', extension}) + }) + + runTest({suiteName, fixtureName, testName: 'auto-detect root with verbatim'}, async () => { + await writeTeX('subfileverbatim', fixture) + assert.ok(extension) + await assertRoot({fixture, openName: 'sub/s.tex', rootName: 'main.tex', extension}) + }) + runTest({suiteName, fixtureName, testName: 'import package'}, async () => { await writeTeX('importthreelayer', fixture) - const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(path.resolve(fixture, 'sub/subsub/sss/sss.tex'))) - await vscode.window.showTextDocument(doc) - const root = await extension?.manager.findRoot() - assert.strictEqual(root, path.resolve(fixture, 'main.tex')) + assert.ok(extension) + await assertRoot({fixture, openName: 'sub/subsub/sss/sss.tex', rootName: 'main.tex', extension}) }) runTest({suiteName, fixtureName, testName: 'circular inclusion'}, async () => { await writeTeX('circularinclude', fixture) - const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(path.resolve(fixture, 'alt.tex'))) - await vscode.window.showTextDocument(doc) - const root = await extension?.manager.findRoot() - assert.strictEqual(root, path.join(fixture, 'main.tex')) - const includedTeX = extension?.manager.getIncludedTeX() + assert.ok(extension) + await assertRoot({fixture, openName: 'alt.tex', rootName: 'main.tex', extension}) + const includedTeX = extension.manager.getIncludedTeX() assert.ok(includedTeX) assert.ok( includedTeX.includes(path.resolve(fixture, 'main.tex')) && diff --git a/test/suites/04_intellisense.test.ts b/test/suites/04_intellisense.test.ts index 442aa10c2..cb085546a 100644 --- a/test/suites/04_intellisense.test.ts +++ b/test/suites/04_intellisense.test.ts @@ -3,21 +3,19 @@ import * as path from 'path' import rimraf from 'rimraf' import * as assert from 'assert' -import { Extension, activate } from '../../src/main' -import { getIntellisense, runTest, writeTeX } from './utils' +import { Extension } from '../../src/main' +import { getExtension, getIntellisense, runTest, writeTeX } from './utils' import { sleep } from '../utils/ciutils' suite('Intellisense test suite', () => { - let extension: Extension | undefined + let extension: Extension const suiteName = path.basename(__filename).replace('.test.js', '') let fixture = path.resolve(__dirname, '../../../test/fixtures/testground') const fixtureName = 'testground' suiteSetup(async () => { - await vscode.commands.executeCommand('latex-workshop.activate') - extension = vscode.extensions.getExtension>('James-Yu.latex-workshop')?.exports.extension - assert.ok(extension) + extension = await getExtension() fixture = path.resolve(extension.extensionRoot, 'test/fixtures/testground') }) @@ -36,8 +34,7 @@ suite('Intellisense test suite', () => { await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.atSuggestionJSON.replace', undefined) if (path.basename(fixture) === 'testground') { - await sleep(250) - rimraf(fixture + '/*', (e) => {if (e) {console.error(e)}}) + rimraf(fixture + '/{*,.vscode}', (e) => {if (e) {console.error(e)}}) await sleep(500) // Required for pooling } }) diff --git a/test/suites/05_multiroot.test.ts b/test/suites/05_multiroot.test.ts index c9061c5c2..6bbc6f954 100644 --- a/test/suites/05_multiroot.test.ts +++ b/test/suites/05_multiroot.test.ts @@ -3,22 +3,20 @@ import * as path from 'path' import rimraf from 'rimraf' import * as assert from 'assert' -import { Extension, activate } from '../../src/main' -import { runTest, writeTeX, assertBuild, touch, assertAutoBuild, writeTestFile, copyTestFile, getIntellisense } from './utils' +import { Extension } from '../../src/main' +import { runTest, writeTeX, assertBuild, touch, assertAutoBuild, writeTestFile, copyTestFile, getIntellisense, assertRoot, getExtension } from './utils' import { sleep } from '../utils/ciutils' suite('Multi-root workspace test suite', () => { - let extension: Extension | undefined + let extension: Extension const suiteName = path.basename(__filename).replace('.test.js', '') let fixture = path.resolve(__dirname, '../../../test/fixtures/multiroot') const fixtureName = 'multiroot' suiteSetup(async () => { - await vscode.commands.executeCommand('latex-workshop.activate') - extension = vscode.extensions.getExtension>('James-Yu.latex-workshop')?.exports.extension - assert.ok(extension) - fixture = path.resolve(extension.extensionRoot, 'test/fixtures/multiroot') + extension = await getExtension() + fixture = path.resolve(extension.extensionRoot, 'test/fixtures/testground') }) setup(async () => { @@ -46,7 +44,6 @@ suite('Multi-root workspace test suite', () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.autoBuild.interval', undefined) if (path.basename(fixture) === 'multiroot') { - await sleep(250) rimraf(fixture + '/{A,B}/{*,.vscode}', (e) => {if (e) {console.error(e)}}) await sleep(500) // Required for pooling } @@ -104,7 +101,8 @@ suite('Multi-root workspace test suite', () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.include', ['alt/*.tex']) await writeTeX('subfiletwomain', fixture, {fileDir: 'A/'}) touch(path.resolve(fixture, 'B', 'empty')) - await assertBuild({fixture, texFileName: 'A/sub/s.tex', pdfFileName: 'A/alt/main.pdf', extension}) + assert.ok(extension) + await assertRoot({fixture, openName: 'A/sub/s.tex', rootName: 'A/alt/main.tex', extension}) }) runTest({suiteName, fixtureName, testName: 'detect root with search.rootFiles.exclude'}, async () => { @@ -112,10 +110,11 @@ suite('Multi-root workspace test suite', () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.exclude', ['*.tex']) await writeTeX('subfiletwomain', fixture, {fileDir: 'A/'}) touch(path.resolve(fixture, 'B', 'empty')) - await assertBuild({fixture, texFileName: 'A/sub/s.tex', pdfFileName: 'A/alt/main.pdf', extension}) + assert.ok(extension) + await assertRoot({fixture, openName: 'A/sub/s.tex', rootName: 'A/alt/main.tex', extension}) }) - runTest({suiteName, fixtureName, testName: 'auto-detect subfile root A1'}, async () => { + runTest({suiteName, fixtureName, testName: 'auto-detect subfile root and build A1'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', true) await writeTeX('subfile', fixture, {fileDir: 'A/'}) @@ -123,7 +122,7 @@ suite('Multi-root workspace test suite', () => { await assertBuild({fixture, texFileName: 'A/sub/s.tex', pdfFileName: 'A/sub/s.pdf', extension}) }) - runTest({suiteName, fixtureName, testName: 'auto-detect subfile root A2'}, async () => { + runTest({suiteName, fixtureName, testName: 'auto-detect subfile root and build A2'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', false) await writeTeX('subfile', fixture, {fileDir: 'A/'}) @@ -131,7 +130,7 @@ suite('Multi-root workspace test suite', () => { await assertBuild({fixture, texFileName: 'A/sub/s.tex', pdfFileName: 'A/main.pdf', extension}) }) - runTest({suiteName, fixtureName, testName: 'auto-detect subfile root B1'}, async () => { + runTest({suiteName, fixtureName, testName: 'auto-detect subfile root and build B1'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', true) await writeTeX('subfile', fixture, {fileDir: 'B/'}) @@ -139,7 +138,7 @@ suite('Multi-root workspace test suite', () => { await assertBuild({fixture, texFileName: 'B/sub/s.tex', pdfFileName: 'B/sub/s.pdf', extension}) }) - runTest({suiteName, fixtureName, testName: 'auto-detect subfile root B2'}, async () => { + runTest({suiteName, fixtureName, testName: 'auto-detect subfile root and build B2'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', false) await writeTeX('subfile', fixture, {fileDir: 'B/'}) @@ -156,7 +155,7 @@ suite('Multi-root workspace test suite', () => { await assertAutoBuild({fixture, texFileName: 'A/sub/s.tex', pdfFileName: 'A/main.pdf', extension}, ['onSave']) }) - runTest({only: true, suiteName, fixtureName, testName: 'auto build with subfiles and onSave 2'}, async () => { + runTest({suiteName, fixtureName, testName: 'auto build with subfiles and onSave 2'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('latex.autoBuild.run', 'onSave') await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', true) await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', true) @@ -165,15 +164,16 @@ suite('Multi-root workspace test suite', () => { await assertAutoBuild({fixture, texFileName: 'A/sub/s.tex', pdfFileName: 'A/sub/s.pdf', extension}, ['onSave']) }) - runTest({only: true, suiteName, fixtureName, testName: 'switching rootFile'}, async () => { + runTest({suiteName, fixtureName, testName: 'switching rootFile'}, async () => { await writeTeX('main', fixture, {fileName: 'A/main.tex'}) await writeTeX('main', fixture, {fileName: 'B/main.tex'}) - await assertBuild({fixture, texFileName: 'A/main.tex', pdfFileName: 'A/main.pdf', extension, removepdf: true}) - await assertBuild({fixture, texFileName: 'B/main.tex', pdfFileName: 'B/main.pdf', extension, removepdf: true}) - await assertBuild({fixture, texFileName: 'A/main.tex', pdfFileName: 'A/main.pdf', extension}) + assert.ok(extension) + await assertRoot({fixture, openName: 'A/main.tex', rootName: 'A/main.tex', extension}) + await assertRoot({fixture, openName: 'B/main.tex', rootName: 'B/main.tex', extension}) + await assertRoot({fixture, openName: 'A/main.tex', rootName: 'A/main.tex', extension}) }) - runTest({only: true, suiteName, fixtureName, testName: 'switching intellisense'}, async () => { + runTest({suiteName, fixtureName, testName: 'switching intellisense'}, async () => { await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.citation.label', 'bibtex key') writeTestFile({fixture, fileName: 'A/main.tex'}, '\\documentclass{article}', '\\begin{document}', 'abc\\cite{}', '\\bibliography{A.bib}', '\\end{document}') writeTestFile({fixture, fileName: 'B/main.tex'}, '\\documentclass{article}', '\\begin{document}', 'abc\\cite{}', '\\bibliography{B.bib}', '\\end{document}') diff --git a/test/suites/utils.ts b/test/suites/utils.ts index d7bf8b52d..3d224f6dd 100644 --- a/test/suites/utils.ts +++ b/test/suites/utils.ts @@ -5,9 +5,16 @@ import * as glob from 'glob' import * as os from 'os' import * as assert from 'assert' -import { Extension } from '../../src/main' +import { Extension, activate } from '../../src/main' import { BuildFinished } from '../../src/components/eventbus' +export async function getExtension() { + await vscode.commands.executeCommand('latex-workshop.activate') + const extension = vscode.extensions.getExtension>('James-Yu.latex-workshop')?.exports.extension + assert.ok(extension) + return extension +} + export function touch(filePath: string) { fs.closeSync(fs.openSync(filePath, 'a')) } @@ -99,7 +106,7 @@ type AssertBuildOption = { fixture: string, texFileName: string, pdfFileName: string, - extension?: Extension, + extension: Extension, build?: () => unknown, nobuild?: boolean, removepdf?: boolean @@ -110,15 +117,17 @@ export async function assertBuild(option: AssertBuildOption) { const pdfFilePath = path.join(option.fixture, option.pdfFileName) const doc = await vscode.workspace.openTextDocument(texFilePath) await vscode.window.showTextDocument(doc) - await option.extension?.manager.findRoot() + await option.extension.manager.findRoot() if (option.build) { await option.build() } else { - await vscode.commands.executeCommand('latex-workshop.build') + await option.extension.commander.build() } const files = glob.sync('**/**.pdf', { cwd: option.fixture }) assert.strictEqual(files.map(file => path.resolve(option.fixture, file)).join(','), option.pdfFileName === '' ? option.pdfFileName : pdfFilePath) + await sleep(500) // Wait for post-build processing + if (option.removepdf) { files.forEach(async file => { if (fs.existsSync(path.join(option.fixture, file))) { @@ -155,17 +164,35 @@ export async function assertAutoBuild(option: AssertBuildOption, mode?: ('skipFi files = glob.sync('**/**.pdf', { cwd: option.fixture }) assert.strictEqual(files.map(file => path.resolve(option.fixture, file)).join(','), path.resolve(option.fixture, option.pdfFileName)) } + await sleep(500) // Wait for post-build processing } -export async function waitBuild(extension?: Extension) { +export async function waitBuild(extension: Extension) { return new Promise((resolve, _) => { - const disposable = extension?.eventBus.on(BuildFinished, () => { + const disposable = extension.eventBus.on(BuildFinished, () => { resolve() disposable?.dispose() }) }) } +type AssertRootOption = { + fixture: string, + openName: string, + rootName: string, + extension: Extension +} + +export async function assertRoot(option: AssertRootOption) { + await vscode.commands.executeCommand('latex-workshop.activate') + const openFilePath = vscode.Uri.file(path.join(option.fixture, option.openName)) + const rootFilePath = path.join(option.fixture, option.rootName) + const doc = await vscode.workspace.openTextDocument(openFilePath) + await vscode.window.showTextDocument(doc) + const root = await option.extension.manager.findRoot() + assert.strictEqual(root, rootFilePath) +} + type WriteTeXType = 'main' | 'makeindex' | 'makesubfileindex' | 'magicprogram' | 'magicoption' | 'magicroot' | 'magicinvalidprogram' | 'subfile' | 'subfileverbatim' | 'subfiletwomain' | 'subfilethreelayer' | 'importthreelayer' | 'bibtex' | 'input' | 'inputmacro' | 'inputfromfolder' | 'circularinclude' | 'intellisense' | 'structure' | 'linter' @@ -263,8 +290,8 @@ export async function writeTeX(type: WriteTeXType, fixture: string, payload?: {f await sleep(250) } -export function getIntellisense(doc: vscode.TextDocument, pos: vscode.Position, extension?: Extension, atSuggestion = false) { - const completer = atSuggestion ? extension?.atSuggestionCompleter : extension?.completer +export function getIntellisense(doc: vscode.TextDocument, pos: vscode.Position, extension: Extension, atSuggestion = false) { + const completer = atSuggestion ? extension.atSuggestionCompleter : extension.completer return completer?.provideCompletionItems( doc, pos, new vscode.CancellationTokenSource().token, { triggerKind: vscode.CompletionTriggerKind.Invoke,