Skip to content

Commit

Permalink
Maximal sinon stub for unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Yu committed Sep 27, 2024
1 parent 726cd17 commit 58bd90d
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/completion/completer/citation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function updateAll(bibFiles?: string[]): CitationItem[] {
async function parseBibFile(fileName: string) {
logger.log(`Parsing .bib entries from ${fileName}`)
const configuration = vscode.workspace.getConfiguration('latex-workshop', vscode.Uri.file(fileName))
if (fs.statSync(fileName).size >= (configuration.get('bibtex.maxFileSize') as number) * 1024 * 1024) {
if ((await lw.external.stat(vscode.Uri.file(fileName))).size >= (configuration.get('bibtex.maxFileSize') as number) * 1024 * 1024) {
logger.log(`Bib file is too large, ignoring it: ${fileName}`)
data.bibEntries.delete(fileName)
return
Expand Down
2 changes: 1 addition & 1 deletion test/units/01_core_file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
const fixture = path.basename(__filename).split('.')[0]

before(() => {
mock.init(lw, 'file')
mock.init(lw)
})

after(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/units/02_core_watcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
const getOnDeleteHandlers = () => _onDeleteHandlersSpy.call(lw.watcher.src) as Set<(uri: vscode.Uri) => void>

before(() => {
mock.init(lw, 'file', 'watcher')
mock.init(lw, 'watcher')
_onDidChangeSpy = sinon.spy(lw.watcher.src as any, 'onDidChange')
_onDidDeleteSpy = sinon.spy(lw.watcher.src as any, 'onDidDelete')
_watchersSpy = sinon.spy(lw.watcher.src as any, 'watchers', ['get']).get
Expand Down
2 changes: 1 addition & 1 deletion test/units/03_core_cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
const fixture = path.basename(__filename).split('.')[0]

before(() => {
mock.init(lw, 'file', 'watcher', 'cache')
mock.init(lw, 'watcher', 'cache')
})

after(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/units/04_core_root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
const fixture = path.basename(__filename).split('.')[0]

before(() => {
mock.init(lw, 'file', 'watcher', 'cache', 'root')
mock.init(lw, 'watcher', 'cache', 'root')
})

after(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/units/05_compile_queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { queue } from '../../src/compile/queue'

describe(path.basename(__filename).split('.')[0] + ':', () => {
before(() => {
mock.init(lw, 'file', 'root')
mock.init(lw)
})

after(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/units/06_compile_recipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
let mkdirStub: sinon.SinonStub

before(() => {
mock.init(lw, 'file', 'root')
mock.init(lw)
getOutDirStub = sinon.stub(lw.file, 'getOutDir').returns('.')
getIncludedTeXStub = lw.cache.getIncludedTeX as sinon.SinonStub
mkdirStub = sinon.stub(lw.external, 'mkdirSync').returns(undefined)
Expand Down
2 changes: 1 addition & 1 deletion test/units/07_compile_external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ExternalStep } from '../../src/types'

describe(path.basename(__filename).split('.')[0] + ':', () => {
before(() => {
mock.init(lw, 'file', 'root')
mock.init(lw)
})

after(() => {
Expand Down
7 changes: 5 additions & 2 deletions test/units/08_compile_build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
let findStub: sinon.SinonStub

before(() => {
mock.init(lw, 'file', 'root')
mock.init(lw)
;(lw.cache.getIncludedTeX as sinon.SinonStub).returns([get.path('main.tex')])
findStub = sinon.stub(lw.root, 'find')
findStub = lw.root.find as sinon.SinonStub
;(lw.extra.clean as sinon.SinonStub).resolves(Promise.resolve())
})

Expand Down Expand Up @@ -529,6 +529,8 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {

it('should auto-build when watched bib file is changed', () => {
set.config('latex.autoBuild.run', 'onFileChange')
// Prevent an uncaught promise rejection at citation.parseBibFile
const stub = sinon.stub(lw.external, 'stat').resolves({ type: vscode.FileType.File, ctime: 0, mtime: 0, size: Number.MAX_SAFE_INTEGER })

log.start()
for (const handler of lw.watcher.bib['onChangeHandlers']) {
Expand All @@ -538,6 +540,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
}
log.stop()

stub.restore()
assert.hasLog(`Auto build started detecting the change of a file: ${get.path('main.bib')}`)
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/units/09_viewer_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
let websocket: ws.WebSocket

before(async () => {
mock.init(lw, 'file', 'root', 'server')
mock.init(lw, 'server')
handlerStub = lw.viewer.handler as sinon.SinonStub
await connectWs()
})
Expand Down
2 changes: 1 addition & 1 deletion test/units/10_viewer_pdf_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
let handlerSpy: sinon.SinonSpy

before(() => {
mock.init(lw, 'file', 'root', 'server', 'viewer')
mock.init(lw, 'server', 'viewer')
handlerSpy = sinon.spy(lw.viewer, 'handler')
})

Expand Down
2 changes: 1 addition & 1 deletion test/units/11_parser_tex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type * as Ast from '@unified-latex/unified-latex-types'

describe(path.basename(__filename).split('.')[0] + ':', () => {
before(() => {
mock.init(lw, 'file', 'root', 'parser')
mock.init(lw, 'parser')
})

after(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/units/12_parser_log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { latexLogParser } from '../../src/parse/parser/latexlog'

describe(path.basename(__filename).split('.')[0] + ':', () => {
before(() => {
mock.init(lw, 'file', 'root', 'parser')
mock.init(lw, 'parser')
})

after(() => {
Expand Down
14 changes: 14 additions & 0 deletions test/units/13_completion_latex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as path from 'path'
import * as sinon from 'sinon'
import { lw } from '../../src/lw'
import { mock } from './utils'

describe(path.basename(__filename).split('.')[0] + ':', () => {
before(() => {
mock.init(lw, 'root', 'completion')
})

after(() => {
sinon.restore()
})
})
2 changes: 1 addition & 1 deletion test/units/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export const mock = {
: Object.getOwnPropertyNames(Object.getPrototypeOf(obj))
items.forEach(item => {
// Don't stub the unit to be tested or the logging/external functions.
if (ignore.includes(item) || ['log', 'external', 'constant'].includes(item)) {
if (ignore.includes(item) || ['file', 'log', 'external', 'constant'].includes(item)) {
return
}
if (typeof obj[item] === 'object') {
Expand Down

0 comments on commit 58bd90d

Please sign in to comment.