mirror of
https://github.com/github/codeql-action.git
synced 2026-04-29 02:18:47 +00:00
upload-lib: Unit test filterAlertsByDiffRange
This commit is contained in:
+178
@@ -0,0 +1,178 @@
|
||||
{
|
||||
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
|
||||
"version": "2.1.0",
|
||||
"runs": [{
|
||||
"tool": {
|
||||
"driver": {
|
||||
"name": "LGTM.com",
|
||||
"organization": "Semmle",
|
||||
"version": "1.24.0-SNAPSHOT",
|
||||
"rules": [{
|
||||
"id": "js/unused-local-variable",
|
||||
"name": "js/unused-local-variable",
|
||||
"shortDescription": {
|
||||
"text": "Unused variable, import, function or class"
|
||||
},
|
||||
"fullDescription": {
|
||||
"text": "Unused variables, imports, functions or classes may be a symptom of a bug and should be examined carefully."
|
||||
},
|
||||
"defaultConfiguration": {
|
||||
"level": "note"
|
||||
},
|
||||
"properties": {
|
||||
"tags": ["maintainability"],
|
||||
"kind": "problem",
|
||||
"precision": "very-high",
|
||||
"name": "Unused variable, import, function or class",
|
||||
"description": "Unused variables, imports, functions or classes may be a symptom of a bug\n and should be examined carefully.",
|
||||
"id": "js/unused-local-variable",
|
||||
"problem.severity": "recommendation"
|
||||
}
|
||||
}]
|
||||
}
|
||||
},
|
||||
"results": [{
|
||||
"ruleId": "js/unused-local-variable",
|
||||
"ruleIndex": 0,
|
||||
"message": {
|
||||
"text": "Unused variable foo."
|
||||
},
|
||||
"locations": [{
|
||||
"physicalLocation": {
|
||||
"artifactLocation": {
|
||||
"uri": "main.js",
|
||||
"uriBaseId": "%SRCROOT%",
|
||||
"index": 0
|
||||
},
|
||||
"region": {
|
||||
"startLine": 2,
|
||||
"startColumn": 7,
|
||||
"endColumn": 10
|
||||
}
|
||||
}
|
||||
}],
|
||||
"partialFingerprints": {
|
||||
"primaryLocationLineHash": "39fa2ee980eb94b0:1",
|
||||
"primaryLocationStartColumnFingerprint": "4"
|
||||
}
|
||||
}],
|
||||
"columnKind": "utf16CodeUnits",
|
||||
"properties": {
|
||||
"semmle.formatSpecifier": "2.1.0",
|
||||
"semmle.sourceLanguage": "java"
|
||||
}
|
||||
},
|
||||
{
|
||||
"tool" : {
|
||||
"driver" : {
|
||||
"name" : "CodeQL command-line toolchain",
|
||||
"organization" : "GitHub",
|
||||
"semanticVersion" : "2.0.0",
|
||||
"rules" : [ {
|
||||
"id" : "js/unused-local-variable",
|
||||
"name" : "js/unused-local-variable",
|
||||
"shortDescription" : {
|
||||
"text" : "Unused variable, import, function or class"
|
||||
},
|
||||
"fullDescription" : {
|
||||
"text" : "Unused variables, imports, functions or classes may be a symptom of a bug and should be examined carefully."
|
||||
},
|
||||
"defaultConfiguration" : {
|
||||
"level": "note"
|
||||
},
|
||||
"properties" : {
|
||||
"tags" : [ "maintainability" ],
|
||||
"kind" : "problem",
|
||||
"precision" : "very-high",
|
||||
"name" : "Unused variable, import, function or class",
|
||||
"description" : "Unused variables, imports, functions or classes may be a symptom of a bug\n and should be examined carefully.",
|
||||
"id" : "js/unused-local-variable",
|
||||
"problem.severity" : "recommendation"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "js/inconsistent-use-of-new",
|
||||
"name": "js/inconsistent-use-of-new",
|
||||
"shortDescription": {
|
||||
"text": "Inconsistent use of 'new'"
|
||||
},
|
||||
"fullDescription": {
|
||||
"text": "If a function is intended to be a constructor, it should always be invoked with 'new'. Otherwise, it should always be invoked as a normal function, that is, without 'new'."
|
||||
},
|
||||
"defaultConfiguration": {
|
||||
"level": "note"
|
||||
},
|
||||
"properties": {
|
||||
"tags": [
|
||||
"reliability",
|
||||
"correctness",
|
||||
"language-features"
|
||||
],
|
||||
"kind": "problem",
|
||||
"precision": "very-high",
|
||||
"problem.severity": "warning"
|
||||
}
|
||||
} ]
|
||||
}
|
||||
},
|
||||
"artifacts" : [ {
|
||||
"location" : {
|
||||
"uri" : "main.js",
|
||||
"uriBaseId" : "%SRCROOT%",
|
||||
"index" : 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"location": {
|
||||
"uri": "src/promiseUtils.js",
|
||||
"uriBaseId": "%SRCROOT%",
|
||||
"index": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"location": {
|
||||
"uri": "src/LiveQueryClient.js",
|
||||
"uriBaseId": "%SRCROOT%",
|
||||
"index": 2
|
||||
}
|
||||
},
|
||||
{
|
||||
"location": {
|
||||
"uri": "src/ParseObject.js",
|
||||
"uriBaseId": "%SRCROOT%",
|
||||
"index": 3
|
||||
}
|
||||
} ],
|
||||
"results" : [ {
|
||||
"ruleId" : "js/unused-local-variable",
|
||||
"ruleIndex" : 0,
|
||||
"message" : {
|
||||
"text" : "Unused variable foo."
|
||||
},
|
||||
"locations" : [ {
|
||||
"physicalLocation" : {
|
||||
"artifactLocation" : {
|
||||
"uri" : "main.js",
|
||||
"uriBaseId" : "%SRCROOT%",
|
||||
"index" : 0
|
||||
},
|
||||
"region" : {
|
||||
"startLine" : 2,
|
||||
"startColumn" : 7,
|
||||
"endColumn" : 10
|
||||
}
|
||||
}
|
||||
} ],
|
||||
"partialFingerprints" : {
|
||||
"primaryLocationLineHash" : "39fa2ee980eb94b0:1",
|
||||
"primaryLocationStartColumnFingerprint" : "4"
|
||||
}
|
||||
}],
|
||||
"newlineSequences" : [ "\r\n", "\n", "
", "
" ],
|
||||
"columnKind" : "utf16CodeUnits",
|
||||
"properties" : {
|
||||
"semmle.formatSpecifier" : "sarif-latest"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
+39
-1
@@ -6,13 +6,20 @@ import { HTTPError } from "@actions/tool-cache";
|
||||
import test from "ava";
|
||||
import * as sinon from "sinon";
|
||||
|
||||
import * as actionsUtil from "./actions-util";
|
||||
import * as analyses from "./analyses";
|
||||
import { AnalysisKind, CodeQuality, CodeScanning } from "./analyses";
|
||||
import * as api from "./api-client";
|
||||
import * as diffUtils from "./diff-informed-analysis-utils";
|
||||
import { getRunnerLogger, Logger } from "./logging";
|
||||
import { setupTests } from "./testing-utils";
|
||||
import * as uploadLib from "./upload-lib";
|
||||
import { GitHubVariant, initializeEnvironment, withTmpDir } from "./util";
|
||||
import {
|
||||
GitHubVariant,
|
||||
initializeEnvironment,
|
||||
SarifFile,
|
||||
withTmpDir,
|
||||
} from "./util";
|
||||
|
||||
setupTests(test);
|
||||
|
||||
@@ -960,3 +967,34 @@ for (const analysis of [CodeScanning, CodeQuality]) {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function runFilterAlertsByDiffRange(
|
||||
input: SarifFile,
|
||||
diffRanges: diffUtils.DiffThunkRange[],
|
||||
): SarifFile {
|
||||
sinon
|
||||
.stub(actionsUtil, "getRequiredInput")
|
||||
.withArgs("checkout_path")
|
||||
.returns("/checkout/path");
|
||||
sinon.stub(diffUtils, "readDiffRangesJsonFile").returns(diffRanges);
|
||||
return uploadLib.filterAlertsByDiffRange(getRunnerLogger(true), input);
|
||||
}
|
||||
|
||||
test("filterAlertsByDiffRange filters out alerts outside diff-range", (t) => {
|
||||
const input = uploadLib.readSarifFile(
|
||||
`${__dirname}/../src/testdata/valid-sarif.sarif`,
|
||||
);
|
||||
const actualOutput = runFilterAlertsByDiffRange(input, [
|
||||
{
|
||||
path: "/checkout/path/main.js",
|
||||
startLine: 1,
|
||||
endLine: 3,
|
||||
},
|
||||
]);
|
||||
|
||||
const expectedOutput = uploadLib.readSarifFile(
|
||||
`${__dirname}/../src/testdata/valid-sarif-diff-filtered.sarif`,
|
||||
);
|
||||
|
||||
t.deepEqual(actualOutput, expectedOutput);
|
||||
});
|
||||
|
||||
+4
-1
@@ -1134,7 +1134,10 @@ export class InvalidSarifUploadError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
function filterAlertsByDiffRange(logger: Logger, sarif: SarifFile): SarifFile {
|
||||
export function filterAlertsByDiffRange(
|
||||
logger: Logger,
|
||||
sarif: SarifFile,
|
||||
): SarifFile {
|
||||
const diffRanges = readDiffRangesJsonFile(logger);
|
||||
if (!diffRanges?.length) {
|
||||
return sarif;
|
||||
|
||||
Reference in New Issue
Block a user