Skip to content

Commit 3227935

Browse files
authored
Minor refactoring of the db config store and tests (#1924)
* Remove unnecessary type castings * Extract validation to separate functions * Nest tests inside a 'describe' block
1 parent 0902e18 commit 3227935

2 files changed

Lines changed: 159 additions & 147 deletions

File tree

extensions/ql-vscode/src/databases/config/db-config-store.ts

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class DbConfigStore extends DisposableObject {
9494
);
9595
}
9696

97-
const config: DbConfig = cloneDbConfig(this.config);
97+
const config = cloneDbConfig(this.config);
9898
if (parentList) {
9999
const parent = config.databases.remote.repositoryLists.find(
100100
(list) => list.name === parentList,
@@ -123,7 +123,7 @@ export class DbConfigStore extends DisposableObject {
123123
throw Error(`A remote owner with the name '${owner}' already exists`);
124124
}
125125

126-
const config: DbConfig = cloneDbConfig(this.config);
126+
const config = cloneDbConfig(this.config);
127127
config.databases.remote.owners.push(owner);
128128

129129
await this.writeConfig(config);
@@ -134,15 +134,9 @@ export class DbConfigStore extends DisposableObject {
134134
throw Error("Cannot add local list if config is not loaded");
135135
}
136136

137-
if (listName === "") {
138-
throw Error("List name cannot be empty");
139-
}
140-
141-
if (this.doesLocalListExist(listName)) {
142-
throw Error(`A local list with the name '${listName}' already exists`);
143-
}
137+
this.validateLocalListName(listName);
144138

145-
const config: DbConfig = cloneDbConfig(this.config);
139+
const config = cloneDbConfig(this.config);
146140
config.databases.local.lists.push({
147141
name: listName,
148142
databases: [],
@@ -156,15 +150,9 @@ export class DbConfigStore extends DisposableObject {
156150
throw Error("Cannot add remote list if config is not loaded");
157151
}
158152

159-
if (listName === "") {
160-
throw Error("List name cannot be empty");
161-
}
162-
163-
if (this.doesRemoteListExist(listName)) {
164-
throw Error(`A remote list with the name '${listName}' already exists`);
165-
}
153+
this.validateRemoteListName(listName);
166154

167-
const config: DbConfig = cloneDbConfig(this.config);
155+
const config = cloneDbConfig(this.config);
168156
config.databases.remote.repositoryLists.push({
169157
name: listName,
170158
repositories: [],
@@ -336,4 +324,24 @@ export class DbConfigStore extends DisposableObject {
336324
},
337325
};
338326
}
327+
328+
private validateLocalListName(listName: string): void {
329+
if (listName === "") {
330+
throw Error("List name cannot be empty");
331+
}
332+
333+
if (this.doesLocalListExist(listName)) {
334+
throw Error(`A local list with the name '${listName}' already exists`);
335+
}
336+
}
337+
338+
private validateRemoteListName(listName: string): void {
339+
if (listName === "") {
340+
throw Error("List name cannot be empty");
341+
}
342+
343+
if (this.doesRemoteListExist(listName)) {
344+
throw Error(`A remote list with the name '${listName}' already exists`);
345+
}
346+
}
339347
}

extensions/ql-vscode/test/unit-tests/databases/config/db-config-store.test.ts

Lines changed: 133 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -16,151 +16,155 @@ describe("db config store", () => {
1616
await remove(tempWorkspaceStoragePath);
1717
});
1818

19-
it("should create a new config if one does not exist", async () => {
20-
const app = createMockApp({
21-
extensionPath,
22-
workspaceStoragePath: tempWorkspaceStoragePath,
19+
describe("initialize", () => {
20+
it("should create a new config if one does not exist", async () => {
21+
const app = createMockApp({
22+
extensionPath,
23+
workspaceStoragePath: tempWorkspaceStoragePath,
24+
});
25+
26+
const configPath = join(
27+
tempWorkspaceStoragePath,
28+
"workspace-databases.json",
29+
);
30+
31+
const configStore = new DbConfigStore(app);
32+
await configStore.initialize();
33+
34+
expect(await pathExists(configPath)).toBe(true);
35+
36+
const config = configStore.getConfig().value;
37+
expect(config.databases.remote.repositoryLists).toHaveLength(0);
38+
expect(config.databases.remote.owners).toHaveLength(0);
39+
expect(config.databases.remote.repositories).toHaveLength(0);
40+
expect(config.databases.local.lists).toHaveLength(0);
41+
expect(config.databases.local.databases).toHaveLength(0);
42+
expect(config.selected).toBeUndefined();
43+
44+
configStore.dispose();
2345
});
2446

25-
const configPath = join(
26-
tempWorkspaceStoragePath,
27-
"workspace-databases.json",
28-
);
29-
30-
const configStore = new DbConfigStore(app);
31-
await configStore.initialize();
32-
33-
expect(await pathExists(configPath)).toBe(true);
34-
35-
const config = configStore.getConfig().value;
36-
expect(config.databases.remote.repositoryLists).toHaveLength(0);
37-
expect(config.databases.remote.owners).toHaveLength(0);
38-
expect(config.databases.remote.repositories).toHaveLength(0);
39-
expect(config.databases.local.lists).toHaveLength(0);
40-
expect(config.databases.local.databases).toHaveLength(0);
41-
expect(config.selected).toBeUndefined();
42-
43-
configStore.dispose();
44-
});
45-
46-
it("should load an existing config", async () => {
47-
const app = createMockApp({
48-
extensionPath,
49-
workspaceStoragePath: testDataStoragePath,
50-
});
51-
const configStore = new DbConfigStore(app);
52-
await configStore.initialize();
53-
54-
const config = configStore.getConfig().value;
55-
expect(config.databases.remote.repositoryLists).toHaveLength(1);
56-
expect(config.databases.remote.repositoryLists[0]).toEqual({
57-
name: "repoList1",
58-
repositories: ["foo/bar", "foo/baz"],
59-
});
60-
expect(config.databases.remote.owners).toHaveLength(0);
61-
expect(config.databases.remote.repositories).toHaveLength(3);
62-
expect(config.databases.remote.repositories).toEqual([
63-
"owner/repo1",
64-
"owner/repo2",
65-
"owner/repo3",
66-
]);
67-
expect(config.databases.local.lists).toHaveLength(2);
68-
expect(config.databases.local.lists[0]).toEqual({
69-
name: "localList1",
70-
databases: [
71-
{
72-
name: "foo/bar",
73-
dateAdded: 1668096745193,
74-
language: "go",
75-
storagePath: "/path/to/database/",
76-
},
77-
],
78-
});
79-
expect(config.databases.local.databases).toHaveLength(1);
80-
expect(config.databases.local.databases[0]).toEqual({
81-
name: "example-db",
82-
dateAdded: 1668096927267,
83-
language: "ruby",
84-
storagePath: "/path/to/database/",
85-
});
86-
expect(config.selected).toEqual({
87-
kind: "remoteUserDefinedList",
88-
listName: "repoList1",
47+
it("should load an existing config", async () => {
48+
const app = createMockApp({
49+
extensionPath,
50+
workspaceStoragePath: testDataStoragePath,
51+
});
52+
const configStore = new DbConfigStore(app);
53+
await configStore.initialize();
54+
55+
const config = configStore.getConfig().value;
56+
expect(config.databases.remote.repositoryLists).toHaveLength(1);
57+
expect(config.databases.remote.repositoryLists[0]).toEqual({
58+
name: "repoList1",
59+
repositories: ["foo/bar", "foo/baz"],
60+
});
61+
expect(config.databases.remote.owners).toHaveLength(0);
62+
expect(config.databases.remote.repositories).toHaveLength(3);
63+
expect(config.databases.remote.repositories).toEqual([
64+
"owner/repo1",
65+
"owner/repo2",
66+
"owner/repo3",
67+
]);
68+
expect(config.databases.local.lists).toHaveLength(2);
69+
expect(config.databases.local.lists[0]).toEqual({
70+
name: "localList1",
71+
databases: [
72+
{
73+
name: "foo/bar",
74+
dateAdded: 1668096745193,
75+
language: "go",
76+
storagePath: "/path/to/database/",
77+
},
78+
],
79+
});
80+
expect(config.databases.local.databases).toHaveLength(1);
81+
expect(config.databases.local.databases[0]).toEqual({
82+
name: "example-db",
83+
dateAdded: 1668096927267,
84+
language: "ruby",
85+
storagePath: "/path/to/database/",
86+
});
87+
expect(config.selected).toEqual({
88+
kind: "remoteUserDefinedList",
89+
listName: "repoList1",
90+
});
91+
92+
configStore.dispose();
8993
});
9094

91-
configStore.dispose();
92-
});
93-
94-
it("should load an existing config without selected db", async () => {
95-
const testDataStoragePathWithout = join(
96-
__dirname,
97-
"data",
98-
"without-selected",
99-
);
95+
it("should load an existing config without selected db", async () => {
96+
const testDataStoragePathWithout = join(
97+
__dirname,
98+
"data",
99+
"without-selected",
100+
);
100101

101-
const app = createMockApp({
102-
extensionPath,
103-
workspaceStoragePath: testDataStoragePathWithout,
104-
});
102+
const app = createMockApp({
103+
extensionPath,
104+
workspaceStoragePath: testDataStoragePathWithout,
105+
});
105106

106-
const configStore = new DbConfigStore(app);
107-
await configStore.initialize();
107+
const configStore = new DbConfigStore(app);
108+
await configStore.initialize();
108109

109-
const config = configStore.getConfig().value;
110-
expect(config.selected).toBeUndefined();
111-
112-
configStore.dispose();
113-
});
110+
const config = configStore.getConfig().value;
111+
expect(config.selected).toBeUndefined();
114112

115-
it("should not allow modification of the config", async () => {
116-
const app = createMockApp({
117-
extensionPath,
118-
workspaceStoragePath: testDataStoragePath,
113+
configStore.dispose();
119114
});
120-
const configStore = new DbConfigStore(app);
121-
await configStore.initialize();
122115

123-
const config = configStore.getConfig().value;
124-
config.databases.remote.repositoryLists = [];
116+
it("should not allow modification of the config", async () => {
117+
const app = createMockApp({
118+
extensionPath,
119+
workspaceStoragePath: testDataStoragePath,
120+
});
121+
const configStore = new DbConfigStore(app);
122+
await configStore.initialize();
125123

126-
const reRetrievedConfig = configStore.getConfig().value;
127-
expect(reRetrievedConfig.databases.remote.repositoryLists).toHaveLength(1);
124+
const config = configStore.getConfig().value;
125+
config.databases.remote.repositoryLists = [];
128126

129-
configStore.dispose();
130-
});
131-
132-
it("should set codeQLDatabasesExperimental.configError to true when config has error", async () => {
133-
const testDataStoragePathInvalid = join(__dirname, "data", "invalid");
127+
const reRetrievedConfig = configStore.getConfig().value;
128+
expect(reRetrievedConfig.databases.remote.repositoryLists).toHaveLength(
129+
1,
130+
);
134131

135-
const app = createMockApp({
136-
extensionPath,
137-
workspaceStoragePath: testDataStoragePathInvalid,
132+
configStore.dispose();
138133
});
139-
const configStore = new DbConfigStore(app);
140-
await configStore.initialize();
141-
142-
expect(app.executeCommand).toBeCalledWith(
143-
"setContext",
144-
"codeQLDatabasesExperimental.configError",
145-
true,
146-
);
147-
configStore.dispose();
148-
});
149134

150-
it("should set codeQLDatabasesExperimental.configError to false when config is valid", async () => {
151-
const app = createMockApp({
152-
extensionPath,
153-
workspaceStoragePath: testDataStoragePath,
135+
it("should set codeQLDatabasesExperimental.configError to true when config has error", async () => {
136+
const testDataStoragePathInvalid = join(__dirname, "data", "invalid");
137+
138+
const app = createMockApp({
139+
extensionPath,
140+
workspaceStoragePath: testDataStoragePathInvalid,
141+
});
142+
const configStore = new DbConfigStore(app);
143+
await configStore.initialize();
144+
145+
expect(app.executeCommand).toBeCalledWith(
146+
"setContext",
147+
"codeQLDatabasesExperimental.configError",
148+
true,
149+
);
150+
configStore.dispose();
154151
});
155-
const configStore = new DbConfigStore(app);
156-
await configStore.initialize();
157152

158-
expect(app.executeCommand).toBeCalledWith(
159-
"setContext",
160-
"codeQLDatabasesExperimental.configError",
161-
false,
162-
);
163-
164-
configStore.dispose();
153+
it("should set codeQLDatabasesExperimental.configError to false when config is valid", async () => {
154+
const app = createMockApp({
155+
extensionPath,
156+
workspaceStoragePath: testDataStoragePath,
157+
});
158+
const configStore = new DbConfigStore(app);
159+
await configStore.initialize();
160+
161+
expect(app.executeCommand).toBeCalledWith(
162+
"setContext",
163+
"codeQLDatabasesExperimental.configError",
164+
false,
165+
);
166+
167+
configStore.dispose();
168+
});
165169
});
166170
});

0 commit comments

Comments
 (0)