Skip to content

Commit

Permalink
Add regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Mercer committed Dec 6, 2022
1 parent 1653364 commit 9085295
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 43 deletions.
68 changes: 49 additions & 19 deletions lib/init-action-post-helper.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/init-action-post-helper.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 63 additions & 23 deletions src/init-action-post-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import test from "ava";
import test, { ExecutionContext } from "ava";
import * as sinon from "sinon";

import * as actionsUtil from "./actions-util";
Expand Down Expand Up @@ -86,23 +86,7 @@ test("post: init action with debug mode on", async (t) => {
});

test("uploads failed SARIF run for typical workflow", async (t) => {
const config = {
codeQLCmd: "codeql",
debugMode: true,
languages: [],
packs: [],
} as unknown as configUtils.Config;
const messages = [];
process.env["GITHUB_JOB"] = "analyze";
process.env["GITHUB_WORKSPACE"] =
"/home/runner/work/codeql-action/codeql-action";
sinon.stub(actionsUtil, "getRequiredInput").withArgs("matrix").returns("{}");

const codeqlObject = await codeql.getCodeQLForTesting();
sinon.stub(codeql, "getCodeQL").resolves(codeqlObject);
const diagnosticsExportStub = sinon.stub(codeqlObject, "diagnosticsExport");

sinon.stub(workflow, "getWorkflow").resolves({
const actionsWorkflow: workflow.Workflow = {
name: "CodeQL",
on: {
push: {
Expand Down Expand Up @@ -138,7 +122,61 @@ test("uploads failed SARIF run for typical workflow", async (t) => {
],
},
},
});
};
await testFailedSarifUpload(t, actionsWorkflow, { category: "my-category" });
});

test("uploading failed SARIF run fails when workflow does not reference github/codeql-action", async (t) => {
const actionsWorkflow: workflow.Workflow = {
name: "CodeQL",
on: {
push: {
branches: ["main"],
},
pull_request: {
branches: ["main"],
},
},
jobs: {
analyze: {
name: "CodeQL Analysis",
"runs-on": "ubuntu-latest",
steps: [
{
name: "Checkout repository",
uses: "actions/checkout@v3",
},
],
},
},
};
await t.throwsAsync(
async () => await testFailedSarifUpload(t, actionsWorkflow)
);
});

async function testFailedSarifUpload(
t: ExecutionContext<unknown>,
actionsWorkflow: workflow.Workflow,
{ category }: { category?: string } = {}
): Promise<void> {
const config = {
codeQLCmd: "codeql",
debugMode: true,
languages: [],
packs: [],
} as unknown as configUtils.Config;
const messages = [];
process.env["GITHUB_JOB"] = "analyze";
process.env["GITHUB_WORKSPACE"] =
"/home/runner/work/codeql-action/codeql-action";
sinon.stub(actionsUtil, "getRequiredInput").withArgs("matrix").returns("{}");

const codeqlObject = await codeql.getCodeQLForTesting();
sinon.stub(codeql, "getCodeQL").resolves(codeqlObject);
const diagnosticsExportStub = sinon.stub(codeqlObject, "diagnosticsExport");

sinon.stub(workflow, "getWorkflow").resolves(actionsWorkflow);

const uploadFromActions = sinon.stub(uploadLib, "uploadFromActions");
uploadFromActions.resolves({ sarifID: "42" } as uploadLib.UploadResult);
Expand All @@ -152,19 +190,21 @@ test("uploads failed SARIF run for typical workflow", async (t) => {
);
t.deepEqual(messages, []);
t.true(
diagnosticsExportStub.calledOnceWith(sinon.match.string, "my-category")
diagnosticsExportStub.calledOnceWith(sinon.match.string, category),
`Actual args were: ${diagnosticsExportStub.args}`
);
t.true(
uploadFromActions.calledOnceWith(
sinon.match.string,
sinon.match.string,
"my-category",
category,
sinon.match.any
)
),
`Actual args were: ${uploadFromActions.args}`
);
t.true(
waitForProcessing.calledOnceWith(sinon.match.any, "42", sinon.match.any, {
isUnsuccessfulExecution: true,
})
);
});
}

0 comments on commit 9085295

Please sign in to comment.