Allow testing workflow parsing functionality from PR checks

This commit is contained in:
Henry Mercer 2022-12-06 18:31:52 +00:00
parent 697ed97fa5
commit 384a214d60
9 changed files with 53 additions and 9 deletions

View file

@ -38,6 +38,7 @@ const workflow = __importStar(require("./workflow"));
(0, testing_utils_1.setupTests)(ava_1.default); (0, testing_utils_1.setupTests)(ava_1.default);
(0, ava_1.default)("post: init action with debug mode off", async (t) => { (0, ava_1.default)("post: init action with debug mode off", async (t) => {
return await util.withTmpDir(async (tmpDir) => { return await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["RUNNER_TEMP"] = tmpDir; process.env["RUNNER_TEMP"] = tmpDir;
const gitHubVersion = { const gitHubVersion = {
type: util.GitHubVariant.DOTCOM, type: util.GitHubVariant.DOTCOM,
@ -59,6 +60,7 @@ const workflow = __importStar(require("./workflow"));
}); });
(0, ava_1.default)("post: init action with debug mode on", async (t) => { (0, ava_1.default)("post: init action with debug mode on", async (t) => {
return await util.withTmpDir(async (tmpDir) => { return await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["RUNNER_TEMP"] = tmpDir; process.env["RUNNER_TEMP"] = tmpDir;
const gitHubVersion = { const gitHubVersion = {
type: util.GitHubVariant.DOTCOM, type: util.GitHubVariant.DOTCOM,
@ -139,6 +141,7 @@ async function testFailedSarifUpload(t, actionsWorkflow, { category } = {}) {
}; };
const messages = []; const messages = [];
process.env["GITHUB_JOB"] = "analyze"; process.env["GITHUB_JOB"] = "analyze";
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["GITHUB_WORKSPACE"] = process.env["GITHUB_WORKSPACE"] =
"/home/runner/work/codeql-action/codeql-action"; "/home/runner/work/codeql-action/codeql-action";
sinon.stub(actionsUtil, "getRequiredInput").withArgs("matrix").returns("{}"); sinon.stub(actionsUtil, "getRequiredInput").withArgs("matrix").returns("{}");

File diff suppressed because one or more lines are too long

19
lib/workflow.js generated
View file

@ -293,6 +293,19 @@ function getInputOrThrow(workflow, jobName, actionName, inputName, matrixVars) {
} }
return input; return input;
} }
/**
* Get the expected name of the analyze Action.
*
* This allows us to test workflow parsing functionality as a CodeQL Action PR check.
*/
function getAnalyzeActionName() {
if ((0, util_1.getRequiredEnvParam)("GITHUB_REPOSITORY") === "github/codeql-action") {
return "./analyze";
}
else {
return "github/codeql-action/analyze";
}
}
/** /**
* Makes a best effort attempt to retrieve the category input for the particular job, * Makes a best effort attempt to retrieve the category input for the particular job,
* given a set of matrix variables. * given a set of matrix variables.
@ -303,7 +316,7 @@ function getInputOrThrow(workflow, jobName, actionName, inputName, matrixVars) {
* @throws an error if the category input could not be determined * @throws an error if the category input could not be determined
*/ */
function getCategoryInputOrThrow(workflow, jobName, matrixVars) { function getCategoryInputOrThrow(workflow, jobName, matrixVars) {
return getInputOrThrow(workflow, jobName, "github/codeql-action/analyze", "category", matrixVars); return getInputOrThrow(workflow, jobName, getAnalyzeActionName(), "category", matrixVars);
} }
exports.getCategoryInputOrThrow = getCategoryInputOrThrow; exports.getCategoryInputOrThrow = getCategoryInputOrThrow;
/** /**
@ -316,7 +329,7 @@ exports.getCategoryInputOrThrow = getCategoryInputOrThrow;
* @throws an error if the upload input could not be determined * @throws an error if the upload input could not be determined
*/ */
function getUploadInputOrThrow(workflow, jobName, matrixVars) { function getUploadInputOrThrow(workflow, jobName, matrixVars) {
return (getInputOrThrow(workflow, jobName, "github/codeql-action/analyze", "upload", matrixVars) || "true" // if unspecified, upload defaults to true return (getInputOrThrow(workflow, jobName, getAnalyzeActionName(), "upload", matrixVars) || "true" // if unspecified, upload defaults to true
); );
} }
exports.getUploadInputOrThrow = getUploadInputOrThrow; exports.getUploadInputOrThrow = getUploadInputOrThrow;
@ -330,7 +343,7 @@ exports.getUploadInputOrThrow = getUploadInputOrThrow;
* @throws an error if the checkout_path input could not be determined * @throws an error if the checkout_path input could not be determined
*/ */
function getCheckoutPathInputOrThrow(workflow, jobName, matrixVars) { function getCheckoutPathInputOrThrow(workflow, jobName, matrixVars) {
return (getInputOrThrow(workflow, jobName, "github/codeql-action/analyze", "checkout_path", matrixVars) || (0, util_1.getRequiredEnvParam)("GITHUB_WORKSPACE") // if unspecified, checkout_path defaults to ${{ github.workspace }} return (getInputOrThrow(workflow, jobName, getAnalyzeActionName(), "checkout_path", matrixVars) || (0, util_1.getRequiredEnvParam)("GITHUB_WORKSPACE") // if unspecified, checkout_path defaults to ${{ github.workspace }}
); );
} }
exports.getCheckoutPathInputOrThrow = getCheckoutPathInputOrThrow; exports.getCheckoutPathInputOrThrow = getCheckoutPathInputOrThrow;

File diff suppressed because one or more lines are too long

6
lib/workflow.test.js generated
View file

@ -356,6 +356,7 @@ function errorCodes(actual, expected) {
`)), [])); `)), []));
}); });
(0, ava_1.default)("getCategoryInputOrThrow returns category for simple workflow with category", (t) => { (0, ava_1.default)("getCategoryInputOrThrow returns category for simple workflow with category", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(` t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(`
jobs: jobs:
analysis: analysis:
@ -369,6 +370,7 @@ function errorCodes(actual, expected) {
`), "analysis", {}), "some-category"); `), "analysis", {}), "some-category");
}); });
(0, ava_1.default)("getCategoryInputOrThrow returns undefined for simple workflow without category", (t) => { (0, ava_1.default)("getCategoryInputOrThrow returns undefined for simple workflow without category", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(` t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(`
jobs: jobs:
analysis: analysis:
@ -380,6 +382,7 @@ function errorCodes(actual, expected) {
`), "analysis", {}), undefined); `), "analysis", {}), undefined);
}); });
(0, ava_1.default)("getCategoryInputOrThrow returns category for workflow with multiple jobs", (t) => { (0, ava_1.default)("getCategoryInputOrThrow returns category for workflow with multiple jobs", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(` t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(`
jobs: jobs:
foo: foo:
@ -403,6 +406,7 @@ function errorCodes(actual, expected) {
`), "bar", {}), "bar-category"); `), "bar", {}), "bar-category");
}); });
(0, ava_1.default)("getCategoryInputOrThrow finds category for workflow with language matrix", (t) => { (0, ava_1.default)("getCategoryInputOrThrow finds category for workflow with language matrix", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(` t.is((0, workflow_1.getCategoryInputOrThrow)(yaml.load(`
jobs: jobs:
analysis: analysis:
@ -421,6 +425,7 @@ function errorCodes(actual, expected) {
`), "analysis", { language: "javascript" }), "/language:javascript"); `), "analysis", { language: "javascript" }), "/language:javascript");
}); });
(0, ava_1.default)("getCategoryInputOrThrow throws error for workflow with dynamic category", (t) => { (0, ava_1.default)("getCategoryInputOrThrow throws error for workflow with dynamic category", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.throws(() => (0, workflow_1.getCategoryInputOrThrow)(yaml.load(` t.throws(() => (0, workflow_1.getCategoryInputOrThrow)(yaml.load(`
jobs: jobs:
analysis: analysis:
@ -436,6 +441,7 @@ function errorCodes(actual, expected) {
}); });
}); });
(0, ava_1.default)("getCategoryInputOrThrow throws error for workflow with multiple calls to analyze", (t) => { (0, ava_1.default)("getCategoryInputOrThrow throws error for workflow with multiple calls to analyze", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.throws(() => (0, workflow_1.getCategoryInputOrThrow)(yaml.load(` t.throws(() => (0, workflow_1.getCategoryInputOrThrow)(yaml.load(`
jobs: jobs:
analysis: analysis:

File diff suppressed because one or more lines are too long

View file

@ -21,6 +21,7 @@ setupTests(test);
test("post: init action with debug mode off", async (t) => { test("post: init action with debug mode off", async (t) => {
return await util.withTmpDir(async (tmpDir) => { return await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["RUNNER_TEMP"] = tmpDir; process.env["RUNNER_TEMP"] = tmpDir;
const gitHubVersion: util.GitHubVersion = { const gitHubVersion: util.GitHubVersion = {
@ -54,6 +55,7 @@ test("post: init action with debug mode off", async (t) => {
test("post: init action with debug mode on", async (t) => { test("post: init action with debug mode on", async (t) => {
return await util.withTmpDir(async (tmpDir) => { return await util.withTmpDir(async (tmpDir) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["RUNNER_TEMP"] = tmpDir; process.env["RUNNER_TEMP"] = tmpDir;
const gitHubVersion: util.GitHubVersion = { const gitHubVersion: util.GitHubVersion = {
@ -157,6 +159,7 @@ async function testFailedSarifUpload(
} as unknown as configUtils.Config; } as unknown as configUtils.Config;
const messages = []; const messages = [];
process.env["GITHUB_JOB"] = "analyze"; process.env["GITHUB_JOB"] = "analyze";
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
process.env["GITHUB_WORKSPACE"] = process.env["GITHUB_WORKSPACE"] =
"/home/runner/work/codeql-action/codeql-action"; "/home/runner/work/codeql-action/codeql-action";
sinon.stub(actionsUtil, "getRequiredInput").withArgs("matrix").returns("{}"); sinon.stub(actionsUtil, "getRequiredInput").withArgs("matrix").returns("{}");

View file

@ -525,6 +525,7 @@ test("getWorkflowErrors() should not report an error if PRs are totally unconfig
}); });
test("getCategoryInputOrThrow returns category for simple workflow with category", (t) => { test("getCategoryInputOrThrow returns category for simple workflow with category", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is( t.is(
getCategoryInputOrThrow( getCategoryInputOrThrow(
yaml.load(` yaml.load(`
@ -546,6 +547,7 @@ test("getCategoryInputOrThrow returns category for simple workflow with category
}); });
test("getCategoryInputOrThrow returns undefined for simple workflow without category", (t) => { test("getCategoryInputOrThrow returns undefined for simple workflow without category", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is( t.is(
getCategoryInputOrThrow( getCategoryInputOrThrow(
yaml.load(` yaml.load(`
@ -565,6 +567,7 @@ test("getCategoryInputOrThrow returns undefined for simple workflow without cate
}); });
test("getCategoryInputOrThrow returns category for workflow with multiple jobs", (t) => { test("getCategoryInputOrThrow returns category for workflow with multiple jobs", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is( t.is(
getCategoryInputOrThrow( getCategoryInputOrThrow(
yaml.load(` yaml.load(`
@ -596,6 +599,7 @@ test("getCategoryInputOrThrow returns category for workflow with multiple jobs",
}); });
test("getCategoryInputOrThrow finds category for workflow with language matrix", (t) => { test("getCategoryInputOrThrow finds category for workflow with language matrix", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.is( t.is(
getCategoryInputOrThrow( getCategoryInputOrThrow(
yaml.load(` yaml.load(`
@ -622,6 +626,7 @@ test("getCategoryInputOrThrow finds category for workflow with language matrix",
}); });
test("getCategoryInputOrThrow throws error for workflow with dynamic category", (t) => { test("getCategoryInputOrThrow throws error for workflow with dynamic category", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.throws( t.throws(
() => () =>
getCategoryInputOrThrow( getCategoryInputOrThrow(
@ -647,6 +652,7 @@ test("getCategoryInputOrThrow throws error for workflow with dynamic category",
}); });
test("getCategoryInputOrThrow throws error for workflow with multiple calls to analyze", (t) => { test("getCategoryInputOrThrow throws error for workflow with multiple calls to analyze", (t) => {
process.env["GITHUB_REPOSITORY"] = "github/codeql-action-fake-repository";
t.throws( t.throws(
() => () =>
getCategoryInputOrThrow( getCategoryInputOrThrow(

View file

@ -368,6 +368,19 @@ function getInputOrThrow(
return input; return input;
} }
/**
* Get the expected name of the analyze Action.
*
* This allows us to test workflow parsing functionality as a CodeQL Action PR check.
*/
function getAnalyzeActionName() {
if (getRequiredEnvParam("GITHUB_REPOSITORY") === "github/codeql-action") {
return "./analyze";
} else {
return "github/codeql-action/analyze";
}
}
/** /**
* Makes a best effort attempt to retrieve the category input for the particular job, * Makes a best effort attempt to retrieve the category input for the particular job,
* given a set of matrix variables. * given a set of matrix variables.
@ -385,7 +398,7 @@ export function getCategoryInputOrThrow(
return getInputOrThrow( return getInputOrThrow(
workflow, workflow,
jobName, jobName,
"github/codeql-action/analyze", getAnalyzeActionName(),
"category", "category",
matrixVars matrixVars
); );
@ -409,7 +422,7 @@ export function getUploadInputOrThrow(
getInputOrThrow( getInputOrThrow(
workflow, workflow,
jobName, jobName,
"github/codeql-action/analyze", getAnalyzeActionName(),
"upload", "upload",
matrixVars matrixVars
) || "true" // if unspecified, upload defaults to true ) || "true" // if unspecified, upload defaults to true
@ -434,7 +447,7 @@ export function getCheckoutPathInputOrThrow(
getInputOrThrow( getInputOrThrow(
workflow, workflow,
jobName, jobName,
"github/codeql-action/analyze", getAnalyzeActionName(),
"checkout_path", "checkout_path",
matrixVars matrixVars
) || getRequiredEnvParam("GITHUB_WORKSPACE") // if unspecified, checkout_path defaults to ${{ github.workspace }} ) || getRequiredEnvParam("GITHUB_WORKSPACE") // if unspecified, checkout_path defaults to ${{ github.workspace }}