Skip to content

Commit 2f92477

Browse files
committed
Move storagePath calculation to extension.ts
1 parent 926ab92 commit 2f92477

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import * as fetch from "node-fetch";
22
import * as unzipper from "unzipper";
3-
import { ExtensionContext, Uri, ProgressOptions, ProgressLocation, commands, window } from "vscode";
3+
import { Uri, ProgressOptions, ProgressLocation, commands, window } from "vscode";
44
import * as fs from "fs-extra";
55
import * as path from "path";
66
import { DatabaseManager } from "./databases";
77
import { ProgressCallback, showAndLogErrorMessage, withProgress } from "./helpers";
88

9-
export default async function promptFetchDatabase(dbm: DatabaseManager, ctx: ExtensionContext) {
9+
export default async function promptFetchDatabase(dbm: DatabaseManager, storagePath: string) {
1010
try {
1111
const databaseUrl = await window.showInputBox({
1212
prompt: 'Enter URL of zipfile of database to download'
@@ -20,7 +20,7 @@ export default async function promptFetchDatabase(dbm: DatabaseManager, ctx: Ext
2020
title: 'Adding database from URL',
2121
cancellable: false,
2222
};
23-
await withProgress(progressOptions, async progress => await databaseFetcher(databaseUrl, dbm, ctx, progress));
23+
await withProgress(progressOptions, async progress => await databaseFetcher(databaseUrl, dbm, storagePath, progress));
2424
commands.executeCommand('codeQLDatabases.focus');
2525
}
2626
} catch (e) {
@@ -31,15 +31,14 @@ export default async function promptFetchDatabase(dbm: DatabaseManager, ctx: Ext
3131
async function databaseFetcher(
3232
databaseUrl: string,
3333
databasesManager: DatabaseManager,
34-
ctx: ExtensionContext,
34+
storagePath: string,
3535
progressCallback: ProgressCallback
3636
): Promise<void> {
3737
progressCallback({
3838
maxStep: 3,
3939
message: 'Downloading database',
4040
step: 1
4141
});
42-
const storagePath = ctx.storagePath || ctx.globalStoragePath;
4342
if (!storagePath) {
4443
throw new Error("No storage path specified.");
4544
}

extensions/ql-vscode/src/databases.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ async function findDataset(parentDirectory: string): Promise<vscode.Uri> {
109109
return vscode.Uri.file(dbAbsolutePath);
110110
}
111111

112-
async function findSourceArchive(databasePath: string, silent = false):
113-
Promise<vscode.Uri | undefined> {
112+
async function findSourceArchive(
113+
databasePath: string, silent = false
114+
): Promise<vscode.Uri | undefined> {
114115

115116
const relativePaths = ['src', 'output/src_archive']
116117

extensions/ql-vscode/src/extension.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu
335335
await qs.restartQueryServer();
336336
helpers.showAndLogInformationMessage('CodeQL Query Server restarted.', { outputLogger: queryServerLogger });
337337
}));
338-
ctx.subscriptions.push(commands.registerCommand('codeQL.downloadDatabase', () => promptFetchDatabase(dbm, ctx)));
338+
ctx.subscriptions.push(commands.registerCommand('codeQL.downloadDatabase', () => promptFetchDatabase(dbm, getContextStoragePath(ctx))));
339339

340340
ctx.subscriptions.push(client.start());
341341

@@ -351,10 +351,15 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu
351351
}
352352
}
353353

354+
function getContextStoragePath(ctx: ExtensionContext) {
355+
return ctx.storagePath || ctx.globalStoragePath;
356+
}
357+
354358
function initializeLogging(ctx: ExtensionContext): void {
355-
logger.init(ctx);
356-
queryServerLogger.init(ctx);
357-
ideServerLogger.init(ctx);
359+
const storagePath = getContextStoragePath(ctx);
360+
logger.init(storagePath);
361+
queryServerLogger.init(storagePath);
362+
ideServerLogger.init(storagePath);
358363
ctx.subscriptions.push(logger);
359364
ctx.subscriptions.push(queryServerLogger);
360365
ctx.subscriptions.push(ideServerLogger);

extensions/ql-vscode/src/logging.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { window as Window, OutputChannel, Progress, ExtensionContext, Disposable } from 'vscode';
1+
import { window as Window, OutputChannel, Progress, Disposable } from 'vscode';
22
import { DisposableObject } from 'semmle-vscode-utils';
33
import * as fs from 'fs-extra';
44
import * as path from 'path';
@@ -47,8 +47,8 @@ export class OutputChannelLogger extends DisposableObject implements Logger {
4747
this.push(this.outputChannel);
4848
}
4949

50-
init(ctx: ExtensionContext): void {
51-
this.additionalLogLocationPath = path.join(ctx.storagePath || ctx.globalStoragePath, this.title);
50+
init(storagePath: string): void {
51+
this.additionalLogLocationPath = path.join(storagePath, this.title);
5252

5353
// clear out any old state from previous runs
5454
fs.remove(this.additionalLogLocationPath);

extensions/ql-vscode/test/pure-tests/logging.test.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,38 +48,24 @@ describe('OutputChannelLogger tests', () => {
4848
});
4949

5050
it('should create a side log in the workspace area', async () => {
51-
await sideLogTest('storagePath', 'globalStoragePath');
52-
});
53-
54-
it('should create a side log in the global area', async () => {
55-
await sideLogTest('globalStoragePath', 'storagePath');
56-
});
57-
58-
async function sideLogTest(expectedArea: string, otherArea: string): Promise<void> {
59-
logger.init({
60-
[expectedArea]: tempFolders[expectedArea].name,
61-
[otherArea]: undefined
62-
});
51+
logger.init(tempFolders.storagePath.name);
6352

6453
await logger.log('xxx', { additionalLogLocation: 'first' });
6554
await logger.log('yyy', { additionalLogLocation: 'second' });
6655
await logger.log('zzz', { additionalLogLocation: 'first', trailingNewline: false });
6756
await logger.log('aaa');
6857

6958
// expect 2 side logs
70-
const testLoggerFolder = path.join(tempFolders[expectedArea].name, 'test-logger');
59+
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');
7160
expect(fs.readdirSync(testLoggerFolder).length).to.equal(2);
72-
expect(fs.readdirSync(tempFolders[otherArea].name).length).to.equal(0);
7361

7462
// contents
7563
expect(fs.readFileSync(path.join(testLoggerFolder, 'first'), 'utf8')).to.equal('xxx\nzzz');
7664
expect(fs.readFileSync(path.join(testLoggerFolder, 'second'), 'utf8')).to.equal('yyy\n');
77-
}
65+
});
7866

7967
it('should delete side logs on dispose', async () => {
80-
logger.init({
81-
storagePath: tempFolders.storagePath.name
82-
});
68+
logger.init(tempFolders.storagePath.name);
8369
await logger.log('xxx', { additionalLogLocation: 'first' });
8470
await logger.log('yyy', { additionalLogLocation: 'second' });
8571

@@ -94,9 +80,7 @@ describe('OutputChannelLogger tests', () => {
9480
});
9581

9682
it('should remove an additional log location', async () => {
97-
logger.init({
98-
storagePath: tempFolders.storagePath.name
99-
});
83+
logger.init(tempFolders.storagePath.name);
10084
await logger.log('xxx', { additionalLogLocation: 'first' });
10185
await logger.log('yyy', { additionalLogLocation: 'second' });
10286

@@ -112,9 +96,7 @@ describe('OutputChannelLogger tests', () => {
11296

11397
it('should delete an existing folder on init', async () => {
11498
fs.createFileSync(path.join(tempFolders.storagePath.name, 'test-logger', 'xxx'));
115-
logger.init({
116-
storagePath: tempFolders.storagePath.name
117-
});
99+
logger.init(tempFolders.storagePath.name);
118100
// should be empty dir
119101

120102
const testLoggerFolder = path.join(tempFolders.storagePath.name, 'test-logger');

0 commit comments

Comments
 (0)