Tidy up and add test for getting query metadata (#1050)
* Move/rename query metadata function * Add test for `tryGetQueryMetadata` * Split into two tests
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
|||||||
import { CodeQLCliServer, QlpacksInfo } from './cli';
|
import { CodeQLCliServer, QlpacksInfo } from './cli';
|
||||||
import { UserCancellationException } from './commandRunner';
|
import { UserCancellationException } from './commandRunner';
|
||||||
import { logger } from './logging';
|
import { logger } from './logging';
|
||||||
|
import { QueryMetadata } from './pure/interface-types';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show an error message and log it to the console
|
* Show an error message and log it to the console
|
||||||
@@ -516,3 +517,19 @@ export async function askForLanguage(cliServer: CodeQLCliServer, throwOnEmpty =
|
|||||||
}
|
}
|
||||||
return language;
|
return language;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets metadata for a query, if it exists.
|
||||||
|
* @param cliServer The CLI server.
|
||||||
|
* @param queryPath The path to the query.
|
||||||
|
* @returns A promise that resolves to the query metadata, if available.
|
||||||
|
*/
|
||||||
|
export async function tryGetQueryMetadata(cliServer: CodeQLCliServer, queryPath: string): Promise<QueryMetadata | undefined> {
|
||||||
|
try {
|
||||||
|
return await cliServer.resolveMetadata(queryPath);
|
||||||
|
} catch (e) {
|
||||||
|
// Ignore errors and provide no metadata.
|
||||||
|
void logger.log(`Couldn't resolve metadata for ${queryPath}: ${e}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,12 +3,20 @@ import * as path from 'path';
|
|||||||
import * as yaml from 'js-yaml';
|
import * as yaml from 'js-yaml';
|
||||||
import * as fs from 'fs-extra';
|
import * as fs from 'fs-extra';
|
||||||
import * as tmp from 'tmp-promise';
|
import * as tmp from 'tmp-promise';
|
||||||
import { askForLanguage, findLanguage, getOnDiskWorkspaceFolders, showAndLogErrorMessage, showAndLogInformationMessage, showInformationMessageWithAction } from '../helpers';
|
import {
|
||||||
|
askForLanguage,
|
||||||
|
findLanguage,
|
||||||
|
getOnDiskWorkspaceFolders,
|
||||||
|
showAndLogErrorMessage,
|
||||||
|
showAndLogInformationMessage,
|
||||||
|
showInformationMessageWithAction,
|
||||||
|
tryGetQueryMetadata
|
||||||
|
} from '../helpers';
|
||||||
import { Credentials } from '../authentication';
|
import { Credentials } from '../authentication';
|
||||||
import * as cli from '../cli';
|
import * as cli from '../cli';
|
||||||
import { logger } from '../logging';
|
import { logger } from '../logging';
|
||||||
import { getRemoteControllerRepo, getRemoteRepositoryLists, setRemoteControllerRepo } from '../config';
|
import { getRemoteControllerRepo, getRemoteRepositoryLists, setRemoteControllerRepo } from '../config';
|
||||||
import { getQueryMetadata, tmpDir } from '../run-queries';
|
import { tmpDir } from '../run-queries';
|
||||||
import { ProgressCallback, UserCancellationException } from '../commandRunner';
|
import { ProgressCallback, UserCancellationException } from '../commandRunner';
|
||||||
import { OctokitResponse } from '@octokit/types/dist-types';
|
import { OctokitResponse } from '@octokit/types/dist-types';
|
||||||
import { RemoteQuery } from './remote-query';
|
import { RemoteQuery } from './remote-query';
|
||||||
@@ -324,7 +332,7 @@ export async function runRemoteQuery(
|
|||||||
|
|
||||||
const workflowRunId = await runRemoteQueriesApiRequest(credentials, ref, language, repositories, owner, repo, base64Pack, dryRun);
|
const workflowRunId = await runRemoteQueriesApiRequest(credentials, ref, language, repositories, owner, repo, base64Pack, dryRun);
|
||||||
const queryStartTime = new Date();
|
const queryStartTime = new Date();
|
||||||
const queryMetadata = await getQueryMetadata(cliServer, queryFile);
|
const queryMetadata = await tryGetQueryMetadata(cliServer, queryFile);
|
||||||
|
|
||||||
if (dryRun) {
|
if (dryRun) {
|
||||||
return { queryDirPath: remoteQueryDir.path };
|
return { queryDirPath: remoteQueryDir.path };
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import { ErrorCodes, ResponseError } from 'vscode-languageclient';
|
|||||||
import * as cli from './cli';
|
import * as cli from './cli';
|
||||||
import * as config from './config';
|
import * as config from './config';
|
||||||
import { DatabaseItem } from './databases';
|
import { DatabaseItem } from './databases';
|
||||||
import { getOnDiskWorkspaceFolders, showAndLogErrorMessage } from './helpers';
|
import { getOnDiskWorkspaceFolders, showAndLogErrorMessage, tryGetQueryMetadata } from './helpers';
|
||||||
import { ProgressCallback, UserCancellationException } from './commandRunner';
|
import { ProgressCallback, UserCancellationException } from './commandRunner';
|
||||||
import { DatabaseInfo, QueryMetadata, ResultsPaths } from './pure/interface-types';
|
import { DatabaseInfo, QueryMetadata, ResultsPaths } from './pure/interface-types';
|
||||||
import { logger } from './logging';
|
import { logger } from './logging';
|
||||||
@@ -612,7 +612,7 @@ export async function compileAndRunQueryAgainstDatabase(
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Read the query metadata if possible, to use in the UI.
|
// Read the query metadata if possible, to use in the UI.
|
||||||
const metadata = await getQueryMetadata(cliServer, qlProgram.queryPath);
|
const metadata = await tryGetQueryMetadata(cliServer, qlProgram.queryPath);
|
||||||
|
|
||||||
let availableMlModels: cli.MlModelInfo[] = [];
|
let availableMlModels: cli.MlModelInfo[] = [];
|
||||||
// The `capabilities.untrustedWorkspaces.restrictedConfigurations` entry in package.json doesn't
|
// The `capabilities.untrustedWorkspaces.restrictedConfigurations` entry in package.json doesn't
|
||||||
@@ -712,22 +712,6 @@ const compilationFailedErrorTail = ' compilation failed. Please make sure there
|
|||||||
' and the query and database use the same target language. For more details on the error, go to View > Output,' +
|
' and the query and database use the same target language. For more details on the error, go to View > Output,' +
|
||||||
' and choose CodeQL Query Server from the dropdown.';
|
' and choose CodeQL Query Server from the dropdown.';
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets metadata for a query, if it exists.
|
|
||||||
* @param cliServer The CLI server.
|
|
||||||
* @param queryPath The path to the query.
|
|
||||||
* @returns A promise that resolves to the query metadata, if available.
|
|
||||||
*/
|
|
||||||
export async function getQueryMetadata(cliServer: cli.CodeQLCliServer, queryPath: string): Promise<QueryMetadata | undefined> {
|
|
||||||
try {
|
|
||||||
return await cliServer.resolveMetadata(queryPath);
|
|
||||||
} catch (e) {
|
|
||||||
// Ignore errors and provide no metadata.
|
|
||||||
void logger.log(`Couldn't resolve metadata for ${queryPath}: ${e}`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createSyntheticResult(
|
function createSyntheticResult(
|
||||||
query: QueryInfo,
|
query: QueryInfo,
|
||||||
db: DatabaseItem,
|
db: DatabaseItem,
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* @name This is the name
|
||||||
|
* @kind problem
|
||||||
|
* @problem.severity warning
|
||||||
|
* @id javascript/example/test-query
|
||||||
|
*/
|
||||||
|
|
||||||
import javascript
|
import javascript
|
||||||
|
|
||||||
select 1
|
select 1
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import * as path from 'path';
|
||||||
|
import { extensions } from 'vscode';
|
||||||
|
import 'mocha';
|
||||||
|
|
||||||
|
import { CodeQLCliServer } from '../../cli';
|
||||||
|
import { CodeQLExtensionInterface } from '../../extension';
|
||||||
|
import { tryGetQueryMetadata } from '../../helpers';
|
||||||
|
import { expect } from 'chai';
|
||||||
|
|
||||||
|
describe('helpers (with CLI)', function() {
|
||||||
|
const baseDir = path.join(__dirname, '../../../src/vscode-tests/cli-integration');
|
||||||
|
|
||||||
|
// up to 3 minutes per test
|
||||||
|
this.timeout(3 * 60 * 1000);
|
||||||
|
|
||||||
|
let cli: CodeQLCliServer;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const extension = await extensions.getExtension<CodeQLExtensionInterface | Record<string, never>>('GitHub.vscode-codeql')!.activate();
|
||||||
|
if ('cliServer' in extension) {
|
||||||
|
cli = extension.cliServer;
|
||||||
|
} else {
|
||||||
|
throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should get query metadata when available', async () => {
|
||||||
|
// Query with metadata
|
||||||
|
const metadata = await tryGetQueryMetadata(cli, path.join(baseDir, 'data', 'simple-javascript-query.ql'));
|
||||||
|
|
||||||
|
expect(metadata!.name).to.equal('This is the name');
|
||||||
|
expect(metadata!.kind).to.equal('problem');
|
||||||
|
expect(metadata!.id).to.equal('javascript/example/test-query');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle query with no metadata', async () => {
|
||||||
|
// Query with empty metadata
|
||||||
|
const noMetadata = await tryGetQueryMetadata(cli, path.join(baseDir, 'data', 'simple-query.ql'));
|
||||||
|
|
||||||
|
expect(noMetadata).to.deep.equal({});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user