-
Notifications
You must be signed in to change notification settings - Fork 451
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>; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we have this pattern with a continuation (here:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fundamentally I wanted to have the setup that the macro does in common for all the tests. I had a version of this where all the test behaviour was being driven by flags in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, maybe that could be covered better by a setup function, rather than a macro?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe, considering the test macro is not doing any checks after calling
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I like that better 🙂 |
||||||
| }, | ||||||
| ) => { | ||||||
| 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, { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: I was going to comment that it would probably make sense to include the |
||||||
| 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, { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: maybe a better title would be
Suggested change
|
||||||
| analysis, | ||||||
| body: async (t, upload, requestStub) => { | ||||||
| const wrapApiConfigurationErrorStub = sinon.stub( | ||||||
| api, | ||||||
| "wrapApiConfigurationError", | ||||||
| ); | ||||||
| const originalError = new HTTPError(404); | ||||||
| const wrappedError = new Error("Wrapped error message"); | ||||||
| requestStub.rejects(originalError); | ||||||
| wrapApiConfigurationErrorStub | ||||||
| .withArgs(originalError) | ||||||
| .returns(wrappedError); | ||||||
| await t.throwsAsync(upload, { | ||||||
| is: wrappedError, | ||||||
| }); | ||||||
| }, | ||||||
| }); | ||||||
| } | ||||||
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
optionsobject rather than havinganalysisandbodybe 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