Skip to content

Commit

Permalink
Add viewer tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Mar 13, 2020
1 parent c188e65 commit 933f095
Show file tree
Hide file tree
Showing 27 changed files with 378 additions and 46 deletions.
8 changes: 8 additions & 0 deletions src/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,14 @@ export class Commander {
vscode.workspace.openTextDocument({content: JSON.stringify(ast, null, 2), language: 'json'}).then(doc => vscode.window.showTextDocument(doc))
}

async getViewerStatus(pdfFilePath: string) {
return this.extension.viewer.getViewerStatus(pdfFilePath)
}

isBuildFinished(): boolean {
return this.extension.builder.isBuildFinished()
}

texdoc(pkg?: string) {
this._texdoc.texdoc(pkg)
}
Expand Down
7 changes: 7 additions & 0 deletions src/components/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export class Builder {
}
}

/**
* Should not use. Only for integration tests.
*/
isBuildFinished(): boolean {
return this.buildMutex.count === 1
}

isWaitingForBuildToFinish(): boolean {
return this.waitingForBuildToFinishMutex.count < 1
}
Expand Down
40 changes: 40 additions & 0 deletions src/components/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs'
import * as ws from 'ws'
import * as path from 'path'
import * as cp from 'child_process'
import {sleep} from '../utils/utils'

import {Extension} from '../main'
import {SyncTeXRecordForward} from './locator'
Expand All @@ -24,14 +25,24 @@ class Client {
}
}

export type ViewerStatus = {
path: string,
scrollTop: number
}

