Skip to content

Commit a8440d0

Browse files
authored
Merge pull request #3185 from github/redsun82/skip-sarif-upload-tests
Add unit tests for `uploadPayload`
2 parents 6fd4ceb + 610c7c6 commit a8440d0

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

lib/upload-lib.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/upload-lib.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as fs from "fs";
22
import * as path from "path";
33

4+
import * as github from "@actions/github";
5+
import { HTTPError } from "@actions/tool-cache";
46
import test from "ava";
7+
import * as sinon from "sinon";
58

9+
import * as analyses from "./analyses";
610
import { AnalysisKind, CodeQuality, CodeScanning } from "./analyses";
11+
import * as api from "./api-client";
712
import { getRunnerLogger, Logger } from "./logging";
813
import { setupTests } from "./testing-utils";
914
import * as uploadLib from "./upload-lib";
@@ -867,3 +872,91 @@ function createMockSarif(id?: string, tool?: string) {
867872
],
868873
};
869874
}
875+
876+
function uploadPayloadFixtures(analysis: analyses.AnalysisConfig) {
877+
const mockData = {
878+
payload: { sarif: "base64data", commit_sha: "abc123" },
879+
owner: "test-owner",
880+
repo: "test-repo",
881+
response: {
882+
status: 200,
883+
data: { id: "uploaded-sarif-id" },
884+
headers: {},
885+
url: analysis.target,
886+
},
887+
};
888+
const client = github.getOctokit("123");
889+
sinon.stub(api, "getApiClient").value(() => client);
890+
const requestStub = sinon.stub(client, "request");
891+
892+
const upload = async () =>
893+
uploadLib.uploadPayload(
894+
mockData.payload,
895+
{
896+
owner: mockData.owner,
897+
repo: mockData.repo,
898+
},
899+
getRunnerLogger(true),
900+
analysis,
901+
);
902+
903+
return {
904+
upload,
905+
requestStub,
906+
mockData,
907+
};
908+
}
909+
910+
for (const analysis of [CodeScanning, CodeQuality]) {
911+
test(`uploadPayload on ${analysis.name} uploads successfully`, async (t) => {
912+
const { upload, requestStub, mockData } = uploadPayloadFixtures(analysis);
913+
requestStub
914+
.withArgs(analysis.target, {
915+
owner: mockData.owner,
916+
repo: mockData.repo,
917+
data: mockData.payload,
918+
})
919+
.onFirstCall()
920+
.returns(Promise.resolve(mockData.response));
921+
const result = await upload();
922+
t.is(result, mockData.response.data.id);
923+
t.true(requestStub.calledOnce);
924+
});
925+
926+
for (const envVar of [
927+
"CODEQL_ACTION_SKIP_SARIF_UPLOAD",
928+
"CODEQL_ACTION_TEST_MODE",
929+
]) {
930+
test(`uploadPayload on ${analysis.name} skips upload when ${envVar} is set`, async (t) => {
931+
const { upload, requestStub, mockData } = uploadPayloadFixtures(analysis);
932+
await withTmpDir(async (tmpDir) => {
933+
process.env.RUNNER_TEMP = tmpDir;
934+
process.env[envVar] = "true";
935+
const result = await upload();
936+
t.is(result, "dummy-sarif-id");
937+
t.false(requestStub.called);
938+
939+
const payloadFile = path.join(tmpDir, `payload-${analysis.kind}.json`);
940+
t.true(fs.existsSync(payloadFile));
941+
942+
const savedPayload = JSON.parse(fs.readFileSync(payloadFile, "utf8"));
943+
t.deepEqual(savedPayload, mockData.payload);
944+
});
945+
});
946+
}
947+
948+
test(`uploadPayload on ${analysis.name} wraps request errors using wrapApiConfigurationError`, async (t) => {
949+
const { upload, requestStub } = uploadPayloadFixtures(analysis);
950+
const wrapApiConfigurationErrorStub = sinon.stub(
951+
api,
952+
"wrapApiConfigurationError",
953+
);
954+
const originalError = new HTTPError(404);
955+
const wrappedError = new Error("Wrapped error message");
956+
requestStub.rejects(originalError);
957+
wrapApiConfigurationErrorStub.withArgs(originalError).returns(wrappedError);
958+
await t.throwsAsync(upload, {
959+
is: wrappedError,
960+
});
961+
});
962+
}

src/upload-lib.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,12 @@ function getAutomationID(
346346
return api.computeAutomationID(analysis_key, environment);
347347
}
348348

349-
// Upload the given payload.
350-
// If the request fails then this will retry a small number of times.
351-
async function uploadPayload(
349+
/**
350+
* Upload the given payload.
351+
* If the request fails then this will retry a small number of times.
352+
* This is exported for testing purposes only.
353+
*/
354+
export async function uploadPayload(
352355
payload: any,
353356
repositoryNwo: RepositoryNwo,
354357
logger: Logger,

0 commit comments

Comments
 (0)