From ca9510c08d1c60e0891bee464bda3b23f1f36382 Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Mon, 14 Dec 2020 15:54:46 -0800 Subject: [PATCH] Add unit test for test discovery When directory is not present. --- extensions/ql-vscode/src/databases-ui.ts | 2 +- .../qltest-discovery.test.ts | 44 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/extensions/ql-vscode/src/databases-ui.ts b/extensions/ql-vscode/src/databases-ui.ts index 7fa0ec985..efda3f1ac 100644 --- a/extensions/ql-vscode/src/databases-ui.ts +++ b/extensions/ql-vscode/src/databases-ui.ts @@ -384,7 +384,7 @@ export class DatabaseUI extends DisposableObject { !(await fs.pathExists(this.storagePath) || !(await fs.stat(this.storagePath)).isDirectory()) ) { - // ignore a missing or invalid storage directory. + logger.log('Missing or invalid storage directory. Not trying to remove orphaned databases.'); return; } diff --git a/extensions/ql-vscode/src/vscode-tests/minimal-workspace/qltest-discovery.test.ts b/extensions/ql-vscode/src/vscode-tests/minimal-workspace/qltest-discovery.test.ts index cab1781ee..d0445ad63 100644 --- a/extensions/ql-vscode/src/vscode-tests/minimal-workspace/qltest-discovery.test.ts +++ b/extensions/ql-vscode/src/vscode-tests/minimal-workspace/qltest-discovery.test.ts @@ -2,20 +2,27 @@ import 'vscode-test'; import 'mocha'; import { Uri, WorkspaceFolder } from 'vscode'; import { expect } from 'chai'; +import * as fs from 'fs-extra'; +import * as sinon from 'sinon'; import { QLTestDiscovery } from '../../qltest-discovery'; describe('qltest-discovery', () => { describe('discoverTests', () => { - it('should run discovery', async () => { - const baseUri = Uri.parse('file:/a/b'); - const baseDir = baseUri.fsPath; - const cDir = Uri.parse('file:/a/b/c').fsPath; - const dFile = Uri.parse('file:/a/b/c/d.ql').fsPath; - const eFile = Uri.parse('file:/a/b/c/e.ql').fsPath; - const hDir = Uri.parse('file:/a/b/c/f/g/h').fsPath; - const iFile = Uri.parse('file:/a/b/c/f/g/h/i.ql').fsPath; - const qlTestDiscover = new QLTestDiscovery( + let sandbox: sinon.SinonSandbox; + + const baseUri = Uri.parse('file:/a/b'); + const baseDir = baseUri.fsPath; + const cDir = Uri.parse('file:/a/b/c').fsPath; + const dFile = Uri.parse('file:/a/b/c/d.ql').fsPath; + const eFile = Uri.parse('file:/a/b/c/e.ql').fsPath; + const hDir = Uri.parse('file:/a/b/c/f/g/h').fsPath; + const iFile = Uri.parse('file:/a/b/c/f/g/h/i.ql').fsPath; + let qlTestDiscover: QLTestDiscovery; + + beforeEach(() => { + sandbox = sinon.createSandbox(); + qlTestDiscover = new QLTestDiscovery( { uri: baseUri, name: 'My tests' @@ -31,6 +38,15 @@ describe('qltest-discovery', () => { } as any ); + }); + + afterEach(() => { + sandbox.restore(); + }); + + it('should run discovery', async () => { + sandbox.stub(fs, 'pathExists').resolves(true); + const result = await (qlTestDiscover as any).discover(); expect(result.watchPath).to.eq(baseDir); expect(result.testDirectory.path).to.eq(baseDir); @@ -56,5 +72,15 @@ describe('qltest-discovery', () => { expect(children[0].path).to.eq(iFile); expect(children[0].name).to.eq('i.ql'); }); + + it('should avoid discovery if a folder does not exist', async () => { + sandbox.stub(fs, 'pathExists').resolves(false); + const result = await (qlTestDiscover as any).discover(); + expect(result.watchPath).to.eq(baseDir); + expect(result.testDirectory.path).to.eq(baseDir); + expect(result.testDirectory.name).to.eq('My tests'); + + expect(result.testDirectory.children.length).to.eq(0); + }); }); });