|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Red Hat, Inc. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as sinon from 'sinon'; |
| 7 | +import * as sinonChai from 'sinon-chai'; |
| 8 | +import * as chai from 'chai'; |
| 9 | +import * as vscode from 'vscode'; |
| 10 | +import * as fs from 'fs-extra'; |
| 11 | +import { JSONSchemaCache } from '../src/json-schema-cache'; |
| 12 | +import { TestMemento } from './helper'; |
| 13 | + |
| 14 | +const expect = chai.expect; |
| 15 | +chai.use(sinonChai); |
| 16 | +describe('JSON Schema Cache Tests', () => { |
| 17 | + const sandbox = sinon.createSandbox(); |
| 18 | + let memento: sinon.SinonStubbedInstance<vscode.Memento>; |
| 19 | + let ensureDirStub: sinon.SinonStub; |
| 20 | + let readdirStub: sinon.SinonStub; |
| 21 | + let pathExistsStub: sinon.SinonStub; |
| 22 | + let readFileStub: sinon.SinonStub; |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + sandbox.restore(); |
| 26 | + }); |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + memento = sandbox.stub(new TestMemento()); |
| 30 | + ensureDirStub = sandbox.stub(fs, 'ensureDir'); |
| 31 | + readdirStub = sandbox.stub(fs, 'readdir'); |
| 32 | + pathExistsStub = sandbox.stub(fs, 'pathExists'); |
| 33 | + readFileStub = sandbox.stub(fs, 'readFile'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should clean up cache if there are no schema file', async () => { |
| 37 | + memento.get.returns({ somePath: { schemaPath: '/foo/path/' } }); |
| 38 | + memento.update.resolves(); |
| 39 | + |
| 40 | + ensureDirStub.resolves(); |
| 41 | + readdirStub.resolves([]); |
| 42 | + |
| 43 | + pathExistsStub.resolves(false); |
| 44 | + readFileStub.resolves(); |
| 45 | + |
| 46 | + const cache = new JSONSchemaCache('/some/path/', (memento as unknown) as vscode.Memento, {} as vscode.OutputChannel); |
| 47 | + const result = await cache.getSchema('/some/uri'); |
| 48 | + expect(result).is.undefined; |
| 49 | + expect(memento.update).calledOnceWith('json-schema-key', {}); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should check cache', async () => { |
| 53 | + const mementoData = { somePath: { schemaPath: '/some/path/foo.json' } }; |
| 54 | + memento.get.returns(mementoData); |
| 55 | + memento.update.resolves(); |
| 56 | + |
| 57 | + ensureDirStub.resolves(); |
| 58 | + readdirStub.resolves(['foo.json']); |
| 59 | + |
| 60 | + pathExistsStub.resolves(false); |
| 61 | + readFileStub.resolves(); |
| 62 | + |
| 63 | + const cache = new JSONSchemaCache('/some/path/', (memento as unknown) as vscode.Memento, {} as vscode.OutputChannel); |
| 64 | + const result = await cache.getSchema('/some/uri'); |
| 65 | + expect(result).is.undefined; |
| 66 | + expect(memento.update).calledOnceWith('json-schema-key', mementoData); |
| 67 | + }); |
| 68 | +}); |
0 commit comments