Skip to content

Commit bc08cbe

Browse files
authored
Tidy up and add test for getting query metadata (#1050)
* Move/rename query metadata function * Add test for `tryGetQueryMetadata` * Split into two tests
1 parent 6e2e72a commit bc08cbe

5 files changed

Lines changed: 79 additions & 21 deletions

File tree

extensions/ql-vscode/src/helpers.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { CodeQLCliServer, QlpacksInfo } from './cli';
1313
import { UserCancellationException } from './commandRunner';
1414
import { logger } from './logging';
15+
import { QueryMetadata } from './pure/interface-types';
1516

1617
/**
1718
* Show an error message and log it to the console
@@ -516,3 +517,19 @@ export async function askForLanguage(cliServer: CodeQLCliServer, throwOnEmpty =
516517
}
517518
return language;
518519
}
520+
521+
/**
522+
* Gets metadata for a query, if it exists.
523+
* @param cliServer The CLI server.
524+
* @param queryPath The path to the query.
525+
* @returns A promise that resolves to the query metadata, if available.
526+
*/
527+
export async function tryGetQueryMetadata(cliServer: CodeQLCliServer, queryPath: string): Promise<QueryMetadata | undefined> {
528+
try {
529+
return await cliServer.resolveMetadata(queryPath);
530+
} catch (e) {
531+
// Ignore errors and provide no metadata.
532+
void logger.log(`Couldn't resolve metadata for ${queryPath}: ${e}`);
533+
return;
534+
}
535+
}

extensions/ql-vscode/src/remote-queries/run-remote-query.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ import * as path from 'path';
33
import * as yaml from 'js-yaml';
44
import * as fs from 'fs-extra';
55
import * as tmp from 'tmp-promise';
6-
import { askForLanguage, findLanguage, getOnDiskWorkspaceFolders, showAndLogErrorMessage, showAndLogInformationMessage, showInformationMessageWithAction } from '../helpers';
6+
import {
7+
askForLanguage,
8+
findLanguage,
9+
getOnDiskWorkspaceFolders,
10+
showAndLogErrorMessage,
11+
showAndLogInformationMessage,
12+
showInformationMessageWithAction,
13+
tryGetQueryMetadata
14+
} from '../helpers';
715
import { Credentials } from '../authentication';
816
import * as cli from '../cli';
917
import { logger } from '../logging';
1018
import { getRemoteControllerRepo, getRemoteRepositoryLists, setRemoteControllerRepo } from '../config';
11-
import { getQueryMetadata, tmpDir } from '../run-queries';
19+
import { tmpDir } from '../run-queries';
1220
import { ProgressCallback, UserCancellationException } from '../commandRunner';
1321
import { OctokitResponse } from '@octokit/types/dist-types';
1422
import { RemoteQuery } from './remote-query';
@@ -324,7 +332,7 @@ export async function runRemoteQuery(
324332

325333
const workflowRunId = await runRemoteQueriesApiRequest(credentials, ref, language, repositories, owner, repo, base64Pack, dryRun);
326334
const queryStartTime = new Date();
327-
const queryMetadata = await getQueryMetadata(cliServer, queryFile);
335+
const queryMetadata = await tryGetQueryMetadata(cliServer, queryFile);
328336

329337
if (dryRun) {
330338
return { queryDirPath: remoteQueryDir.path };

extensions/ql-vscode/src/run-queries.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { ErrorCodes, ResponseError } from 'vscode-languageclient';
1717
import * as cli from './cli';
1818
import * as config from './config';
1919
import { DatabaseItem } from './databases';
20-
import { getOnDiskWorkspaceFolders, showAndLogErrorMessage } from './helpers';
20+
import { getOnDiskWorkspaceFolders, showAndLogErrorMessage, tryGetQueryMetadata } from './helpers';
2121
import { ProgressCallback, UserCancellationException } from './commandRunner';
2222
import { DatabaseInfo, QueryMetadata, ResultsPaths } from './pure/interface-types';
2323
import { logger } from './logging';
@@ -612,7 +612,7 @@ export async function compileAndRunQueryAgainstDatabase(
612612
};
613613

614614
// Read the query metadata if possible, to use in the UI.
615-
const metadata = await getQueryMetadata(cliServer, qlProgram.queryPath);
615+
const metadata = await tryGetQueryMetadata(cliServer, qlProgram.queryPath);
616616

617617
let availableMlModels: cli.MlModelInfo[] = [];
618618
// The `capabilities.untrustedWorkspaces.restrictedConfigurations` entry in package.json doesn't
@@ -712,22 +712,6 @@ const compilationFailedErrorTail = ' compilation failed. Please make sure there
712712
' and the query and database use the same target language. For more details on the error, go to View > Output,' +
713713
' and choose CodeQL Query Server from the dropdown.';
714714

715-
/**
716-
* Gets metadata for a query, if it exists.
717-
* @param cliServer The CLI server.
718-
* @param queryPath The path to the query.
719-
* @returns A promise that resolves to the query metadata, if available.
720-
*/
721-
export async function getQueryMetadata(cliServer: cli.CodeQLCliServer, queryPath: string): Promise<QueryMetadata | undefined> {
722-
try {
723-
return await cliServer.resolveMetadata(queryPath);
724-
} catch (e) {
725-
// Ignore errors and provide no metadata.
726-
void logger.log(`Couldn't resolve metadata for ${queryPath}: ${e}`);
727-
return;
728-
}
729-
}
730-
731715
function createSyntheticResult(
732716
query: QueryInfo,
733717
db: DatabaseItem,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* @name This is the name
3+
* @kind problem
4+
* @problem.severity warning
5+
* @id javascript/example/test-query
6+
*/
7+
18
import javascript
29

310
select 1
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as path from 'path';
2+
import { extensions } from 'vscode';
3+
import 'mocha';
4+
5+
import { CodeQLCliServer } from '../../cli';
6+
import { CodeQLExtensionInterface } from '../../extension';
7+
import { tryGetQueryMetadata } from '../../helpers';
8+
import { expect } from 'chai';
9+
10+
describe('helpers (with CLI)', function() {
11+
const baseDir = path.join(__dirname, '../../../src/vscode-tests/cli-integration');
12+
13+
// up to 3 minutes per test
14+
this.timeout(3 * 60 * 1000);
15+
16+
let cli: CodeQLCliServer;
17+
18+
beforeEach(async () => {
19+
const extension = await extensions.getExtension<CodeQLExtensionInterface | Record<string, never>>('GitHub.vscode-codeql')!.activate();
20+
if ('cliServer' in extension) {
21+
cli = extension.cliServer;
22+
} else {
23+
throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.');
24+
}
25+
});
26+
27+
it('should get query metadata when available', async () => {
28+
// Query with metadata
29+
const metadata = await tryGetQueryMetadata(cli, path.join(baseDir, 'data', 'simple-javascript-query.ql'));
30+
31+
expect(metadata!.name).to.equal('This is the name');
32+
expect(metadata!.kind).to.equal('problem');
33+
expect(metadata!.id).to.equal('javascript/example/test-query');
34+
});
35+
36+
it('should handle query with no metadata', async () => {
37+
// Query with empty metadata
38+
const noMetadata = await tryGetQueryMetadata(cli, path.join(baseDir, 'data', 'simple-query.ql'));
39+
40+
expect(noMetadata).to.deep.equal({});
41+
});
42+
});

0 commit comments

Comments
 (0)