-
Notifications
You must be signed in to change notification settings - Fork 403
Add unit tests for uploadPayload
#3185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
4489a63
a57997f
621809b
aeb12f6
a841c54
ff2fc66
610c7c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,9 +1,14 @@ | ||||||
import * as fs from "fs"; | ||||||
import * as path from "path"; | ||||||
|
||||||
import test from "ava"; | ||||||
import * as github from "@actions/github"; | ||||||
import { HTTPError } from "@actions/tool-cache"; | ||||||
import test, { ExecutionContext } from "ava"; | ||||||
import * as sinon from "sinon"; | ||||||
|
||||||
import * as analyses from "./analyses"; | ||||||
import { AnalysisKind, CodeQuality, CodeScanning } from "./analyses"; | ||||||
import * as api from "./api-client"; | ||||||
import { getRunnerLogger, Logger } from "./logging"; | ||||||
import { setupTests } from "./testing-utils"; | ||||||
import * as uploadLib from "./upload-lib"; | ||||||
|
@@ -867,3 +872,123 @@ function createMockSarif(id?: string, tool?: string) { | |||||
], | ||||||
}; | ||||||
} | ||||||
|
||||||
const uploadPayloadMacro = test.macro({ | ||||||
exec: async ( | ||||||
t: ExecutionContext<unknown>, | ||||||
options: { | ||||||
analysis: analyses.AnalysisConfig; | ||||||
body: ( | ||||||
t: ExecutionContext<unknown>, | ||||||
upload: () => Promise<string>, | ||||||
requestStub: sinon.SinonStub, | ||||||
mockData: { | ||||||
payload: { sarif: string; commit_sha: string }; | ||||||
owner: string; | ||||||
repo: string; | ||||||
response: { | ||||||
status: number; | ||||||
data: { id: string }; | ||||||
headers: any; | ||||||
url: string; | ||||||
}; | ||||||
}, | ||||||
) => void | Promise<void>; | ||||||
|
||||||
}, | ||||||
) => { | ||||||
const mockData = { | ||||||
payload: { sarif: "base64data", commit_sha: "abc123" }, | ||||||
owner: "test-owner", | ||||||
repo: "test-repo", | ||||||
response: { | ||||||
status: 200, | ||||||
data: { id: "uploaded-sarif-id" }, | ||||||
headers: {}, | ||||||
url: options.analysis.target, | ||||||
}, | ||||||
}; | ||||||
|
||||||
const client = github.getOctokit("123"); | ||||||
sinon.stub(api, "getApiClient").value(() => client); | ||||||
const requestStub = sinon.stub(client, "request"); | ||||||
|
||||||
const upload = async () => | ||||||
uploadLib.uploadPayload( | ||||||
mockData.payload, | ||||||
{ | ||||||
owner: mockData.owner, | ||||||
repo: mockData.repo, | ||||||
}, | ||||||
getRunnerLogger(true), | ||||||
options.analysis, | ||||||
); | ||||||
|
||||||
await options.body(t, upload, requestStub, mockData); | ||||||
}, | ||||||
title: (providedTitle = "", options: { analysis: analyses.AnalysisConfig }) => | ||||||
`uploadPayload - ${options.analysis.name} - ${providedTitle}`, | ||||||
}); | ||||||
|
||||||
for (const analysis of [CodeScanning, CodeQuality]) { | ||||||
test("uploads successfully", uploadPayloadMacro, { | ||||||
|
||||||
analysis, | ||||||
body: async (t, upload, requestStub, mockData) => { | ||||||
requestStub | ||||||
.withArgs(analysis.target, { | ||||||
owner: mockData.owner, | ||||||
repo: mockData.repo, | ||||||
data: mockData.payload, | ||||||
}) | ||||||
.onFirstCall() | ||||||
.returns(Promise.resolve(mockData.response)); | ||||||
const result = await upload(); | ||||||
t.is(result, mockData.response.data.id); | ||||||
t.true(requestStub.calledOnce); | ||||||
}, | ||||||
}); | ||||||
|
||||||
for (const envVar of [ | ||||||
"CODEQL_ACTION_SKIP_SARIF_UPLOAD", | ||||||
"CODEQL_ACTION_TEST_MODE", | ||||||
]) { | ||||||
test(`skips upload when ${envVar} is set`, uploadPayloadMacro, { | ||||||
analysis, | ||||||
body: async (t, upload, requestStub, mockData) => | ||||||
withTmpDir(async (tmpDir) => { | ||||||
process.env.RUNNER_TEMP = tmpDir; | ||||||
process.env[envVar] = "true"; | ||||||
const result = await upload(); | ||||||
t.is(result, "dummy-sarif-id"); | ||||||
t.false(requestStub.called); | ||||||
|
||||||
const payloadFile = path.join( | ||||||
tmpDir, | ||||||
`payload-${analysis.kind}.json`, | ||||||
); | ||||||
t.true(fs.existsSync(payloadFile)); | ||||||
|
||||||
const savedPayload = JSON.parse(fs.readFileSync(payloadFile, "utf8")); | ||||||
t.deepEqual(savedPayload, mockData.payload); | ||||||
}), | ||||||
}); | ||||||
} | ||||||
|
||||||
test("handles error", uploadPayloadMacro, { | ||||||
|
test("handles error", uploadPayloadMacro, { | |
test("wraps request errors using `wrapApiConfigurationError`", uploadPayloadMacro, { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you have any particular reason for having this
options
object rather than havinganalysis
andbody
be parameters ofexec
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
initially I had more flags that profited from being named explicitly, but I agree that at this stage it's not really needed