Provide a way to search for database items by name and language

We'll use this to check whether a database for our ql pack already exists.

While there are other methods that search for a database item by URI, we
only have a language chosen by the user and an nwo ("github/codeql").

So let's introduce a way to search for the db based on the information we
have.
This commit is contained in:
Elena Tanasoiu
2023-03-29 14:41:09 +00:00
parent 053a1800a9
commit 2995b0120d
2 changed files with 47 additions and 0 deletions

View File

@@ -897,6 +897,20 @@ export class DatabaseManager extends DisposableObject {
}
}
public async digForDatabaseItem(
language: string,
databaseNwo: string,
): Promise<DatabaseItem | undefined> {
const dbItems = this.databaseItems || [];
const dbs = dbItems.filter(
(db) => db.language === language && db.name === databaseNwo,
);
if (dbs.length === 0) {
return undefined;
}
return dbs[0];
}
/**
* Returns the index of the workspace folder that corresponds to the source archive of `item`
* if there is one, and -1 otherwise.

View File

@@ -785,6 +785,39 @@ describe("local databases", () => {
});
});
describe("digForDatabaseItem", () => {
describe("when the item exists", () => {
it("should return the database item", async () => {
const mockDbItem = createMockDB();
await (databaseManager as any).addDatabaseItem(
{} as ProgressCallback,
{} as CancellationToken,
mockDbItem,
);
const databaseItem = await databaseManager.digForDatabaseItem(
mockDbItem.language,
mockDbItem.name,
);
expect(databaseItem!.language).toEqual(mockDbItem.language);
expect(databaseItem!.name).toEqual(mockDbItem.name);
});
});
describe("when the item doesn't exist", () => {
it("should return nothing", async () => {
const databaseItem = await databaseManager.digForDatabaseItem(
"ruby",
"mock-database-name",
);
expect(databaseItem).toBeUndefined();
});
});
});
function createMockDB(
mockDbOptions = MOCK_DB_OPTIONS,
// source archive location must be a real(-ish) location since