|
| 1 | +import 'mocha'; |
| 2 | +import 'sinon-chai'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as tmp from 'tmp'; |
| 6 | + |
| 7 | +import { expect } from 'chai'; |
| 8 | +import { ConfigurationTarget, workspace, extensions, CancellationToken, Uri } from 'vscode'; |
| 9 | +import * as vscode from 'vscode'; |
| 10 | +import * as fs from 'fs-extra'; |
| 11 | + |
| 12 | +import { CodeQLExtensionInterface } from '../../extension'; |
| 13 | +import { DatabaseManager } from '../../databases'; |
| 14 | +import { promptImportLgtmDatabase, importArchiveDatabase, promptImportInternetDatabase } from '../../databaseFetcher'; |
| 15 | +import { ProgressCallback } from '../../helpers'; |
| 16 | +import { fail } from 'assert'; |
| 17 | +import fetch from 'node-fetch'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Run various integration tests for databases |
| 21 | + */ |
| 22 | +describe('Databases', function() { |
| 23 | + const DB_URL = 'https://github.com/github/vscode-codeql/files/5586722/simple-db.zip'; |
| 24 | + const LGTM_URL = 'https://lgtm.com/projects/g/aeisenberg/angular-bind-notifier/'; |
| 25 | + |
| 26 | + this.timeout(60000); |
| 27 | + |
| 28 | + let databaseManager: DatabaseManager; |
| 29 | + let sandbox: sinon.SinonSandbox; |
| 30 | + let storagePath: string; |
| 31 | + let storagePathCleanup: () => void; |
| 32 | + let inputBoxStub: sinon.SinonStub; |
| 33 | + let progressCallback: ProgressCallback; |
| 34 | + |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + try { |
| 38 | + // Set it here before activation to ensure we don't accidentally try to download a cli |
| 39 | + await workspace.getConfiguration().update('codeQL.cli.executablePath', process.env.CLI_PATH, ConfigurationTarget.Global); |
| 40 | + const extension = await extensions.getExtension<CodeQLExtensionInterface | {}>('GitHub.vscode-codeql')!.activate(); |
| 41 | + if ('cliServer' in extension) { |
| 42 | + databaseManager = extension.databaseManager; |
| 43 | + } else { |
| 44 | + throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.'); |
| 45 | + } |
| 46 | + |
| 47 | + sandbox = sinon.createSandbox(); |
| 48 | + const dir = tmp.dirSync(); |
| 49 | + storagePath = dir.name; |
| 50 | + // the uri.fsPath function on windows returns a lowercase drive letter |
| 51 | + // so, force the storage path string to be lowercase, too. |
| 52 | + if (storagePath.substring(0, 2).match(/[A-Z]:/)) { |
| 53 | + storagePath = storagePath.substring(0, 1).toLocaleLowerCase() + storagePath.substring(1); |
| 54 | + } |
| 55 | + storagePathCleanup = dir.removeCallback; |
| 56 | + progressCallback = sandbox.spy(); |
| 57 | + inputBoxStub = sandbox.stub(vscode.window, 'showInputBox'); |
| 58 | + } catch (e) { |
| 59 | + fail(e); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + afterEach(() => { |
| 64 | + try { |
| 65 | + databaseManager.dispose(); |
| 66 | + sandbox.restore(); |
| 67 | + storagePathCleanup(); |
| 68 | + } catch (e) { |
| 69 | + fail(e); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it('should add a database from a folder', async () => { |
| 74 | + const result = await downloadDb(); |
| 75 | + try { |
| 76 | + const progressCallback = sandbox.spy() as ProgressCallback; |
| 77 | + const uri = Uri.file(result.dbLoc); |
| 78 | + let dbItem = await importArchiveDatabase(uri.toString(true), databaseManager, storagePath, progressCallback, {} as CancellationToken); |
| 79 | + expect(dbItem).to.be.eq(databaseManager.currentDatabaseItem); |
| 80 | + expect(dbItem).to.be.eq(databaseManager.databaseItems[0]); |
| 81 | + expect(dbItem).not.to.be.undefined; |
| 82 | + dbItem = dbItem!; |
| 83 | + expect(dbItem.name).to.eq('db'); |
| 84 | + expect(dbItem.databaseUri.fsPath).to.eq(path.join(storagePath, 'db', 'db')); |
| 85 | + } finally { |
| 86 | + result.removeCallback(); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + it('should add a database from lgtm with only one language', async () => { |
| 91 | + inputBoxStub.resolves(LGTM_URL); |
| 92 | + let dbItem = await promptImportLgtmDatabase(databaseManager, storagePath, progressCallback, {} as CancellationToken); |
| 93 | + expect(dbItem).not.to.be.undefined; |
| 94 | + dbItem = dbItem!; |
| 95 | + expect(dbItem.name).to.eq('aeisenberg_angular-bind-notifier_106179a'); |
| 96 | + expect(dbItem.databaseUri.fsPath).to.eq(path.join(storagePath, 'javascript', 'aeisenberg_angular-bind-notifier_106179a')); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should add a database from a url', async () => { |
| 100 | + inputBoxStub.resolves(DB_URL); |
| 101 | + |
| 102 | + let dbItem = await promptImportInternetDatabase(databaseManager, storagePath, progressCallback, {} as CancellationToken); |
| 103 | + expect(dbItem).not.to.be.undefined; |
| 104 | + dbItem = dbItem!; |
| 105 | + expect(dbItem.name).to.eq('db'); |
| 106 | + expect(dbItem.databaseUri.fsPath).to.eq(path.join(storagePath, 'simple-db', 'db')); |
| 107 | + }); |
| 108 | + |
| 109 | + async function downloadDb(): Promise<{ |
| 110 | + removeCallback: () => void; dbLoc: string; |
| 111 | + }> { |
| 112 | + return new Promise((resolve, reject) => { |
| 113 | + fetch(DB_URL).then(response => { |
| 114 | + const dir = tmp.dirSync(); |
| 115 | + const dbLoc = path.join(dir.name, 'db.zip'); |
| 116 | + const dest = fs.createWriteStream(dbLoc); |
| 117 | + response.body.pipe(dest); |
| 118 | + |
| 119 | + response.body.on('error', reject); |
| 120 | + dest.on('error', reject); |
| 121 | + dest.on('close', () => { |
| 122 | + resolve({ |
| 123 | + removeCallback: dir.removeCallback, |
| 124 | + dbLoc |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | + }); |
| 129 | + } |
| 130 | +}); |
0 commit comments