Skip to content

Commit 7196c26

Browse files
authored
Merge pull request #1168 from github/aeisenberg/query-history-version
Preemptively add a version number to the query history json file
2 parents 735f177 + f857e5e commit 7196c26

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

extensions/ql-vscode/src/query-serialization.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ export async function slurpQueryHistory(fsPath: string, config: QueryHistoryConf
1414
}
1515

1616
const data = await fs.readFile(fsPath, 'utf8');
17-
const queries = JSON.parse(data);
17+
const obj = JSON.parse(data);
18+
if (obj.version !== 1) {
19+
void showAndLogErrorMessage(`Unsupported query history format: v${obj.version}. `);
20+
return [];
21+
}
22+
23+
const queries = obj.queries;
1824
const parsedQueries = queries.map((q: QueryHistoryInfo) => {
1925

2026
// Need to explicitly set prototype since reading in from JSON will not
@@ -82,7 +88,10 @@ export async function splatQueryHistory(queries: QueryHistoryInfo[], fsPath: str
8288
}
8389
// remove incomplete local queries since they cannot be recreated on restart
8490
const filteredQueries = queries.filter(q => q.t === 'local' ? q.completedQuery !== undefined : true);
85-
const data = JSON.stringify(filteredQueries, null, 2);
91+
const data = JSON.stringify({
92+
version: 1,
93+
queries: filteredQueries
94+
}, null, 2);
8695
await fs.writeFile(fsPath, data);
8796
} catch (e) {
8897
throw new Error(`Error saving query history to ${fsPath}: ${e.message}`);

extensions/ql-vscode/src/vscode-tests/no-workspace/query-results.test.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,30 @@ describe('query-results', () => {
254254
});
255255

256256
describe('splat and slurp', () => {
257-
it('should splat and slurp query history', async () => {
258-
const infoSuccessRaw = createMockFullQueryInfo('a', createMockQueryWithResults(`${queryPath}-a`, false, false, '/a/b/c/a', false));
259-
const infoSuccessInterpreted = createMockFullQueryInfo('b', createMockQueryWithResults(`${queryPath}-b`, true, true, '/a/b/c/b', false));
260-
const infoEarlyFailure = createMockFullQueryInfo('c', undefined, true);
261-
const infoLateFailure = createMockFullQueryInfo('d', createMockQueryWithResults(`${queryPath}-c`, false, false, '/a/b/c/d', false));
262-
const infoInprogress = createMockFullQueryInfo('e');
263-
const allHistory = [
257+
258+
let infoSuccessRaw: LocalQueryInfo;
259+
let infoSuccessInterpreted: LocalQueryInfo;
260+
let infoEarlyFailure: LocalQueryInfo;
261+
let infoLateFailure: LocalQueryInfo;
262+
let infoInprogress: LocalQueryInfo;
263+
let allHistory: LocalQueryInfo[];
264+
265+
beforeEach(() => {
266+
infoSuccessRaw = createMockFullQueryInfo('a', createMockQueryWithResults(`${queryPath}-a`, false, false, '/a/b/c/a', false));
267+
infoSuccessInterpreted = createMockFullQueryInfo('b', createMockQueryWithResults(`${queryPath}-b`, true, true, '/a/b/c/b', false));
268+
infoEarlyFailure = createMockFullQueryInfo('c', undefined, true);
269+
infoLateFailure = createMockFullQueryInfo('d', createMockQueryWithResults(`${queryPath}-c`, false, false, '/a/b/c/d', false));
270+
infoInprogress = createMockFullQueryInfo('e');
271+
allHistory = [
264272
infoSuccessRaw,
265273
infoSuccessInterpreted,
266274
infoEarlyFailure,
267275
infoLateFailure,
268276
infoInprogress
269277
];
278+
});
270279

280+
it('should splat and slurp query history', async () => {
271281
// the expected results only contains the history with completed queries
272282
const expectedHistory = [
273283
infoSuccessRaw,
@@ -313,6 +323,18 @@ describe('query-results', () => {
313323
}
314324
expect(allHistoryActual.length).to.deep.eq(expectedHistory.length);
315325
});
326+
327+
it('should handle an invalid query history version', async () => {
328+
const badPath = path.join(tmpDir.name, 'bad-query-history.json');
329+
fs.writeFileSync(badPath, JSON.stringify({
330+
version: 2,
331+
queries: allHistory
332+
}), 'utf8');
333+
334+
const allHistoryActual = await slurpQueryHistory(badPath, mockConfig);
335+
// version number is invalid. Should return an empty array.
336+
expect(allHistoryActual).to.deep.eq([]);
337+
});
316338
});
317339

318340
function createMockQueryWithResults(

0 commit comments

Comments
 (0)