Skip to content

Commit 0bb1501

Browse files
Move sarif parser and tests, build completing
1 parent d53abd8 commit 0bb1501

8 files changed

Lines changed: 57 additions & 72 deletions

File tree

extensions/ql-vscode/src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { assertNever } from './pure/helpers-pure';
1616
import { QueryMetadata, SortDirection } from './pure/interface-types';
1717
import { Logger, ProgressReporter } from './logging';
1818
import { CompilationMessage } from './pure/messages';
19-
import { parseSarif } from './pure/sarif-utils';
19+
import { sarifParser } from './sarif-parser';
2020
import { dbSchemeToLanguage } from './helpers';
2121

2222
/**
@@ -682,7 +682,7 @@ export class CodeQLCliServer implements Disposable {
682682

683683
async interpretBqrs(metadata: QueryMetadata, resultsPath: string, interpretedResultsPath: string, sourceInfo?: SourceInfo): Promise<sarif.Log> {
684684
await this.runInterpretCommand(SARIF_FORMAT, metadata, resultsPath, interpretedResultsPath, sourceInfo);
685-
return await parseSarif(interpretedResultsPath);
685+
return await sarifParser(interpretedResultsPath);
686686
}
687687

688688
async generateResultsCsv(metadata: QueryMetadata, resultsPath: string, csvPath: string, sourceInfo?: SourceInfo): Promise<void> {

extensions/ql-vscode/src/pure/sarif-utils.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import * as Sarif from 'sarif';
2-
import * as fs from 'fs-extra';
3-
import { parser } from 'stream-json';
4-
import { pick } from 'stream-json/filters/Pick';
5-
import Assembler = require('stream-json/Assembler');
6-
import { chain } from 'stream-chain';
7-
82
import { ResolvableLocationValue } from './bqrs-cli-types';
93

10-
const DUMMY_TOOL : Sarif.Tool = {driver: {name: ''}};
11-
124
export interface SarifLink {
135
dest: number;
146
text: string;
@@ -164,46 +156,6 @@ export function parseSarifLocation(
164156
}
165157
}
166158

167-
export async function parseSarif(interpretedResultsPath: string) : Promise<Sarif.Log> {
168-
try {
169-
// Parse the SARIF file into token streams, filtering out only the results array.
170-
const p = parser();
171-
const pipeline = chain([
172-
fs.createReadStream(interpretedResultsPath),
173-
p,
174-
pick({filter: 'runs.0.results'})
175-
]);
176-
177-
// Creates JavaScript objects from the token stream
178-
const asm = Assembler.connectTo(pipeline);
179-
180-
// Returns a constructed Log object with the results or an empty array if no results were found.
181-
// If the parser fails for any reason, it will reject the promise.
182-
return await new Promise((resolve, reject) => {
183-
pipeline.on('error', (error) => {
184-
reject(error);
185-
});
186-
187-
asm.on('done', (asm) => {
188-
189-
const log : Sarif.Log = {
190-
version: '2.1.0',
191-
runs: [
192-
{
193-
tool: DUMMY_TOOL,
194-
results: asm.current ?? []
195-
}
196-
]
197-
};
198-
199-
resolve(log);
200-
});
201-
});
202-
} catch (err) {
203-
throw new Error(`Parsing output of interpretation failed: ${err.stderr || err}`);
204-
}
205-
}
206-
207159
export function isNoLocation(loc: ParsedSarifLocation): loc is NoLocation {
208160
return 'hint' in loc;
209161
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import * as Sarif from 'sarif';
2+
import * as fs from 'fs-extra';
3+
import { parser } from 'stream-json';
4+
import { pick } from 'stream-json/filters/Pick';
5+
import Assembler = require('stream-json/Assembler');
6+
import { chain } from 'stream-chain';
7+
8+
const DUMMY_TOOL : Sarif.Tool = {driver: {name: ''}};
9+
10+
export async function sarifParser(interpretedResultsPath: string) : Promise<Sarif.Log> {
11+
try {
12+
// Parse the SARIF file into token streams, filtering out only the results array.
13+
const p = parser();
14+
const pipeline = chain([
15+
fs.createReadStream(interpretedResultsPath),
16+
p,
17+
pick({filter: 'runs.0.results'})
18+
]);
19+
20+
// Creates JavaScript objects from the token stream
21+
const asm = Assembler.connectTo(pipeline);
22+
23+
// Returns a constructed Log object with the results or an empty array if no results were found.
24+
// If the parser fails for any reason, it will reject the promise.
25+
return await new Promise((resolve, reject) => {
26+
pipeline.on('error', (error) => {
27+
reject(error);
28+
});
29+
30+
asm.on('done', (asm) => {
31+
32+
const log : Sarif.Log = {
33+
version: '2.1.0',
34+
runs: [
35+
{
36+
tool: DUMMY_TOOL,
37+
results: asm.current ?? []
38+
}
39+
]
40+
};
41+
42+
resolve(log);
43+
});
44+
});
45+
} catch (err) {
46+
throw new Error(`Parsing output of interpretation failed: ${err.stderr || err}`);
47+
}
48+
}

extensions/ql-vscode/test/sarif/emptyResultsSarif.sarif renamed to extensions/ql-vscode/src/vscode-tests/no-workspace/sarif/emptyResultsSarif.sarif

File renamed without changes.

extensions/ql-vscode/test/sarif/invalidSarif.sarif renamed to extensions/ql-vscode/src/vscode-tests/no-workspace/sarif/invalidSarif.sarif

File renamed without changes.

extensions/ql-vscode/test/sarif/validSarif.sarif renamed to extensions/ql-vscode/src/vscode-tests/no-workspace/sarif/validSarif.sarif

File renamed without changes.

extensions/ql-vscode/src/vscode-tests/no-workspace/cli.test.ts renamed to extensions/ql-vscode/src/vscode-tests/no-workspace/sarifParser.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
import * as path from 'path';
12
import * as chai from 'chai';
23
import * as chaiAsPromised from 'chai-as-promised';
34

4-
import { CodeQLCliServer } from '../../cli';
5+
import { sarifParser } from '../../sarif-parser';
56

67
chai.use(chaiAsPromised);
78
const expect = chai.expect;
89

9-
describe.only('cliServerTests', function() {
10-
10+
describe.only('sarif parser', function() {
11+
const sarifDir = path.join(path.dirname(__dirname), 'sarif');
1112
it('should parse a valid SARIF file', async () => {
12-
const result = await CodeQLCliServer.parseSarif(__dirname + '/data/sarif/validSarif.sarif');
13+
const result = await sarifParser(path.join(sarifDir, 'validSarif.sarif'));
1314
expect(result.version).to.exist;
1415
expect(result.runs).to.exist;
1516
expect(result.runs[0].tool).to.exist;
1617
expect(result.runs[0].tool.driver).to.exist;
18+
expect(result.runs.length).to.be.at.least(1);
1719
});
1820

1921
it('should return an empty array if there are no results', async () => {
20-
const result = await CodeQLCliServer.parseSarif(__dirname + '/data/sarif/emptyResultsSarif.sarif');
22+
const result = await sarifParser(path.join(sarifDir, 'emptyResultsSarif.sarif'));
2123
expect(result.runs[0].results).to.be.empty;
2224
});
2325
});

extensions/ql-vscode/test/pure-tests/sarif-utils.test.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import 'mocha';
22
import { expect } from 'chai';
33
import * as Sarif from 'sarif';
4-
import * as path from 'path';
54

65
import {
76
getPathRelativeToSourceLocationPrefix,
87
parseSarifLocation,
98
parseSarifPlainTextMessage,
109
unescapeSarifText,
11-
parseSarif
1210
} from '../../src/pure/sarif-utils';
1311

1412

1513
describe('parsing sarif', () => {
16-
const sarifDir = path.join(path.dirname(__dirname), 'sarif');
1714

1815
it('should be able to parse a simple message from the spec', async function() {
1916
const message = 'Tainted data was used. The data came from [here](3).';
@@ -64,20 +61,6 @@ describe('parsing sarif', () => {
6461
.to.eq('file:/a/b/c/?x=test');
6562
});
6663

67-
it('should parse a valid SARIF file', async () => {
68-
const result = await parseSarif(path.join(sarifDir, 'validSarif.sarif'));
69-
expect(result.version).to.exist;
70-
expect(result.runs).to.exist;
71-
expect(result.runs[0].tool).to.exist;
72-
expect(result.runs[0].tool.driver).to.exist;
73-
expect(result.runs.length).to.be.at.least(1);
74-
});
75-
76-
it('should return an empty array if there are no results', async () => {
77-
const result = await parseSarif(path.join(sarifDir, 'emptyResultsSarif.sarif'));
78-
expect(result.runs[0].results).to.be.empty;
79-
});
80-
8164
describe('parseSarifLocation', () => {
8265
it('should parse a sarif location with "no location"', () => {
8366
expect(parseSarifLocation({}, '')).to.deep.equal({

0 commit comments

Comments
 (0)