Skip to content

Commit 2d040c2

Browse files
committed
Add comment and test to enforce the name of the tutorial database
I'm extracting the call to `openDatabase` where we set a custom display name into a separate method: `openTutorialDatabase`. I've added comments to it to specify the display name is significant. I've also added a test that will fail if you ever change the name of the tutorial database.
1 parent 937e504 commit 2d040c2

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

extensions/ql-vscode/src/local-databases-ui.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,12 +380,7 @@ export class DatabaseUI extends DisposableObject {
380380

381381
let databaseItem = this.databaseManager.findDatabaseItem(uri);
382382
if (databaseItem === undefined) {
383-
databaseItem = await this.databaseManager.openDatabase(
384-
progress,
385-
token,
386-
uri,
387-
"CodeQL Tutorial Database",
388-
);
383+
databaseItem = await this.openTutorialDatabase(progress, token, uri);
389384
}
390385
await this.databaseManager.setCurrentDatabaseItem(databaseItem);
391386
}
@@ -399,6 +394,22 @@ export class DatabaseUI extends DisposableObject {
399394
}
400395
};
401396

397+
// Opens the tutorial database for the CodeQL Tour.
398+
// We set the database name as "CodeQL Tutorial Database" in purpose. This means
399+
// we won't attempt to create a skeleton QL pack for this database.
400+
openTutorialDatabase = async (
401+
progress: ProgressCallback,
402+
token: CancellationToken,
403+
uri: Uri,
404+
): Promise<DatabaseItem> => {
405+
return await this.databaseManager.openDatabase(
406+
progress,
407+
token,
408+
uri,
409+
"CodeQL Tutorial Database",
410+
);
411+
};
412+
402413
handleRemoveOrphanedDatabases = async (): Promise<void> => {
403414
void extLogger.log("Removing orphaned databases from workspace storage.");
404415
let dbDirs = undefined;

extensions/ql-vscode/test/vscode-tests/no-workspace/local-databases-ui.test.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
createFileSync,
88
pathExistsSync,
99
} from "fs-extra";
10-
import { Uri } from "vscode";
10+
import { CancellationToken, Uri } from "vscode";
1111

1212
import { DatabaseUI } from "../../../src/local-databases-ui";
1313
import { testDisposeHandler } from "../test-dispose-handler";
1414
import { createMockApp } from "../../__mocks__/appMock";
1515
import { QueryLanguage } from "../../../src/common/query-language";
16+
import { App } from "../../../src/common/app";
17+
import { ProgressCallback } from "../../../src/commandRunner";
1618

1719
describe("local-databases-ui", () => {
1820
describe("fixDbUri", () => {
@@ -116,6 +118,51 @@ describe("local-databases-ui", () => {
116118
databaseUI.dispose(testDisposeHandler);
117119
});
118120

121+
describe("openTutorialDatabase", () => {
122+
let app: App;
123+
let databaseUI: DatabaseUI;
124+
let openDatabaseSpy: jest.SpyInstance;
125+
let databaseManager: any;
126+
127+
beforeEach(async () => {
128+
app = createMockApp({});
129+
openDatabaseSpy = jest.fn();
130+
131+
databaseManager = {
132+
databaseItems: [],
133+
openDatabase: openDatabaseSpy,
134+
onDidChangeDatabaseItem: () => {
135+
/**/
136+
},
137+
onDidChangeCurrentDatabaseItem: () => {
138+
/**/
139+
},
140+
};
141+
databaseUI = new DatabaseUI(
142+
app,
143+
databaseManager as any,
144+
{} as any,
145+
"",
146+
"",
147+
);
148+
});
149+
150+
it("should call openDatase with 'CodeQL Tutorial Database' as displayName", async () => {
151+
await databaseUI.openTutorialDatabase(
152+
{} as ProgressCallback,
153+
{} as CancellationToken,
154+
Uri.parse(""),
155+
);
156+
157+
expect(openDatabaseSpy).toHaveBeenCalledWith(
158+
expect.anything(),
159+
expect.anything(),
160+
expect.anything(),
161+
"CodeQL Tutorial Database",
162+
);
163+
});
164+
});
165+
119166
function createDatabase(
120167
storageDir: string,
121168
dbName: string,

0 commit comments

Comments
 (0)