Merge pull request #173 from dbartol/dbartol/QLTest
Implement QL Test support
This commit is contained in:
Коммит
9377279b05
|
@ -8,8 +8,7 @@
|
|||
"request": "launch",
|
||||
"runtimeExecutable": "${execPath}",
|
||||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceRoot}/dist/vscode-codeql",
|
||||
"--disable-extensions"
|
||||
"--extensionDevelopmentPath=${workspaceRoot}/dist/vscode-codeql"
|
||||
],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
|
@ -29,8 +28,7 @@
|
|||
"runtimeExecutable": "${execPath}",
|
||||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceRoot}/dist/vscode-codeql",
|
||||
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/test",
|
||||
"--disable-extensions"
|
||||
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/test"
|
||||
],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
|
@ -51,8 +49,7 @@
|
|||
"runtimeExecutable": "${execPath}",
|
||||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceRoot}/dist/vscode-codeql",
|
||||
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/vscode-tests/no-workspace/index",
|
||||
"--disable-extensions"
|
||||
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/vscode-tests/no-workspace/index"
|
||||
],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
|
@ -70,7 +67,7 @@
|
|||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceRoot}/dist/vscode-codeql",
|
||||
"--extensionTestsPath=${workspaceRoot}/extensions/ql-vscode/out/vscode-tests/minimal-workspace/index",
|
||||
"${workspaceRoot}/extensions/ql-vscode/test/data",
|
||||
"${workspaceRoot}/extensions/ql-vscode/test/data"
|
||||
],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
|
|
|
@ -56,6 +56,7 @@ dependencies:
|
|||
style-loader: 0.23.1
|
||||
through2: 3.0.1
|
||||
tmp: 0.1.0
|
||||
tree-kill: 1.2.2
|
||||
ts-loader: 5.4.5
|
||||
ts-node: 8.3.0
|
||||
ts-protoc-gen: 0.9.0
|
||||
|
@ -67,6 +68,8 @@ dependencies:
|
|||
vscode-jsonrpc: 4.0.0
|
||||
vscode-languageclient: 5.2.1
|
||||
vscode-test: 1.2.0
|
||||
vscode-test-adapter-api: 1.7.0
|
||||
vscode-test-adapter-util: 0.7.0
|
||||
webpack: 4.39.1
|
||||
webpack-cli: 3.3.6
|
||||
packages:
|
||||
|
@ -5688,6 +5691,11 @@ packages:
|
|||
dev: false
|
||||
resolution:
|
||||
integrity: sha1-cXuPIgzAu3tE5AUUwisui7xw2Lk=
|
||||
/tree-kill/1.2.2:
|
||||
dev: false
|
||||
hasBin: true
|
||||
resolution:
|
||||
integrity: sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==
|
||||
/true-case-path/2.2.1:
|
||||
dev: false
|
||||
resolution:
|
||||
|
@ -6122,6 +6130,21 @@ packages:
|
|||
dev: false
|
||||
resolution:
|
||||
integrity: sha512-lTmS6AlAlMHOvPQemVwo3CezxBp0sNB95KNPkqp3Nxd5VFEnuG1ByM0zlRWos0zjO3ZWtkvhal0COgiV1xIA4A==
|
||||
/vscode-test-adapter-api/1.7.0:
|
||||
dev: false
|
||||
engines:
|
||||
vscode: ^1.23.0
|
||||
resolution:
|
||||
integrity: sha512-X0rTcoDhDBmpmJuev2C5+GHGZD41nmcRYoSe7iw5e9/aIPTOFve1T1F5x9gb+zXoNQnkXSDibyMkeHDKtIkqCg==
|
||||
/vscode-test-adapter-util/0.7.0:
|
||||
dependencies:
|
||||
tslib: 1.10.0
|
||||
vscode-test-adapter-api: 1.7.0
|
||||
dev: false
|
||||
engines:
|
||||
vscode: ^1.24.0
|
||||
resolution:
|
||||
integrity: sha512-eAsB8koXct5JytvUcV62wLEBCQfsoclauzMLEFT6H0qBr1h8LyRc+dGDcs48pO28yFOo6VV+5AwCRLxTKh7TzQ==
|
||||
/vscode-test/1.2.0:
|
||||
dependencies:
|
||||
http-proxy-agent: 2.1.0
|
||||
|
@ -6433,7 +6456,7 @@ packages:
|
|||
child-process-promise: 2.2.1
|
||||
fs-extra: 8.1.0
|
||||
glob: 7.1.4
|
||||
glob-promise: 3.4.0
|
||||
glob-promise: /glob-promise/3.4.0/glob@7.1.4
|
||||
gulp: 4.0.2
|
||||
gulp-sourcemaps: 2.6.5
|
||||
gulp-typescript: /gulp-typescript/5.0.1/typescript@3.7.2
|
||||
|
@ -6449,7 +6472,7 @@ packages:
|
|||
dev: false
|
||||
name: '@rush-temp/build-tasks'
|
||||
resolution:
|
||||
integrity: sha512-ta2kXnX7phnKrO7rxdJl5A9Vtd8B4RDyoae3vhdI1d+COeITaXDd9xdPxo8lvduPSJTw2+HnzOgOu2pMAKSjTw==
|
||||
integrity: sha512-WBog5Gepo348LYeu7FAZMORJpgx0CNFM+IjgLcKNFYnN9YOCRQogUkRL7ZmBt8WXoew1S5OyVltmEM8XPy8i+w==
|
||||
tarball: 'file:projects/build-tasks.tgz'
|
||||
version: 0.0.0
|
||||
'file:projects/semmle-bqrs.tgz':
|
||||
|
@ -6461,7 +6484,7 @@ packages:
|
|||
dev: false
|
||||
name: '@rush-temp/semmle-bqrs'
|
||||
resolution:
|
||||
integrity: sha512-24GdnvMbGfQIWMfgDhift+kYJDnG7dX03NrpX4ajZ2rckteysvq2/K7XI1OXGvUuqrt3m0/+GRDHpSI9XKDJJA==
|
||||
integrity: sha512-ufPu8zLXf9JvPCRycWLiFTDb5rZ7bqxQZuiFjy1DAxnatEG5VJITPSXwDFVc11qpjJpaFd4hI+4QtOda7d5zww==
|
||||
tarball: 'file:projects/semmle-bqrs.tgz'
|
||||
version: 0.0.0
|
||||
'file:projects/semmle-io-node.tgz':
|
||||
|
@ -6473,7 +6496,7 @@ packages:
|
|||
dev: false
|
||||
name: '@rush-temp/semmle-io-node'
|
||||
resolution:
|
||||
integrity: sha512-Bj0ax/bASrHV7tamOuXZZdd3UOB4NBKdjdszIRaDvDRTu8RlEst+TVoUhkfy30qb2/6ePp3/juOJyyiBJN7u8Q==
|
||||
integrity: sha512-jB3C3WWEI991Kr3knPKUwqqNi040WmYCubLJJG7AK1nz3V1YjmyLHIAdjqwOgDNXYKCQPC6tlaEgljbs2Q/kIQ==
|
||||
tarball: 'file:projects/semmle-io-node.tgz'
|
||||
version: 0.0.0
|
||||
'file:projects/semmle-io.tgz':
|
||||
|
@ -6484,7 +6507,7 @@ packages:
|
|||
dev: false
|
||||
name: '@rush-temp/semmle-io'
|
||||
resolution:
|
||||
integrity: sha512-NtyviDSevxbd+hj4J66LucOzo8LU2hJ1Jh0eHw0Qu3tRZPUT8HcQlseyy29AvZR8n8eppfEZiAm/JdiHfmRPMA==
|
||||
integrity: sha512-6DFvjDclWTihDToSf31Hh+wQNhLGkA37l4QajeW/w6gS4NHjSrFL1qBlS7dLUY80VC/8nQJH9foe3r6dfEfQYw==
|
||||
tarball: 'file:projects/semmle-io.tgz'
|
||||
version: 0.0.0
|
||||
'file:projects/semmle-vscode-utils.tgz':
|
||||
|
@ -6496,19 +6519,20 @@ packages:
|
|||
dev: false
|
||||
name: '@rush-temp/semmle-vscode-utils'
|
||||
resolution:
|
||||
integrity: sha512-5y5r8SDoN9Fp44naC9gUe8rOexeckXg2T0h9QCJAIcEgnFqOxzRc6Rv9gbMUStFKNh+rFlvmYmgPAdg5QkfgUg==
|
||||
integrity: sha512-yE5S1wsnrsJ8lTt9O9ALedlvH37M9sWQha7sL5iQ3P6dn2KsyUItGsJDnFoh2f0wy3TpZuj3p/KTEDBbgjXBGg==
|
||||
tarball: 'file:projects/semmle-vscode-utils.tgz'
|
||||
version: 0.0.0
|
||||
'file:projects/typescript-config.tgz':
|
||||
dev: false
|
||||
name: '@rush-temp/typescript-config'
|
||||
resolution:
|
||||
integrity: sha512-XuUIySaNoooIduvehnlKYaHqZJmmQoCqB1RtKhNszjCYZaSSJAnKVucViWBf5oNLKSNP7NchrD7gcoBlQ3xYvw==
|
||||
integrity: sha512-kSFyvKy63jUHFVXQEzALiYfsTdn7J+Y7PcqtUVo9GndU5b5Xh3rBpVbZD1QN8+y8GfT0m/sdZZQVyH0h+On11Q==
|
||||
tarball: 'file:projects/typescript-config.tgz'
|
||||
version: 0.0.0
|
||||
'file:projects/vscode-codeql.tgz':
|
||||
dependencies:
|
||||
'@types/chai': 4.1.7
|
||||
'@types/child-process-promise': 2.2.1
|
||||
'@types/classnames': 2.2.9
|
||||
'@types/fs-extra': 8.0.0
|
||||
'@types/glob': 7.1.1
|
||||
|
@ -6548,6 +6572,7 @@ packages:
|
|||
style-loader: 0.23.1
|
||||
through2: 3.0.1
|
||||
tmp: 0.1.0
|
||||
tree-kill: 1.2.2
|
||||
ts-loader: /ts-loader/5.4.5/typescript@3.7.2
|
||||
ts-node: /ts-node/8.3.0/typescript@3.7.2
|
||||
ts-protoc-gen: 0.9.0
|
||||
|
@ -6559,13 +6584,15 @@ packages:
|
|||
vscode-jsonrpc: 4.0.0
|
||||
vscode-languageclient: 5.2.1
|
||||
vscode-test: 1.2.0
|
||||
vscode-test-adapter-api: 1.7.0
|
||||
vscode-test-adapter-util: 0.7.0
|
||||
webpack: 4.39.1
|
||||
webpack-cli: /webpack-cli/3.3.6/webpack@4.39.1
|
||||
xml2js: 0.4.19
|
||||
dev: false
|
||||
name: '@rush-temp/vscode-codeql'
|
||||
resolution:
|
||||
integrity: sha512-DE97bdxda65gVLZne73QzBpj2hyCbyzvQiRZxrJqDP1rkF62EGNohBSmlEQs8H2Jp8hxh5RhPhm/yUx70G7KEA==
|
||||
integrity: sha512-pHdIjv8YEIRicmnIzv2OB5cqdCRoKcF2ZbylLcaR+VP28TzoyYZK5q2anIYayibWa3Ik4la4pVGnSa344IatvA==
|
||||
tarball: 'file:projects/vscode-codeql.tgz'
|
||||
version: 0.0.0
|
||||
registry: 'https://registry.npmjs.org/'
|
||||
|
@ -6629,6 +6656,7 @@ specifiers:
|
|||
style-loader: ~0.23.1
|
||||
through2: ^3.0.1
|
||||
tmp: ^0.1.0
|
||||
tree-kill: ~1.2.2
|
||||
ts-loader: ^5.4.5
|
||||
ts-node: ^8.3.0
|
||||
ts-protoc-gen: ^0.9.0
|
||||
|
@ -6640,5 +6668,7 @@ specifiers:
|
|||
vscode-jsonrpc: ^4.0.0
|
||||
vscode-languageclient: ^5.2.1
|
||||
vscode-test: ^1.0.0
|
||||
vscode-test-adapter-api: ~1.7.0
|
||||
vscode-test-adapter-util: ~0.7.0
|
||||
webpack: ^4.38.0
|
||||
webpack-cli: ^3.3.2
|
||||
|
|
|
@ -18,10 +18,14 @@
|
|||
"categories": [
|
||||
"Programming Languages"
|
||||
],
|
||||
"extensionDependencies": [
|
||||
"hbenl.vscode-test-explorer"
|
||||
],
|
||||
"activationEvents": [
|
||||
"onLanguage:ql",
|
||||
"onView:codeQLDatabases",
|
||||
"onView:codeQLQueryHistory",
|
||||
"onView:test-explorer",
|
||||
"onCommand:codeQL.checkForUpdatesToCLI",
|
||||
"onCommand:codeQL.chooseDatabase",
|
||||
"onCommand:codeQL.setCurrentDatabase",
|
||||
|
@ -132,6 +136,14 @@
|
|||
"type": "string",
|
||||
"default": "[%t] %q on %d - %s",
|
||||
"description": "Default string for how to label query history items. %t is the time of the query, %q is the query name, %d is the database name, and %s is a status string."
|
||||
},
|
||||
"codeQL.runningTests.numberOfThreads": {
|
||||
"scope": "window",
|
||||
"type": "integer",
|
||||
"default": 1,
|
||||
"minimum": 1,
|
||||
"maximum": 1024,
|
||||
"description": "Number of threads for running CodeQL tests."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -211,6 +223,14 @@
|
|||
{
|
||||
"command": "codeQL.restartQueryServer",
|
||||
"title": "CodeQL: Restart Query Server"
|
||||
},
|
||||
{
|
||||
"command": "codeQLTests.showOutputDifferences",
|
||||
"title": "CodeQL: Show Test Output Differences"
|
||||
},
|
||||
{
|
||||
"command": "codeQLTests.acceptOutput",
|
||||
"title": "CodeQL: Accept Test Output"
|
||||
}
|
||||
],
|
||||
"menus": {
|
||||
|
@ -251,6 +271,16 @@
|
|||
"command": "codeQLQueryHistory.setLabel",
|
||||
"group": "9_qlCommands",
|
||||
"when": "view == codeQLQueryHistory"
|
||||
},
|
||||
{
|
||||
"command": "codeQLTests.showOutputDifferences",
|
||||
"group": "qltest@1",
|
||||
"when": "view == test-explorer && viewItem == testWithSource"
|
||||
},
|
||||
{
|
||||
"command": "codeQLTests.acceptOutput",
|
||||
"group": "qltest@2",
|
||||
"when": "view == test-explorer && viewItem == testWithSource"
|
||||
}
|
||||
],
|
||||
"explorer/context": [
|
||||
|
@ -348,6 +378,7 @@
|
|||
"format": "tsfmt -r"
|
||||
},
|
||||
"dependencies": {
|
||||
"child-process-promise": "^2.2.1",
|
||||
"classnames": "~2.2.6",
|
||||
"fs-extra": "^8.1.0",
|
||||
"glob-promise": "^3.4.0",
|
||||
|
@ -359,12 +390,16 @@
|
|||
"semmle-io-node": "^0.0.1",
|
||||
"semmle-vscode-utils": "^0.0.1",
|
||||
"tmp": "^0.1.0",
|
||||
"tree-kill": "~1.2.2",
|
||||
"unzipper": "~0.10.5",
|
||||
"vscode-jsonrpc": "^4.0.0",
|
||||
"vscode-languageclient": "^5.2.1"
|
||||
"vscode-languageclient": "^5.2.1",
|
||||
"vscode-test-adapter-api": "~1.7.0",
|
||||
"vscode-test-adapter-util": "~0.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/chai": "^4.1.7",
|
||||
"@types/child-process-promise": "^2.2.1",
|
||||
"@types/classnames": "~2.2.9",
|
||||
"@types/fs-extra": "^8.0.0",
|
||||
"@types/glob": "^7.1.1",
|
||||
|
@ -385,7 +420,6 @@
|
|||
"@types/xml2js": "~0.4.4",
|
||||
"build-tasks": "^0.0.1",
|
||||
"chai": "^4.2.0",
|
||||
"child-process-promise": "^2.2.1",
|
||||
"css-loader": "~3.1.0",
|
||||
"glob": "^7.1.4",
|
||||
"gulp": "^4.0.2",
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
import * as child_process from "child_process";
|
||||
import * as child_process from 'child_process';
|
||||
import * as cpp from 'child-process-promise';
|
||||
import * as fs from 'fs-extra';
|
||||
import * as path from 'path';
|
||||
import * as sarif from 'sarif';
|
||||
import * as tk from 'tree-kill';
|
||||
import * as util from 'util';
|
||||
import { Logger, ProgressReporter } from "./logging";
|
||||
import { Disposable } from "vscode";
|
||||
import { DistributionProvider } from "./distribution";
|
||||
import { SortDirection, QueryMetadata } from "./interface-types";
|
||||
import { assertNever } from "./helpers-pure";
|
||||
import { SortDirection, QueryMetadata } from './interface-types';
|
||||
import { Logger, ProgressReporter } from './logging';
|
||||
import { Disposable, CancellationToken } from 'vscode';
|
||||
import { DistributionProvider } from './distribution';
|
||||
import { assertNever } from './helpers-pure';
|
||||
import { Readable } from 'stream';
|
||||
import { StringDecoder } from 'string_decoder';
|
||||
|
||||
/**
|
||||
* The version of the SARIF format that we are using.
|
||||
|
@ -62,6 +66,31 @@ export interface SourceInfo {
|
|||
sourceLocationPrefix: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The expected output of `codeql resolve tests`.
|
||||
*/
|
||||
export type ResolvedTests = string[];
|
||||
|
||||
/**
|
||||
* Options for `codeql test run`.
|
||||
*/
|
||||
export interface TestRunOptions {
|
||||
cancellationToken?: CancellationToken;
|
||||
logger?: Logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event fired by `codeql test run`.
|
||||
*/
|
||||
export interface TestCompleted {
|
||||
test: string;
|
||||
pass: boolean;
|
||||
messages: string[];
|
||||
compilationMs: number;
|
||||
evaluationMs: number;
|
||||
expected: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class manages a cli server started by `codeql execute cli-server` to
|
||||
* run commands without the overhead of starting a new java
|
||||
|
@ -141,14 +170,22 @@ export class CodeQLCliServer implements Disposable {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the path to the CodeQL CLI distribution, or throw an exception if not found.
|
||||
*/
|
||||
private async getCodeQlPath(): Promise<string> {
|
||||
const codeqlPath = await this.config.getCodeQlPathWithoutVersionCheck();
|
||||
if (!codeqlPath) {
|
||||
throw new Error('Failed to find CodeQL distribution.');
|
||||
}
|
||||
return codeqlPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch the cli server
|
||||
*/
|
||||
private async launchProcess(): Promise<child_process.ChildProcessWithoutNullStreams> {
|
||||
const config = await this.config.getCodeQlPathWithoutVersionCheck();
|
||||
if (!config) {
|
||||
throw new Error("Failed to find codeql distribution")
|
||||
}
|
||||
const config = await this.getCodeQlPath();
|
||||
return spawnServer(config, "CodeQL CLI Server", ["execute", "cli-server"], [], this.logger, _data => { })
|
||||
}
|
||||
|
||||
|
@ -233,6 +270,87 @@ export class CodeQLCliServer implements Disposable {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs an asynchronous CodeQL CLI command without invoking the CLI server, returning any events
|
||||
* fired by the command as an asynchronous generator.
|
||||
*
|
||||
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
|
||||
* @param commandArgs The arguments to pass to the `codeql` command.
|
||||
* @param cancellationToken CancellationToken to terminate the test process.
|
||||
* @param logger Logger to write text output from the command.
|
||||
* @returns The sequence of async events produced by the command.
|
||||
*/
|
||||
private async* runAsyncCodeQlCliCommandInternal(
|
||||
command: string[],
|
||||
commandArgs: string[],
|
||||
cancellationToken?: CancellationToken,
|
||||
logger?: Logger
|
||||
): AsyncGenerator<string, void, unknown> {
|
||||
// Add format argument first, in case commandArgs contains positional parameters.
|
||||
const args = [
|
||||
...command,
|
||||
'--format', 'jsonz',
|
||||
...commandArgs
|
||||
];
|
||||
|
||||
// Spawn the CodeQL process
|
||||
const codeqlPath = await this.getCodeQlPath();
|
||||
const childPromise = cpp.spawn(codeqlPath, args);
|
||||
const child = childPromise.childProcess;
|
||||
|
||||
let cancellationRegistration: Disposable | undefined = undefined;
|
||||
try {
|
||||
if (cancellationToken !== undefined) {
|
||||
cancellationRegistration = cancellationToken.onCancellationRequested(_e => {
|
||||
tk(child.pid);
|
||||
});
|
||||
}
|
||||
if (logger !== undefined) {
|
||||
// The human-readable output goes to stderr.
|
||||
logStream(child.stderr!, logger);
|
||||
}
|
||||
|
||||
for await (const event of await splitStreamAtSeparators(child.stdout!, ['\0'])) {
|
||||
yield event;
|
||||
}
|
||||
|
||||
await childPromise;
|
||||
}
|
||||
finally {
|
||||
if (cancellationRegistration !== undefined) {
|
||||
cancellationRegistration.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs an asynchronous CodeQL CLI command without invoking the CLI server, returning any events
|
||||
* fired by the command as an asynchronous generator.
|
||||
*
|
||||
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
|
||||
* @param commandArgs The arguments to pass to the `codeql` command.
|
||||
* @param description Description of the action being run, to be shown in log and error messages.
|
||||
* @param cancellationToken CancellationToken to terminate the test process.
|
||||
* @param logger Logger to write text output from the command.
|
||||
* @returns The sequence of async events produced by the command.
|
||||
*/
|
||||
public async* runAsyncCodeQlCliCommand<EventType>(
|
||||
command: string[],
|
||||
commandArgs: string[],
|
||||
description: string,
|
||||
cancellationToken?: CancellationToken,
|
||||
logger?: Logger
|
||||
): AsyncGenerator<EventType, void, unknown> {
|
||||
for await (const event of await this.runAsyncCodeQlCliCommandInternal(command, commandArgs,
|
||||
cancellationToken, logger)) {
|
||||
try {
|
||||
yield JSON.parse(event) as EventType;
|
||||
} catch (err) {
|
||||
throw new Error(`Parsing output of ${description} failed: ${err.stderr || err}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a CodeQL CLI command on the server, returning the output as a string.
|
||||
* @param command The `codeql` command to be run, provided as an array of command/subcommand names.
|
||||
|
@ -298,6 +416,39 @@ export class CodeQLCliServer implements Disposable {
|
|||
return await this.runJsonCodeQlCliCommand<QuerySetup>(['resolve', 'library-path'], subcommandArgs, "Resolving library paths");
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds all available QL tests in a given directory.
|
||||
* @param testPath Root of directory tree to search for tests.
|
||||
* @returns The list of tests that were found.
|
||||
*/
|
||||
public async resolveTests(testPath: string): Promise<ResolvedTests> {
|
||||
const subcommandArgs = [
|
||||
testPath
|
||||
];
|
||||
return await this.runJsonCodeQlCliCommand<ResolvedTests>(['resolve', 'tests'], subcommandArgs, 'Resolving tests');
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs QL tests.
|
||||
* @param testPaths Full paths of the tests to run.
|
||||
* @param workspaces Workspace paths to use as search paths for QL packs.
|
||||
* @param options Additional options.
|
||||
*/
|
||||
public async* runTests(testPaths: string[], workspaces: string[], options: TestRunOptions):
|
||||
AsyncGenerator<TestCompleted, void, unknown> {
|
||||
|
||||
const subcommandArgs = [
|
||||
'--additional-packs', workspaces.join(path.delimiter),
|
||||
'--threads', '8',
|
||||
...testPaths
|
||||
];
|
||||
|
||||
for await (const event of await this.runAsyncCodeQlCliCommand<TestCompleted>(['test', 'run'],
|
||||
subcommandArgs, 'Run CodeQL Tests', options.cancellationToken, options.logger)) {
|
||||
yield event;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the metadata for a query.
|
||||
* @param queryPath The path to the query.
|
||||
|
@ -409,11 +560,16 @@ export class CodeQLCliServer implements Disposable {
|
|||
|
||||
/**
|
||||
* Gets information about available qlpacks
|
||||
* @param searchPath A list of directories to search for qlpacks
|
||||
* @param additionalPacks A list of directories to search for qlpacks before searching in `searchPath`.
|
||||
* @param searchPath A list of directories to search for packs not found in `additionalPacks`. If undefined,
|
||||
* the default CLI search path is used.
|
||||
* @returns A dictionary mapping qlpack name to the directory it comes from
|
||||
*/
|
||||
resolveQlpacks(searchPath: string[]): Promise<QlpacksInfo> {
|
||||
const args = ['--additional-packs', searchPath.join(path.delimiter)];
|
||||
resolveQlpacks(additionalPacks: string[], searchPath?: string[]): Promise<QlpacksInfo> {
|
||||
const args = ['--additional-packs', additionalPacks.join(path.delimiter)];
|
||||
if (searchPath !== undefined) {
|
||||
args.push('--search-path', searchPath.join(path.delimiter));
|
||||
}
|
||||
|
||||
return this.runJsonCodeQlCliCommand<QlpacksInfo>(
|
||||
['resolve', 'qlpacks'],
|
||||
|
@ -503,3 +659,100 @@ export async function runCodeQlCliCommand(codeQlPath: string, command: string[],
|
|||
throw new Error(`${description} failed: ${err.stderr || err}`)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Buffer to hold state used when splitting a text stream into lines.
|
||||
*/
|
||||
class SplitBuffer {
|
||||
private readonly decoder = new StringDecoder('utf8');
|
||||
private readonly maxSeparatorLength: number;
|
||||
private buffer = '';
|
||||
private searchIndex = 0;
|
||||
|
||||
constructor(private readonly separators: readonly string[]) {
|
||||
this.maxSeparatorLength = separators.map(s => s.length).reduce((a, b) => Math.max(a, b), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append new text data to the buffer.
|
||||
* @param chunk The chunk of data to append.
|
||||
*/
|
||||
public addChunk(chunk: Buffer): void {
|
||||
this.buffer += this.decoder.write(chunk);
|
||||
}
|
||||
|
||||
/**
|
||||
* Signal that the end of the input stream has been reached.
|
||||
*/
|
||||
public end(): void {
|
||||
this.buffer += this.decoder.end();
|
||||
this.buffer += this.separators[0]; // Append a separator to the end to ensure the last line is returned.
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the next full line from the buffer, if one is available.
|
||||
* @returns The text of the next available full line (without the separator), or `undefined` if no
|
||||
* line is available.
|
||||
*/
|
||||
public getNextLine(): string | undefined {
|
||||
while (this.searchIndex <= (this.buffer.length - this.maxSeparatorLength)) {
|
||||
for (const separator of this.separators) {
|
||||
if (this.buffer.startsWith(separator, this.searchIndex)) {
|
||||
const line = this.buffer.substr(0, this.searchIndex);
|
||||
this.buffer = this.buffer.substr(this.searchIndex + separator.length);
|
||||
this.searchIndex = 0;
|
||||
return line;
|
||||
}
|
||||
}
|
||||
this.searchIndex++;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a text stream into lines based on a list of valid line separators.
|
||||
* @param stream The text stream to split. This stream will be fully consumed.
|
||||
* @param separators The list of strings that act as line separators.
|
||||
* @returns A sequence of lines (not including separators).
|
||||
*/
|
||||
async function* splitStreamAtSeparators(stream: Readable, separators: string[]):
|
||||
AsyncGenerator<string, void, unknown> {
|
||||
|
||||
const buffer = new SplitBuffer(separators);
|
||||
for await (const chunk of stream) {
|
||||
buffer.addChunk(chunk);
|
||||
let line: string | undefined;
|
||||
do {
|
||||
line = buffer.getNextLine();
|
||||
if (line !== undefined) {
|
||||
yield line;
|
||||
}
|
||||
} while (line !== undefined);
|
||||
}
|
||||
buffer.end();
|
||||
let line: string | undefined;
|
||||
do {
|
||||
line = buffer.getNextLine();
|
||||
if (line !== undefined) {
|
||||
yield line;
|
||||
}
|
||||
} while (line !== undefined);
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard line endings for splitting human-readable text.
|
||||
*/
|
||||
const lineEndings = ['\r\n', '\r', '\n'];
|
||||
|
||||
/**
|
||||
* Log a text stream to a `Logger` interface.
|
||||
* @param stream The stream to log.
|
||||
* @param logger The logger that will consume the stream output.
|
||||
*/
|
||||
async function logStream(stream: Readable, logger: Logger): Promise<void> {
|
||||
for await (const line of await splitStreamAtSeparators(stream, lineEndings)) {
|
||||
logger.log(line);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
import { DisposableObject } from 'semmle-vscode-utils';
|
||||
|
||||
/**
|
||||
* Base class for "discovery" operations, which scan the file system to find specific kinds of
|
||||
* files. This class automatically prevents more than one discovery operation from running at the
|
||||
* same time.
|
||||
*/
|
||||
export abstract class Discovery<T> extends DisposableObject {
|
||||
private retry = false;
|
||||
private discoveryInProgress = false;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Force the discovery process to run. Normally invoked by the derived class when a relevant file
|
||||
* system change is detected.
|
||||
*/
|
||||
public refresh(): void {
|
||||
// We avoid having multiple discovery operations in progress at the same time. Otherwise, if we
|
||||
// got a storm of refresh requests due to, say, the copying or deletion of a large directory
|
||||
// tree, we could potentially spawn a separate simultaneous discovery operation for each
|
||||
// individual file change notification.
|
||||
// Our approach is to spawn a discovery operation immediately upon receiving the first refresh
|
||||
// request. If we receive any additional refresh requests before the first one is complete, we
|
||||
// record this fact by setting `this.retry = true`. When the original discovery operation
|
||||
// completes, we discard its results and spawn another one to account for that additional
|
||||
// changes that have happened since.
|
||||
// The means that for the common case of a single file being modified, we'll complete the
|
||||
// discovery and update as soon as possible. If multiple files are being modified, we'll
|
||||
// probably wind up doing discovery at least twice.
|
||||
// We could choose to delay the initial discovery request by a second or two to wait for any
|
||||
// other change notifications that might be coming along. However, this would create more
|
||||
// latency in the common case, in order to save a bit of latency in the uncommon case.
|
||||
|
||||
if (this.discoveryInProgress) {
|
||||
// There's already a discovery operation in progress. Tell it to restart when it's done.
|
||||
this.retry = true;
|
||||
}
|
||||
else {
|
||||
// No discovery in progress, so start one now.
|
||||
this.discoveryInProgress = true;
|
||||
this.launchDiscovery();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the asynchronous discovery operation by invoking the `discover` function. When the
|
||||
* discovery operation completes, the `update` function will be invoked with the results of the
|
||||
* discovery.
|
||||
*/
|
||||
private launchDiscovery(): void {
|
||||
const discoveryPromise = this.discover();
|
||||
discoveryPromise.then(results => {
|
||||
if (!this.retry) {
|
||||
// Update any listeners with the results of the discovery.
|
||||
this.discoveryInProgress = false;
|
||||
this.update(results);
|
||||
}
|
||||
});
|
||||
discoveryPromise.finally(() => {
|
||||
if (this.retry) {
|
||||
// Another refresh request came in while we were still running a previous discovery
|
||||
// operation. Since the discovery results we just computed are now stale, we'll launch
|
||||
// another discovery operation instead of updating.
|
||||
// Note that by doing this inside of `finally`, we will relaunch discovery even if the
|
||||
// initial discovery operation failed.
|
||||
this.retry = false;
|
||||
this.launchDiscovery();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden by the derived class to spawn the actual discovery operation, returning the results.
|
||||
*/
|
||||
protected abstract discover(): Promise<T>;
|
||||
|
||||
/**
|
||||
* Overridden by the derived class to atomically update the `Discovery` object with the results of
|
||||
* the discovery operation, and to notify any listeners that the discovery results may have
|
||||
* changed.
|
||||
* @param results The discovery results returned by the `discover` function.
|
||||
*/
|
||||
protected abstract update(results: T): void;
|
||||
}
|
|
@ -19,6 +19,9 @@ import * as qsClient from './queryserver-client';
|
|||
import { CodeQLCliServer } from './cli';
|
||||
import { assertNever } from './helpers-pure';
|
||||
import { displayQuickQuery } from './quick-query';
|
||||
import { TestHub, testExplorerExtensionId } from 'vscode-test-adapter-api';
|
||||
import { QLTestAdapterFactory } from './test-adapter';
|
||||
import { TestUIService } from './test-ui';
|
||||
|
||||
/**
|
||||
* extension.ts
|
||||
|
@ -305,6 +308,16 @@ async function activateWithInstalledDistribution(ctx: ExtensionContext, distribu
|
|||
outputChannel: ideServerLogger.outputChannel
|
||||
}, true);
|
||||
|
||||
const testExplorerExtension = extensions.getExtension<TestHub>(testExplorerExtensionId);
|
||||
if (testExplorerExtension) {
|
||||
const testHub = testExplorerExtension.exports;
|
||||
const testAdapterFactory = new QLTestAdapterFactory(testHub, cliServer);
|
||||
ctx.subscriptions.push(testAdapterFactory);
|
||||
|
||||
const testUIService = new TestUIService(testHub);
|
||||
ctx.subscriptions.push(testUIService);
|
||||
}
|
||||
|
||||
ctx.subscriptions.push(commands.registerCommand('codeQL.runQuery', async (uri: Uri | undefined) => await compileAndRunQuery(false, uri)));
|
||||
ctx.subscriptions.push(commands.registerCommand('codeQL.quickEval', async (uri: Uri | undefined) => await compileAndRunQuery(true, uri)));
|
||||
ctx.subscriptions.push(commands.registerCommand('codeQL.quickQuery', async () => displayQuickQuery(ctx, cliServer, databaseUI)));
|
||||
|
|
|
@ -18,7 +18,7 @@ export type ProgressReporter = Progress<{ message: string }>;
|
|||
|
||||
/** A logger that writes messages to an output channel in the Output tab. */
|
||||
export class OutputChannelLogger extends DisposableObject implements Logger {
|
||||
outputChannel: OutputChannel;
|
||||
public readonly outputChannel: OutputChannel;
|
||||
|
||||
constructor(title: string) {
|
||||
super();
|
||||
|
@ -47,3 +47,6 @@ export const queryServerLogger = new OutputChannelLogger('CodeQL Query Server');
|
|||
|
||||
/** The logger for messages from the language server. */
|
||||
export const ideServerLogger = new OutputChannelLogger('CodeQL Language Server');
|
||||
|
||||
/** The logger for messages from tests. */
|
||||
export const testLogger = new OutputChannelLogger('CodeQL Tests');
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
import { EventEmitter, Event, Uri, WorkspaceFolder, RelativePattern } from 'vscode';
|
||||
import { MultiFileSystemWatcher } from 'semmle-vscode-utils';
|
||||
import { CodeQLCliServer, QlpacksInfo } from './cli';
|
||||
import { Discovery } from './discovery';
|
||||
|
||||
export interface QLPack {
|
||||
name: string;
|
||||
uri: Uri;
|
||||
};
|
||||
|
||||
/**
|
||||
* Service to discover all available QL packs in a workspace folder.
|
||||
*/
|
||||
export class QLPackDiscovery extends Discovery<QlpacksInfo> {
|
||||
private readonly _onDidChangeQLPacks = this.push(new EventEmitter<void>());
|
||||
private readonly watcher = this.push(new MultiFileSystemWatcher());
|
||||
private _qlPacks: readonly QLPack[] = [];
|
||||
|
||||
constructor(private readonly workspaceFolder: WorkspaceFolder,
|
||||
private readonly cliServer: CodeQLCliServer) {
|
||||
|
||||
super();
|
||||
|
||||
// Watch for any changes to `qlpack.yml` files in this workspace folder.
|
||||
// TODO: The CLI server should tell us what paths to watch for.
|
||||
this.watcher.addWatch(new RelativePattern(this.workspaceFolder, '**/qlpack.yml'));
|
||||
this.watcher.addWatch(new RelativePattern(this.workspaceFolder, '**/.codeqlmanifest.json'));
|
||||
this.push(this.watcher.onDidChange(this.handleQLPackFileChanged, this));
|
||||
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
public get onDidChangeQLPacks(): Event<void> { return this._onDidChangeQLPacks.event; }
|
||||
|
||||
public get qlPacks(): readonly QLPack[] { return this._qlPacks; }
|
||||
|
||||
private handleQLPackFileChanged(_uri: Uri): void {
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
protected discover(): Promise<QlpacksInfo> {
|
||||
// Only look for QL packs in this workspace folder.
|
||||
return this.cliServer.resolveQlpacks([this.workspaceFolder.uri.fsPath], []);
|
||||
}
|
||||
|
||||
protected update(results: QlpacksInfo): void {
|
||||
const qlPacks: QLPack[] = [];
|
||||
for (const id in results) {
|
||||
qlPacks.push(...results[id].map(fsPath => {
|
||||
return {
|
||||
name: id,
|
||||
uri: Uri.file(fsPath)
|
||||
};
|
||||
}));
|
||||
}
|
||||
this._qlPacks = qlPacks;
|
||||
this._onDidChangeQLPacks.fire();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,222 @@
|
|||
import * as path from 'path';
|
||||
import { QLPackDiscovery } from './qlpack-discovery';
|
||||
import { Discovery } from './discovery';
|
||||
import { EventEmitter, Event, Uri, RelativePattern } from 'vscode';
|
||||
import { MultiFileSystemWatcher } from 'semmle-vscode-utils';
|
||||
import { CodeQLCliServer } from './cli';
|
||||
|
||||
/**
|
||||
* A node in the tree of tests. This will be either a `QLTestDirectory` or a `QLTestFile`.
|
||||
*/
|
||||
export abstract class QLTestNode {
|
||||
constructor(private _path: string, private _name: string) {
|
||||
}
|
||||
|
||||
public get path(): string {
|
||||
return this._path;
|
||||
}
|
||||
|
||||
public get name(): string {
|
||||
return this._name;
|
||||
}
|
||||
|
||||
public abstract get children(): readonly QLTestNode[];
|
||||
|
||||
public abstract finish(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* A directory containing one or more QL tests or other test directories.
|
||||
*/
|
||||
export class QLTestDirectory extends QLTestNode {
|
||||
private _children: QLTestNode[] = [];
|
||||
|
||||
constructor(_path: string, _name: string) {
|
||||
super(_path, _name);
|
||||
}
|
||||
|
||||
public get children(): readonly QLTestNode[] {
|
||||
return this._children;
|
||||
}
|
||||
|
||||
public addChild(child: QLTestNode): void {
|
||||
this._children.push(child);
|
||||
}
|
||||
|
||||
public createDirectory(relativePath: string): QLTestDirectory {
|
||||
const dirName = path.dirname(relativePath);
|
||||
if (dirName === '.') {
|
||||
return this.createChildDirectory(relativePath);
|
||||
}
|
||||
else {
|
||||
const parent = this.createDirectory(dirName);
|
||||
return parent.createDirectory(path.basename(relativePath));
|
||||
}
|
||||
}
|
||||
|
||||
public finish(): void {
|
||||
this._children.sort((a, b) => a.name.localeCompare(b.name));
|
||||
for (const child of this._children) {
|
||||
child.finish();
|
||||
}
|
||||
}
|
||||
|
||||
private createChildDirectory(name: string): QLTestDirectory {
|
||||
const existingChild = this._children.find((child) => child.name === name);
|
||||
if (existingChild !== undefined) {
|
||||
return <QLTestDirectory>existingChild;
|
||||
}
|
||||
else {
|
||||
const newChild = new QLTestDirectory(path.join(this.path, name), name);
|
||||
this.addChild(newChild);
|
||||
return newChild;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A single QL test. This will be either a `.ql` file or a `.qlref` file.
|
||||
*/
|
||||
export class QLTestFile extends QLTestNode {
|
||||
constructor(_path: string, _name: string) {
|
||||
super(_path, _name);
|
||||
}
|
||||
|
||||
public get children(): readonly QLTestNode[] {
|
||||
return [];
|
||||
}
|
||||
|
||||
public finish(): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The results of discovering QL tests.
|
||||
*/
|
||||
interface QLTestDiscoveryResults {
|
||||
/**
|
||||
* The root test directory for each QL pack that contains tests.
|
||||
*/
|
||||
testDirectories: QLTestDirectory[];
|
||||
/**
|
||||
* The list of file system paths to watch. If any of these paths changes, the discovery results
|
||||
* may be out of date.
|
||||
*/
|
||||
watchPaths: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Discovers all QL tests contained in the QL packs in a given workspace folder.
|
||||
*/
|
||||
export class QLTestDiscovery extends Discovery<QLTestDiscoveryResults> {
|
||||
private readonly _onDidChangeTests = this.push(new EventEmitter<void>());
|
||||
private readonly watcher: MultiFileSystemWatcher = this.push(new MultiFileSystemWatcher());
|
||||
private _testDirectories: QLTestDirectory[] = [];
|
||||
|
||||
constructor(private readonly qlPackDiscovery: QLPackDiscovery,
|
||||
private readonly cliServer: CodeQLCliServer) {
|
||||
|
||||
super();
|
||||
|
||||
this.push(this.qlPackDiscovery.onDidChangeQLPacks(this.handleDidChangeQLPacks, this));
|
||||
this.push(this.watcher.onDidChange(this.handleDidChange, this));
|
||||
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Event to be fired when the set of discovered tests may have changed.
|
||||
*/
|
||||
public get onDidChangeTests(): Event<void> { return this._onDidChangeTests.event; }
|
||||
|
||||
/**
|
||||
* The root test directory for each QL pack that contains tests.
|
||||
*/
|
||||
public get testDirectories(): QLTestDirectory[] { return this._testDirectories; }
|
||||
|
||||
private handleDidChangeQLPacks(): void {
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
private handleDidChange(uri: Uri): void {
|
||||
if (!QLTestDiscovery.ignoreTestPath(uri.fsPath)) {
|
||||
this.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
protected async discover(): Promise<QLTestDiscoveryResults> {
|
||||
const testDirectories: QLTestDirectory[] = [];
|
||||
const watchPaths: string[] = [];
|
||||
const qlPacks = this.qlPackDiscovery.qlPacks;
|
||||
for (const qlPack of qlPacks) {
|
||||
//HACK: Assume that only QL packs whose name ends with '-tests' contain tests.
|
||||
if (qlPack.name.endsWith('-tests')) {
|
||||
watchPaths.push(qlPack.uri.fsPath);
|
||||
const testPackage = await this.discoverTests(qlPack.uri.fsPath, qlPack.name);
|
||||
if (testPackage !== undefined) {
|
||||
testDirectories.push(testPackage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
testDirectories: testDirectories,
|
||||
watchPaths: watchPaths
|
||||
};
|
||||
}
|
||||
|
||||
protected update(results: QLTestDiscoveryResults): void {
|
||||
this._testDirectories = results.testDirectories;
|
||||
|
||||
// Watch for changes to any `.ql` or `.qlref` file in any of the QL packs that contain tests.
|
||||
this.watcher.clear();
|
||||
results.watchPaths.forEach(watchPath => {
|
||||
this.watcher.addWatch(new RelativePattern(watchPath, '**/*.{ql,qlref}'));
|
||||
});
|
||||
this._onDidChangeTests.fire();
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover all QL tests in the specified directory and its subdirectories.
|
||||
* @param fullPath The full path of the test directory.
|
||||
* @param name The display name to use for the returned `TestDirectory` object.
|
||||
* @returns A `QLTestDirectory` object describing the contents of the directory, or `undefined` if
|
||||
* no tests were found.
|
||||
*/
|
||||
private async discoverTests(fullPath: string, name: string): Promise<QLTestDirectory | undefined> {
|
||||
const resolvedTests = (await this.cliServer.resolveTests(fullPath))
|
||||
.filter((testPath) => !QLTestDiscovery.ignoreTestPath(testPath));
|
||||
|
||||
if (resolvedTests.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
else {
|
||||
const rootDirectory = new QLTestDirectory(fullPath, name);
|
||||
for (const testPath of resolvedTests) {
|
||||
const relativePath = path.normalize(path.relative(fullPath, testPath));
|
||||
const dirName = path.dirname(relativePath);
|
||||
const parentDirectory = rootDirectory.createDirectory(dirName);
|
||||
parentDirectory.addChild(new QLTestFile(testPath, path.basename(testPath)));
|
||||
}
|
||||
|
||||
rootDirectory.finish();
|
||||
|
||||
return rootDirectory;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the specified QL test should be ignored based on its filename.
|
||||
* @param testPath Path to the test file.
|
||||
*/
|
||||
private static ignoreTestPath(testPath: string): boolean {
|
||||
switch (path.extname(testPath).toLowerCase()) {
|
||||
case '.ql':
|
||||
case '.qlref':
|
||||
return path.basename(testPath).startsWith('__');
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
import * as cp from 'child_process';
|
||||
import { DisposableObject } from 'semmle-vscode-utils';
|
||||
// Import from the specific module within `semmle-vscode-utils`, rather than via `index.ts`, because
|
||||
// we avoid taking an accidental runtime dependency on `vscode` this way.
|
||||
import { DisposableObject } from 'semmle-vscode-utils/out/disposable-object';
|
||||
import { Disposable } from 'vscode';
|
||||
import { CancellationToken, createMessageConnection, MessageConnection, RequestType } from 'vscode-jsonrpc';
|
||||
import * as cli from './cli';
|
||||
|
|
|
@ -0,0 +1,234 @@
|
|||
import * as path from 'path';
|
||||
import * as vscode from 'vscode';
|
||||
import {
|
||||
TestAdapter,
|
||||
TestLoadStartedEvent,
|
||||
TestLoadFinishedEvent,
|
||||
TestRunStartedEvent,
|
||||
TestRunFinishedEvent,
|
||||
TestSuiteEvent,
|
||||
TestEvent,
|
||||
TestSuiteInfo,
|
||||
TestInfo,
|
||||
TestHub
|
||||
} from 'vscode-test-adapter-api';
|
||||
import { TestAdapterRegistrar } from 'vscode-test-adapter-util';
|
||||
import { QLTestFile, QLTestNode, QLTestDirectory, QLTestDiscovery } from './qltest-discovery';
|
||||
import { Event, EventEmitter, CancellationTokenSource, CancellationToken } from 'vscode';
|
||||
import { DisposableObject } from 'semmle-vscode-utils';
|
||||
import { QLPackDiscovery } from './qlpack-discovery';
|
||||
import { CodeQLCliServer } from './cli';
|
||||
import { getOnDiskWorkspaceFolders } from './helpers';
|
||||
import { testLogger } from './logging';
|
||||
|
||||
/**
|
||||
* Get the full path of the `.expected` file for the specified QL test.
|
||||
* @param testPath The full path to the test file.
|
||||
*/
|
||||
export function getExpectedFile(testPath: string): string {
|
||||
return getTestOutputFile(testPath, '.expected');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full path of the `.actual` file for the specified QL test.
|
||||
* @param testPath The full path to the test file.
|
||||
*/
|
||||
export function getActualFile(testPath: string): string {
|
||||
return getTestOutputFile(testPath, '.actual');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the directory containing the specified QL test.
|
||||
* @param testPath The full path to the test file.
|
||||
*/
|
||||
export function getTestDirectory(testPath: string): string {
|
||||
return path.dirname(testPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the the full path to a particular output file of the specified QL test.
|
||||
* @param testPath The full path to the QL test.
|
||||
* @param extension The file extension of the output file.
|
||||
*/
|
||||
function getTestOutputFile(testPath: string, extension: string): string {
|
||||
return changeExtension(testPath, extension);
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory service that creates `QLTestAdapter` objects for workspace folders on demand.
|
||||
*/
|
||||
export class QLTestAdapterFactory extends DisposableObject {
|
||||
constructor(testHub: TestHub, cliServer: CodeQLCliServer) {
|
||||
super();
|
||||
|
||||
// this will register a QLTestAdapter for each WorkspaceFolder
|
||||
this.push(new TestAdapterRegistrar(
|
||||
testHub,
|
||||
workspaceFolder => new QLTestAdapter(workspaceFolder, cliServer)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the file extension of the specified path.
|
||||
* @param p The original file path.
|
||||
* @param ext The new extension, including the `.`.
|
||||
*/
|
||||
function changeExtension(p: string, ext: string): string {
|
||||
return p.substr(0, p.length - path.extname(p).length) + ext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test adapter for QL tests.
|
||||
*/
|
||||
export class QLTestAdapter extends DisposableObject implements TestAdapter {
|
||||
private readonly qlPackDiscovery: QLPackDiscovery;
|
||||
private readonly qlTestDiscovery: QLTestDiscovery;
|
||||
private readonly _tests = this.push(
|
||||
new EventEmitter<TestLoadStartedEvent | TestLoadFinishedEvent>());
|
||||
private readonly _testStates = this.push(
|
||||
new EventEmitter<TestRunStartedEvent | TestRunFinishedEvent | TestSuiteEvent |
|
||||
TestEvent>());
|
||||
private readonly _autorun = this.push(new EventEmitter<void>());
|
||||
private runningTask?: vscode.CancellationTokenSource = undefined;
|
||||
|
||||
constructor(
|
||||
public readonly workspaceFolder: vscode.WorkspaceFolder,
|
||||
private readonly cliServer: CodeQLCliServer
|
||||
) {
|
||||
super();
|
||||
|
||||
this.qlPackDiscovery = this.push(new QLPackDiscovery(workspaceFolder, cliServer));
|
||||
this.qlTestDiscovery = this.push(new QLTestDiscovery(this.qlPackDiscovery, cliServer));
|
||||
|
||||
this.push(this.qlTestDiscovery.onDidChangeTests(this.discoverTests, this));
|
||||
}
|
||||
|
||||
public get tests(): Event<TestLoadStartedEvent | TestLoadFinishedEvent> {
|
||||
return this._tests.event;
|
||||
}
|
||||
|
||||
public get testStates(): Event<TestRunStartedEvent | TestRunFinishedEvent | TestSuiteEvent |
|
||||
TestEvent> {
|
||||
|
||||
return this._testStates.event;
|
||||
}
|
||||
|
||||
public get autorun(): Event<void> | undefined {
|
||||
return this._autorun.event;
|
||||
}
|
||||
|
||||
private static createTestOrSuiteInfos(testNodes: readonly QLTestNode[]):
|
||||
(TestSuiteInfo | TestInfo)[] {
|
||||
|
||||
return testNodes.map((childNode) => {
|
||||
return QLTestAdapter.createTestOrSuiteInfo(childNode);
|
||||
});
|
||||
}
|
||||
|
||||
private static createTestOrSuiteInfo(testNode: QLTestNode): TestSuiteInfo | TestInfo {
|
||||
if (testNode instanceof QLTestFile) {
|
||||
return QLTestAdapter.createTestInfo(testNode);
|
||||
}
|
||||
else if (testNode instanceof QLTestDirectory) {
|
||||
return QLTestAdapter.createTestSuiteInfo(testNode, testNode.name);
|
||||
}
|
||||
else {
|
||||
throw new Error('Unexpected test type.');
|
||||
}
|
||||
}
|
||||
|
||||
private static createTestInfo(testFile: QLTestFile): TestInfo {
|
||||
return {
|
||||
type: 'test',
|
||||
id: testFile.path,
|
||||
label: testFile.name,
|
||||
tooltip: testFile.path,
|
||||
file: testFile.path
|
||||
};
|
||||
}
|
||||
|
||||
private static createTestSuiteInfo(testDirectory: QLTestDirectory, label: string):
|
||||
TestSuiteInfo {
|
||||
|
||||
return {
|
||||
type: 'suite',
|
||||
id: testDirectory.path,
|
||||
label: label,
|
||||
children: QLTestAdapter.createTestOrSuiteInfos(testDirectory.children),
|
||||
tooltip: testDirectory.path
|
||||
};
|
||||
}
|
||||
|
||||
public async load(): Promise<void> {
|
||||
this.discoverTests();
|
||||
}
|
||||
|
||||
private discoverTests(): void {
|
||||
this._tests.fire(<TestLoadStartedEvent>{ type: 'started' });
|
||||
|
||||
const testDirectories = this.qlTestDiscovery.testDirectories;
|
||||
const testSuite: TestSuiteInfo = {
|
||||
type: 'suite',
|
||||
label: 'CodeQL',
|
||||
id: '.',
|
||||
children: testDirectories.map(
|
||||
testDirectory => QLTestAdapter.createTestSuiteInfo(testDirectory, testDirectory.name))
|
||||
};
|
||||
|
||||
this._tests.fire(<TestLoadFinishedEvent>{ type: 'finished', suite: testSuite });
|
||||
}
|
||||
|
||||
public async run(tests: string[]): Promise<void> {
|
||||
if (this.runningTask !== undefined) {
|
||||
throw new Error('Tests already running.');
|
||||
}
|
||||
|
||||
testLogger.outputChannel.clear();
|
||||
testLogger.outputChannel.show(true);
|
||||
|
||||
this.runningTask = this.track(new CancellationTokenSource());
|
||||
|
||||
this._testStates.fire(<TestRunStartedEvent>{ type: 'started', tests: tests });
|
||||
|
||||
const testAdapter = this;
|
||||
|
||||
try {
|
||||
await this.runTests(tests, this.runningTask.token);
|
||||
}
|
||||
catch (e) {
|
||||
}
|
||||
testAdapter._testStates.fire(<TestRunFinishedEvent>{ type: 'finished' });
|
||||
testAdapter.clearTask();
|
||||
}
|
||||
|
||||
private clearTask(): void {
|
||||
if (this.runningTask !== undefined) {
|
||||
const runningTask = this.runningTask;
|
||||
this.runningTask = undefined;
|
||||
this.disposeAndStopTracking(runningTask);
|
||||
}
|
||||
}
|
||||
|
||||
public cancel(): void {
|
||||
if (this.runningTask !== undefined) {
|
||||
testLogger.log('Cancelling test run...');
|
||||
this.runningTask.cancel();
|
||||
this.clearTask();
|
||||
}
|
||||
}
|
||||
|
||||
private async runTests(tests: string[], cancellationToken: CancellationToken): Promise<void> {
|
||||
const workspacePaths = await getOnDiskWorkspaceFolders();
|
||||
for await (const event of await this.cliServer.runTests(tests, workspacePaths, {
|
||||
cancellationToken: cancellationToken,
|
||||
logger: testLogger
|
||||
})) {
|
||||
this._testStates.fire({
|
||||
type: 'test',
|
||||
state: event.pass ? 'passed' : 'failed',
|
||||
test: event.test
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
import { TestSuiteInfo, TestInfo } from 'vscode-test-adapter-api';
|
||||
|
||||
/**
|
||||
* Tree view node for a test, suite, or collection. This object is passed as the argument to the
|
||||
* command handler of a context menu item for a tree view item.
|
||||
*/
|
||||
export interface TestTreeNode {
|
||||
readonly info: TestSuiteInfo | TestInfo;
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
import * as fs from 'fs-extra';
|
||||
import * as path from 'path';
|
||||
import { Uri, TextDocumentShowOptions, commands, window } from 'vscode';
|
||||
import { TestTreeNode } from './test-tree-node';
|
||||
import { DisposableObject, UIService } from 'semmle-vscode-utils';
|
||||
import { TestHub, TestController, TestAdapter, TestRunStartedEvent, TestRunFinishedEvent, TestEvent, TestSuiteEvent } from 'vscode-test-adapter-api';
|
||||
import { QLTestAdapter, getExpectedFile, getActualFile } from './test-adapter';
|
||||
|
||||
/**
|
||||
* Test event listener. Currently unused, but left in to keep the plumbing hooked up for future use.
|
||||
*/
|
||||
class QLTestListener extends DisposableObject {
|
||||
constructor(adapter: TestAdapter) {
|
||||
super();
|
||||
|
||||
this.push(adapter.testStates(this.onTestStatesEvent, this));
|
||||
}
|
||||
|
||||
private onTestStatesEvent(_e: TestRunStartedEvent | TestRunFinishedEvent | TestSuiteEvent | TestEvent): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Service that implements all UI and commands for QL tests.
|
||||
*/
|
||||
export class TestUIService extends UIService implements TestController {
|
||||
private readonly listeners: Map<TestAdapter, QLTestListener> = new Map();
|
||||
|
||||
constructor(private readonly testHub: TestHub) {
|
||||
super();
|
||||
|
||||
this.registerCommand('codeQLTests.showOutputDifferences', this.showOutputDifferences);
|
||||
this.registerCommand('codeQLTests.acceptOutput', this.acceptOutput);
|
||||
|
||||
testHub.registerTestController(this);
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
this.testHub.unregisterTestController(this);
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
public registerTestAdapter(adapter: TestAdapter): void {
|
||||
this.listeners.set(adapter, new QLTestListener(adapter));
|
||||
}
|
||||
|
||||
public unregisterTestAdapter(adapter: TestAdapter): void {
|
||||
if (adapter instanceof QLTestAdapter) {
|
||||
this.listeners.delete(adapter);
|
||||
}
|
||||
}
|
||||
|
||||
private async acceptOutput(node: TestTreeNode): Promise<void> {
|
||||
const testId = node.info.id;
|
||||
const stat = await fs.lstat(testId);
|
||||
if (stat.isFile()) {
|
||||
const expectedPath = getExpectedFile(testId);
|
||||
const actualPath = getActualFile(testId);
|
||||
await fs.copy(actualPath, expectedPath, { overwrite: true });
|
||||
}
|
||||
}
|
||||
|
||||
private async showOutputDifferences(node: TestTreeNode): Promise<void> {
|
||||
const testId = node.info.id;
|
||||
const stat = await fs.lstat(testId);
|
||||
if (stat.isFile()) {
|
||||
const expectedPath = getExpectedFile(testId);
|
||||
const expectedUri = Uri.file(expectedPath);
|
||||
const actualPath = getActualFile(testId);
|
||||
const options: TextDocumentShowOptions = {
|
||||
preserveFocus: true,
|
||||
preview: true
|
||||
};
|
||||
if (await fs.pathExists(actualPath)) {
|
||||
const actualUri = Uri.file(actualPath);
|
||||
await commands.executeCommand<void>('vscode.diff', expectedUri, actualUri,
|
||||
`Expected vs. Actual for ${path.basename(testId)}`, options);
|
||||
}
|
||||
else {
|
||||
await window.showTextDocument(expectedUri, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1 +1,4 @@
|
|||
export * from './disposable-object';
|
||||
export * from './multi-file-system-watcher';
|
||||
export * from './ui-service';
|
||||
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
import { DisposableObject } from './disposable-object';
|
||||
import { EventEmitter, Event, Uri, GlobPattern, workspace } from 'vscode';
|
||||
|
||||
/**
|
||||
* A collection of `FileSystemWatcher` objects. Disposing this object disposes all of the individual
|
||||
* `FileSystemWatcher` objects and their event registrations.
|
||||
*/
|
||||
class WatcherCollection extends DisposableObject {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a `FileSystemWatcher` and add it to the collection.
|
||||
* @param pattern The pattern to watch.
|
||||
* @param listener The event listener to be invoked when a watched file is created, changed, or
|
||||
* deleted.
|
||||
* @param thisArgs The `this` argument for the event listener.
|
||||
*/
|
||||
public addWatcher(pattern: GlobPattern, listener: (e: Uri) => any, thisArgs: any): void {
|
||||
const watcher = workspace.createFileSystemWatcher(pattern);
|
||||
this.push(watcher.onDidCreate(listener, thisArgs));
|
||||
this.push(watcher.onDidChange(listener, thisArgs));
|
||||
this.push(watcher.onDidDelete(listener, thisArgs));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A class to watch multiple patterns in the file system at the same time, reporting all
|
||||
* notifications via a single event.
|
||||
*/
|
||||
export class MultiFileSystemWatcher extends DisposableObject {
|
||||
private readonly _onDidChange = this.push(new EventEmitter<Uri>());
|
||||
private watchers = this.track(new WatcherCollection());
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Event to be fired when any watched file is created, changed, or deleted.
|
||||
*/
|
||||
public get onDidChange(): Event<Uri> { return this._onDidChange.event; }
|
||||
|
||||
/**
|
||||
* Adds a new pattern to watch.
|
||||
* @param pattern The pattern to watch.
|
||||
*/
|
||||
public addWatch(pattern: GlobPattern): void {
|
||||
this.watchers.addWatcher(pattern, this.handleDidChange, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all existing watchers.
|
||||
*/
|
||||
public clear(): void {
|
||||
this.disposeAndStopTracking(this.watchers);
|
||||
this.watchers = this.track(new WatcherCollection());
|
||||
}
|
||||
|
||||
private handleDidChange(uri: Uri): void {
|
||||
this._onDidChange.fire(uri);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import { commands, TreeDataProvider, window } from 'vscode';
|
||||
import { DisposableObject } from './disposable-object';
|
||||
|
||||
/**
|
||||
* A VS Code service that interacts with the UI, including handling commands.
|
||||
*/
|
||||
export class UIService extends DisposableObject {
|
||||
protected constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a command handler with Visual Studio Code.
|
||||
* @param command The ID of the command to register.
|
||||
* @param callback Callback function to implement the command.
|
||||
* @remarks The command handler is automatically unregistered when the service is disposed.
|
||||
*/
|
||||
protected registerCommand(command: string, callback: (...args: any[]) => any): void {
|
||||
this.push(commands.registerCommand(command, callback, this));
|
||||
}
|
||||
|
||||
protected registerTreeDataProvider<T>(viewId: string, treeDataProvider: TreeDataProvider<T>):
|
||||
void {
|
||||
|
||||
this.push(window.registerTreeDataProvider<T>(viewId, treeDataProvider));
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@
|
|||
"rushVersion": "5.11.2",
|
||||
"pnpmVersion": "2.15.1",
|
||||
"pnpmOptions": {
|
||||
"strictPeerDependencies": true,
|
||||
"strictPeerDependencies": true
|
||||
},
|
||||
"nodeSupportedVersionRange": ">=10.13.0 <13.0.0",
|
||||
"ensureConsistentVersions": true,
|
||||
|
|
Загрузка…
Ссылка в новой задаче