Skip to content

Commit b26519c

Browse files
committed
Add isOtherAnalysisSarif with tests
1 parent 320fdb3 commit b26519c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/analyses.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from "ava";
22

33
import {
44
AnalysisKind,
5+
isOtherAnalysisSarif,
56
parseAnalysisKinds,
67
supportedAnalysisKinds,
78
} from "./analyses";
@@ -34,3 +35,23 @@ test("Parsing analysis kinds requires at least one analysis kind", async (t) =>
3435
instanceOf: ConfigurationError,
3536
});
3637
});
38+
39+
test("isOtherAnalysisSarif", async (t) => {
40+
function expectTrue(analysisKind: AnalysisKind, filepath: string) {
41+
t.assert(
42+
isOtherAnalysisSarif(analysisKind, filepath),
43+
`Expected ${filepath} to be for another analysis kind, but it matched ${analysisKind}.`,
44+
);
45+
}
46+
function expectFalse(analysisKind: AnalysisKind, filepath: string) {
47+
t.assert(
48+
!isOtherAnalysisSarif(analysisKind, filepath),
49+
`Expected ${filepath} to be for ${analysisKind}, but it matched some other analysis kind.`,
50+
);
51+
}
52+
expectTrue(AnalysisKind.CodeQuality, "test.sarif");
53+
expectFalse(AnalysisKind.CodeQuality, "test.quality.sarif");
54+
expectTrue(AnalysisKind.CodeScanning, "test.quality.sarif");
55+
expectFalse(AnalysisKind.CodeScanning, "test.sarif");
56+
expectFalse(AnalysisKind.CodeScanning, "test.json");
57+
});

src/analyses.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,23 @@ export const CodeQuality: AnalysisConfig = {
8686
sarifPredicate: (name) => name.endsWith(CodeQuality.sarifExtension),
8787
sentinelPrefix: "CODEQL_UPLOAD_QUALITY_SARIF_",
8888
};
89+
90+
// An array of all analysis configurations we know of.
91+
export const Analyses = [CodeScanning, CodeQuality];
92+
93+
/**
94+
* Determines based on the given `filepath`, whether the file is a SARIF file for
95+
* an analysis other than the one given by `current`.
96+
*
97+
* @param current The current analysis kind.
98+
* @param filepath The path of the file to check.
99+
* @returns True if `filepath` is a SARIF file for an analysis other than `current`; False otherwise.
100+
*/
101+
export function isOtherAnalysisSarif(
102+
current: AnalysisKind,
103+
filepath: string,
104+
): boolean {
105+
return Analyses.some(
106+
(config) => config.kind !== current && config.sarifPredicate(filepath),
107+
);
108+
}

0 commit comments

Comments
 (0)