Skip to content

Commit 9cd2118

Browse files
committed
Add test for new generateFailedSarif behaviour
1 parent 61b1d55 commit 9cd2118

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎src/init-action-post-helper.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
14
import * as core from "@actions/core";
25
import test, { ExecutionContext } from "ava";
36
import * as sinon from "sinon";
@@ -8,6 +11,7 @@ import * as apiClient from "./api-client";
811
import * as codeql from "./codeql";
912
import * as configUtils from "./config-utils";
1013
import * as debugArtifacts from "./debug-artifacts";
14+
import { getTempDiagnosticPath } from "./diagnostics";
1115
import { EnvVar } from "./environment";
1216
import { Feature } from "./feature-flags";
1317
import * as initActionPostHelper from "./init-action-post-helper";
@@ -35,6 +39,47 @@ const NUM_BYTES_PER_GIB = 1024 * 1024 * 1024;
3539

3640
setupTests(test);
3741

42+
test.serial("generateFailedSarif picks up unwritten diagnostics", async (t) =>
43+
util.withTmpDir(async (tmpDir) => {
44+
const logger = new RecordingLogger();
45+
46+
const tempDiagDir = getTempDiagnosticPath(tmpDir);
47+
fs.mkdirSync(tempDiagDir);
48+
49+
const codeqlObject = await codeql.getCodeQLForTesting();
50+
const diagnosticsExport = sinon
51+
.stub(codeqlObject, "diagnosticsExport")
52+
.resolves();
53+
54+
sinon.stub(codeql, "getCodeQL").resolves(codeqlObject);
55+
56+
const sarifFile = path.join(tmpDir, "failed.sarif");
57+
const result = await initActionPostHelper.generateFailedSarif(
58+
logger,
59+
createFeatures([]),
60+
createTestConfig({
61+
tempDir: tmpDir,
62+
// Explicitly undefined to ensure we get the "no database" path.
63+
dbLocation: undefined,
64+
}),
65+
"category",
66+
"",
67+
sarifFile,
68+
);
69+
70+
t.truthy(result);
71+
t.is(diagnosticsExport.callCount, 1);
72+
t.true(
73+
diagnosticsExport.calledOnceWith(
74+
sarifFile,
75+
"category",
76+
sinon.match.any,
77+
tempDiagDir,
78+
),
79+
);
80+
}),
81+
);
82+
3883
test.serial("init-post action with debug mode off", async (t) => {
3984
return await util.withTmpDir(async (tmpDir) => {
4085
setupActionsVars(tmpDir, tmpDir);

‎src/init-action-post-helper.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,16 @@ async function prepareFailedSarif(
169169
}
170170
}
171171

172-
async function generateFailedSarif(
172+
/**
173+
* Generates a SARIF file for a failed run, i.e. an analysis where no SARIF was produced as a result of the analysis.
174+
*
175+
* Not intended to be called directly, other than by `prepareFailedSarif`.
176+
*
177+
* @param category The analysis category to include in the SARIF.
178+
* @param checkoutPath The path where the repository was checked out at.
179+
* @param sarifFile The SARIF file to produce.
180+
*/
181+
export async function generateFailedSarif(
173182
logger: Logger,
174183
features: FeatureEnablement,
175184
config: ActionConfig<"codeQLCmd" | "tempDir">,

0 commit comments

Comments
 (0)