export class Viewer {
extension: Extension
clients: {[key: string]: Client[]} = {}
statusMessageQueue: Map<string, ViewerStatus[]> = new Map()

constructor(extension: Extension) {
this.extension = extension
}

getClients(pdfFilePath: string): Client[] | undefined {
return this.clients[pdfFilePath.toLocaleUpperCase()]
}

refreshExistingViewer(sourceFile?: string, viewer?: string): boolean {
if (!sourceFile) {
Object.keys(this.clients).forEach(key => {
Expand Down Expand Up @@ -276,6 +287,14 @@ export class Viewer {
// nothing to do
break
}
case 'status': {
const results = this.statusMessageQueue.get(data.path)
if (!results) {
break
}
results.push({ path: data.path, scrollTop: data.scrollTop })
break
}
default: {
this.extension.logger.addLogMessage(`Unknown websocket message: ${msg}`)
break
Expand All @@ -294,4 +313,25 @@ export class Viewer {
this.extension.logger.addLogMessage(`Try to synctex ${pdfFile}`)
}
}

async getViewerStatus(pdfFilePath: string): Promise<ViewerStatus[]> {
const clients = this.getClients(pdfFilePath)
if (clients === undefined || clients.length === 0) {
return []
}
this.statusMessageQueue.set(pdfFilePath, [])
for (const client of clients) {
client.send({type: 'request_status'})
}
for (let i = 0; i < 100; i++) {
const results = this.statusMessageQueue.get(pdfFilePath)
if (results && results.length > 0) {
this.statusMessageQueue.delete(pdfFilePath)
return results
}
await sleep(100)
}
throw new Error('Cannot get viewer status.')
}

}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand('latex-workshop-dev.parselog', () => extension.commander.devParseLog())
vscode.commands.registerCommand('latex-workshop-dev.parsetex', () => extension.commander.devParseTeX())
vscode.commands.registerCommand('latex-workshop-dev.parsebib', () => extension.commander.devParseBib())
vscode.commands.registerCommand('latex-workshop-dev.getViewerStatus', (pdfFilePath: string) => extension.commander.getViewerStatus(pdfFilePath))
vscode.commands.registerCommand('latex-workshop-dev.isBuildFinished', () => extension.commander.isBuildFinished())

vscode.commands.registerCommand('latex-workshop.shortcut.item', () => extension.commander.insertSnippet('item'))
vscode.commands.registerCommand('latex-workshop.shortcut.emph', () => extension.commander.toggleSelectedKeyword('emph'))
Expand Down
4 changes: 4 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import * as path from 'path'
import * as fs from 'fs'
import * as iconv from 'iconv-lite'

export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}

export function escapeRegExp(str: string) {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.ts → test/build.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function run(): Promise<void> {
const testsRoot = path.resolve(__dirname, '.')

return new Promise((resolve, reject) => {
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
glob('**/build.test.js', { cwd: testsRoot }, (err, files) => {
if (err) {
return reject(err)
}
Expand Down
32 changes: 21 additions & 11 deletions test/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import * as process from 'process'
import * as vscode from 'vscode'
import {
assertPdfIsGenerated,
execCommandThenPick,
getFixtureDir,
isDockerEnabled,
runTestWithFixture,
sleep,
waitReleaseOf
waitReleaseOf,
waitlLatexWorkshopActivated
} from './utils'
import {sleep} from '../src/utils/utils'

suite('Buid TeX files test suite', () => {

Expand Down Expand Up @@ -157,7 +159,7 @@ suite('Buid TeX files test suite', () => {
const texFilePath = vscode.Uri.file(path.join(fixtureDir, 'sub', texFileName))
const doc = await vscode.workspace.openTextDocument(texFilePath)
const editor = await vscode.window.showTextDocument(doc)
await sleep(5000)
await waitlLatexWorkshopActivated()
await editor.edit((builder) => {
builder.insert(new vscode.Position(2, 0), ' ')
})
Expand Down Expand Up @@ -418,14 +420,19 @@ suite('Buid TeX files test suite', () => {
const texFilePath = vscode.Uri.file(path.join(fixtureDir, 'sub', texFileName))
const doc = await vscode.workspace.openTextDocument(texFilePath)
await vscode.window.showTextDocument(doc)
setTimeout(() => {
vscode.commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem')
let done = false
setTimeout(async () => {
while (!done) {
await vscode.commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem')
await sleep(3000)
}
}, 3000)
await vscode.commands.executeCommand('latex-workshop.build')
done = true
})
})

runTestWithFixture('fixture05A', 'build s.tex choosing an item in QuickPick', async () => {
runTestWithFixture('fixture05a', 'build s.tex choosing an item in QuickPick', async () => {
const fixtureDir = getFixtureDir()
const texFileName = 's.tex'
const pdfFileName = 's.pdf'
Expand All @@ -434,11 +441,14 @@ suite('Buid TeX files test suite', () => {
const texFilePath = vscode.Uri.file(path.join(fixtureDir, 'sub', texFileName))
const doc = await vscode.workspace.openTextDocument(texFilePath)
await vscode.window.showTextDocument(doc)
setTimeout(async () => {
await vscode.commands.executeCommand('workbench.action.quickOpenSelectNext')
await vscode.commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem')
}, 3000)
await vscode.commands.executeCommand('latex-workshop.build')
await waitlLatexWorkshopActivated()
execCommandThenPick(
() => vscode.commands.executeCommand('latex-workshop.build'),
async () => {
await vscode.commands.executeCommand('workbench.action.quickOpenSelectNext')
await vscode.commands.executeCommand('workbench.action.acceptSelectedQuickOpenItem')
}
)
})
}, () => isDockerEnabled())

Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/viewer/fixture001/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"latex-workshop.view.pdf.viewer": "tab"
}
4 changes: 4 additions & 0 deletions test/fixtures/viewer/fixture001/t.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\documentclass{article}
\begin{document}
abc
\end{document}
4 changes: 4 additions & 0 deletions test/fixtures/viewer/fixture002/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"latex-workshop.latex.rootFile.doNotPrompt": true,
"latex-workshop.view.pdf.viewer": "tab"
}
6 changes: 6 additions & 0 deletions test/fixtures/viewer/fixture002/main.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\documentclass{article}
\usepackage{subfiles}
\begin{document}
main main main
\subfile{sub/s}
\end{document}
4 changes: 4 additions & 0 deletions test/fixtures/viewer/fixture002/sub/s.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\documentclass[../main.tex]{subfiles}
\begin{document}
sub sub sub
\end{document}
5 changes: 5 additions & 0 deletions test/fixtures/viewer/fixture003/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"latex-workshop.latex.rootFile.doNotPrompt": true,
"latex-workshop.latex.rootFile.useSubFile": false,
"latex-workshop.view.pdf.viewer": "tab"
}
6 changes: 6 additions & 0 deletions test/fixtures/viewer/fixture003/main.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\documentclass{article}
\usepackage{subfiles}
\begin{document}
main main main
\subfile{sub/s}
\end{document}
4 changes: 4 additions & 0 deletions test/fixtures/viewer/fixture003/sub/s.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\documentclass[../main.tex]{subfiles}
\begin{document}
sub sub sub
\end{document}
3 changes: 3 additions & 0 deletions test/fixtures/viewer/fixture004/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"latex-workshop.view.pdf.viewer": "tab"
}
6 changes: 6 additions & 0 deletions test/fixtures/viewer/fixture004/main.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
\documentclass{article}
\usepackage{subfiles}
\begin{document}
main main main
\subfile{sub/s}
\end{document}
4 changes: 4 additions & 0 deletions test/fixtures/viewer/fixture004/sub/s.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\documentclass[../main.tex]{subfiles}
\begin{document}
sub sub sub
\end{document}
62 changes: 33 additions & 29 deletions test/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,42 @@ import * as glob from 'glob'
import * as tmpFile from 'tmp'
import { runTests } from 'vscode-test'

async function main() {
try {
const extensionDevelopmentPath = path.resolve(__dirname, '../../')
const extensionTestsPath = path.resolve(__dirname, './index')
const tmpdir = tmpFile.dirSync({ unsafeCleanup: true })
const fixtures = glob.sync('test/fixtures/build/*', { cwd: extensionDevelopmentPath })
async function runTestsOnEachFixture(targetName: 'build' | 'viewer') {
const extensionDevelopmentPath = path.resolve(__dirname, '../../')
const extensionTestsPath = path.resolve(__dirname, `./${targetName}.index`)
const tmpdir = tmpFile.dirSync({ unsafeCleanup: true })
const fixtures = glob.sync(`test/fixtures/${targetName}/*`, { cwd: extensionDevelopmentPath })

let testBuildWorkspaces: string[] = []
const fixturePatterns = process.argv.slice(2).filter(s => !/^-/.exec(s))
if (fixturePatterns.length === 0) {
testBuildWorkspaces = fixtures
} else {
testBuildWorkspaces = fixtures.filter( fixture => {
return fixturePatterns.some( pat => fixture.includes(pat) )
})
}
let testBuildWorkspaces: string[] = []
const fixturePatterns = process.argv.slice(2).filter(s => !/^-/.exec(s))
if (fixturePatterns.length === 0) {
testBuildWorkspaces = fixtures
} else {
testBuildWorkspaces = fixtures.filter( fixture => {
return fixturePatterns.some( pat => fixture.includes(pat) )
})
}

for (const testWorkspace of testBuildWorkspaces) {
await runTests({
version: '1.42.1',
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [
testWorkspace,
'--user-data-dir=' + tmpdir.name,
'--disable-extensions',
'--disable-gpu'
],
extensionTestsEnv: { LATEXWORKSHOP_CI_ENABLE_DOCKER: process.argv.includes('--enable-docker') ? '1' : undefined }
})
}
for (const testWorkspace of testBuildWorkspaces) {
await runTests({
version: '1.42.1',
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [
testWorkspace,
'--user-data-dir=' + tmpdir.name,
'--disable-extensions',
'--disable-gpu'
],
extensionTestsEnv: { LATEXWORKSHOP_CI_ENABLE_DOCKER: process.argv.includes('--enable-docker') ? '1' : undefined }
})
}
}

async function main() {
try {
await runTestsOnEachFixture('build')
await runTestsOnEachFixture('viewer')
} catch (err) {
console.error('Failed to run tests')
process.exit(1)
Expand Down
Loading

0 comments on commit 933f095

Please sign in to comment.