Add isOtherAnalysisSarif with tests

This commit is contained in:
Michael B. Gale
2025-09-26 15:51:43 +01:00
parent 320fdb3773
commit b26519c6c0
2 changed files with 41 additions and 0 deletions
+21
View File
@@ -2,6 +2,7 @@ import test from "ava";
import {
AnalysisKind,
isOtherAnalysisSarif,
parseAnalysisKinds,
supportedAnalysisKinds,
} from "./analyses";
@@ -34,3 +35,23 @@ test("Parsing analysis kinds requires at least one analysis kind", async (t) =>
instanceOf: ConfigurationError,
});
});
test("isOtherAnalysisSarif", async (t) => {
function expectTrue(analysisKind: AnalysisKind, filepath: string) {
t.assert(
isOtherAnalysisSarif(analysisKind, filepath),
`Expected ${filepath} to be for another analysis kind, but it matched ${analysisKind}.`,
);
}
function expectFalse(analysisKind: AnalysisKind, filepath: string) {
t.assert(
!isOtherAnalysisSarif(analysisKind, filepath),
`Expected ${filepath} to be for ${analysisKind}, but it matched some other analysis kind.`,
);
}
expectTrue(AnalysisKind.CodeQuality, "test.sarif");
expectFalse(AnalysisKind.CodeQuality, "test.quality.sarif");
expectTrue(AnalysisKind.CodeScanning, "test.quality.sarif");
expectFalse(AnalysisKind.CodeScanning, "test.sarif");
expectFalse(AnalysisKind.CodeScanning, "test.json");
});
+20
View File
@@ -86,3 +86,23 @@ export const CodeQuality: AnalysisConfig = {
sarifPredicate: (name) => name.endsWith(CodeQuality.sarifExtension),
sentinelPrefix: "CODEQL_UPLOAD_QUALITY_SARIF_",
};
// An array of all analysis configurations we know of.
export const Analyses = [CodeScanning, CodeQuality];
/**
* Determines based on the given `filepath`, whether the file is a SARIF file for
* an analysis other than the one given by `current`.
*
* @param current The current analysis kind.
* @param filepath The path of the file to check.
* @returns True if `filepath` is a SARIF file for an analysis other than `current`; False otherwise.
*/
export function isOtherAnalysisSarif(
current: AnalysisKind,
filepath: string,
): boolean {
return Analyses.some(
(config) => config.kind !== current && config.sarifPredicate(filepath),
);
}