Add unit tests for event emitting in database manager
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
- Fix proper escaping of backslashes in SARIF message strings.
|
||||
- Allow setting `codeQL.runningQueries.numberOfThreads` and `codeQL.runningTests.numberOfThreads` to 0, (which is interpreted as 'use one thread per core on the machine').
|
||||
- Clear the problems view of all Code QL problems when a database is removed.
|
||||
- Clear the problems view of all CodeQL query results when a database is removed.
|
||||
|
||||
## 1.3.3 - 16 September 2020
|
||||
|
||||
|
||||
@@ -273,10 +273,12 @@ class DatabaseItemImpl implements DatabaseItem {
|
||||
/** A cache of database info */
|
||||
private _dbinfo: cli.DbInfo | undefined;
|
||||
|
||||
public constructor(public readonly databaseUri: vscode.Uri,
|
||||
contents: DatabaseContents | undefined, private options: FullDatabaseOptions,
|
||||
private readonly onChanged: (event: DatabaseChangedEvent) => void) {
|
||||
|
||||
public constructor(
|
||||
public readonly databaseUri: vscode.Uri,
|
||||
contents: DatabaseContents | undefined,
|
||||
private options: FullDatabaseOptions,
|
||||
private readonly onChanged: (event: DatabaseChangedEvent) => void
|
||||
) {
|
||||
this._contents = contents;
|
||||
}
|
||||
|
||||
@@ -483,9 +485,11 @@ export class DatabaseManager extends DisposableObject {
|
||||
private readonly _databaseItems: DatabaseItemImpl[] = [];
|
||||
private _currentDatabaseItem: DatabaseItem | undefined = undefined;
|
||||
|
||||
constructor(private ctx: ExtensionContext,
|
||||
constructor(
|
||||
private ctx: ExtensionContext,
|
||||
public config: QueryServerConfig,
|
||||
public logger: Logger) {
|
||||
public logger: Logger
|
||||
) {
|
||||
super();
|
||||
|
||||
this.loadPersistedState(); // Let this run async.
|
||||
@@ -655,6 +659,7 @@ export class DatabaseManager extends DisposableObject {
|
||||
private async addDatabaseItem(item: DatabaseItemImpl) {
|
||||
this._databaseItems.push(item);
|
||||
this.updatePersistedDatabaseList();
|
||||
// note that we use undefined as the item in order to reset the entire tree
|
||||
this._onDidChangeDatabaseItem.fire({
|
||||
item: undefined,
|
||||
kind: DatabaseEventKind.Add
|
||||
@@ -694,6 +699,7 @@ export class DatabaseManager extends DisposableObject {
|
||||
e => logger.log(`Failed to delete '${item.databaseUri.path}'. Reason: ${e.message}`));
|
||||
}
|
||||
|
||||
// note that we use undefined as the item in order to reset the entire tree
|
||||
this._onDidChangeDatabaseItem.fire({
|
||||
item: undefined,
|
||||
kind: DatabaseEventKind.Remove
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import 'vscode-test';
|
||||
import 'mocha';
|
||||
import * as sinon from 'sinon';
|
||||
import { expect } from 'chai';
|
||||
import { ExtensionContext } from 'vscode';
|
||||
|
||||
import { DatabaseEventKind, DatabaseItem, DatabaseManager } from '../../databases';
|
||||
import { QueryServerConfig } from '../../config';
|
||||
import { Logger } from '../../logging';
|
||||
|
||||
describe('databases', () => {
|
||||
let databaseManager: DatabaseManager;
|
||||
let updateSpy: sinon.SinonSpy;
|
||||
|
||||
beforeEach(() => {
|
||||
updateSpy = sinon.spy();
|
||||
databaseManager = new DatabaseManager(
|
||||
{
|
||||
workspaceState: {
|
||||
update: updateSpy
|
||||
}
|
||||
} as unknown as ExtensionContext,
|
||||
{} as QueryServerConfig,
|
||||
{} as Logger,
|
||||
);
|
||||
});
|
||||
|
||||
it('should fire events when adding and removing a db item', () => {
|
||||
const mockDbItem = {
|
||||
databaseUri: 'file:/abc',
|
||||
getPersistedState() {
|
||||
return this.databaseUri;
|
||||
}
|
||||
};
|
||||
const spy = sinon.spy();
|
||||
databaseManager.onDidChangeDatabaseItem(spy);
|
||||
(databaseManager as any).addDatabaseItem(mockDbItem);
|
||||
|
||||
expect((databaseManager as any)._databaseItems).to.deep.eq([mockDbItem]);
|
||||
expect(updateSpy).to.have.been.calledWith('databaseList', ['file:/abc']);
|
||||
expect(spy).to.have.been.calledWith({
|
||||
item: undefined,
|
||||
kind: DatabaseEventKind.Add
|
||||
});
|
||||
|
||||
sinon.reset();
|
||||
|
||||
// now remove the item
|
||||
databaseManager.removeDatabaseItem(mockDbItem as unknown as DatabaseItem);
|
||||
expect((databaseManager as any)._databaseItems).to.deep.eq([]);
|
||||
expect(updateSpy).to.have.been.calledWith('databaseList', []);
|
||||
expect(spy).to.have.been.calledWith({
|
||||
item: undefined,
|
||||
kind: DatabaseEventKind.Remove
|
||||
});
|
||||
});
|
||||
|
||||
it('should rename a db item and emit an event', () => {
|
||||
const mockDbItem = {
|
||||
databaseUri: 'file:/abc',
|
||||
name: 'abc',
|
||||
getPersistedState() {
|
||||
return this.name;
|
||||
}
|
||||
};
|
||||
const spy = sinon.spy();
|
||||
databaseManager.onDidChangeDatabaseItem(spy);
|
||||
(databaseManager as any).addDatabaseItem(mockDbItem);
|
||||
|
||||
databaseManager.renameDatabaseItem(mockDbItem as unknown as DatabaseItem, 'new name');
|
||||
|
||||
expect(mockDbItem.name).to.eq('new name');
|
||||
expect(updateSpy).to.have.been.calledWith('databaseList', ['new name']);
|
||||
expect(spy).to.have.been.calledWith({
|
||||
item: undefined,
|
||||
kind: DatabaseEventKind.Rename
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user