|
| 1 | +import test from "ava"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | + |
| 4 | +import * as actionsUtil from "./actions-util"; |
| 5 | +import * as analyze from "./analyze"; |
| 6 | +import { runWrapper } from "./analyze-action"; |
| 7 | +import * as api from "./api-client"; |
| 8 | +import * as configUtils from "./config-utils"; |
| 9 | +import * as gitUtils from "./git-utils"; |
| 10 | +import * as statusReport from "./status-report"; |
| 11 | +import { |
| 12 | + setupTests, |
| 13 | + setupActionsVars, |
| 14 | + mockFeatureFlagApiEndpoint, |
| 15 | +} from "./testing-utils"; |
| 16 | +import * as util from "./util"; |
| 17 | + |
| 18 | +setupTests(test); |
| 19 | + |
| 20 | +test.serial( |
| 21 | + "analyze action with RAM & threads from environment variables", |
| 22 | + async (t) => { |
| 23 | + // This test frequently times out on Windows with the default timeout, so we bump |
| 24 | + // it a bit to 20s. |
| 25 | + t.timeout(1000 * 20); |
| 26 | + await util.withTmpDir(async (tmpDir) => { |
| 27 | + setupActionsVars(tmpDir, tmpDir); |
| 28 | + sinon |
| 29 | + .stub(statusReport, "createStatusReportBase") |
| 30 | + .resolves({} as statusReport.StatusReportBase); |
| 31 | + sinon.stub(statusReport, "sendStatusReport").resolves(); |
| 32 | + sinon.stub(gitUtils, "isAnalyzingDefaultBranch").resolves(true); |
| 33 | + |
| 34 | + const gitHubVersion: util.GitHubVersion = { |
| 35 | + type: util.GitHubVariant.DOTCOM, |
| 36 | + }; |
| 37 | + sinon.stub(configUtils, "getConfig").resolves({ |
| 38 | + gitHubVersion, |
| 39 | + augmentationProperties: {}, |
| 40 | + languages: [], |
| 41 | + packs: [], |
| 42 | + trapCaches: {}, |
| 43 | + } as unknown as configUtils.Config); |
| 44 | + const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput"); |
| 45 | + requiredInputStub.withArgs("token").returns("fake-token"); |
| 46 | + requiredInputStub.withArgs("upload-database").returns("false"); |
| 47 | + requiredInputStub.withArgs("output").returns("out"); |
| 48 | + const optionalInputStub = sinon.stub(actionsUtil, "getOptionalInput"); |
| 49 | + optionalInputStub.withArgs("expect-error").returns("false"); |
| 50 | + sinon.stub(api, "getGitHubVersion").resolves(gitHubVersion); |
| 51 | + mockFeatureFlagApiEndpoint(200, {}); |
| 52 | + |
| 53 | + // When there are no action inputs for RAM and threads, the action uses |
| 54 | + // environment variables (passed down from the init action) to set RAM and |
| 55 | + // threads usage. |
| 56 | + process.env["CODEQL_THREADS"] = "-1"; |
| 57 | + process.env["CODEQL_RAM"] = "4992"; |
| 58 | + |
| 59 | + const runFinalizeStub = sinon.stub(analyze, "runFinalize"); |
| 60 | + const runQueriesStub = sinon.stub(analyze, "runQueries"); |
| 61 | + |
| 62 | + await runWrapper(); |
| 63 | + |
| 64 | + t.assert( |
| 65 | + runFinalizeStub.calledOnceWith( |
| 66 | + sinon.match.any, |
| 67 | + sinon.match.any, |
| 68 | + "--threads=-1", |
| 69 | + "--ram=4992", |
| 70 | + ), |
| 71 | + ); |
| 72 | + t.assert( |
| 73 | + runQueriesStub.calledOnceWith( |
| 74 | + sinon.match.any, |
| 75 | + "--ram=4992", |
| 76 | + "--threads=-1", |
| 77 | + ), |
| 78 | + ); |
| 79 | + }); |
| 80 | + }, |
| 81 | +); |
| 82 | + |
| 83 | +test.serial( |
| 84 | + "analyze action with RAM & threads from action inputs", |
| 85 | + async (t) => { |
| 86 | + t.timeout(1000 * 20); |
| 87 | + await util.withTmpDir(async (tmpDir) => { |
| 88 | + setupActionsVars(tmpDir, tmpDir); |
| 89 | + sinon |
| 90 | + .stub(statusReport, "createStatusReportBase") |
| 91 | + .resolves({} as statusReport.StatusReportBase); |
| 92 | + sinon.stub(statusReport, "sendStatusReport").resolves(); |
| 93 | + const gitHubVersion: util.GitHubVersion = { |
| 94 | + type: util.GitHubVariant.DOTCOM, |
| 95 | + }; |
| 96 | + sinon.stub(configUtils, "getConfig").resolves({ |
| 97 | + gitHubVersion, |
| 98 | + augmentationProperties: {}, |
| 99 | + languages: [], |
| 100 | + packs: [], |
| 101 | + trapCaches: {}, |
| 102 | + } as unknown as configUtils.Config); |
| 103 | + const requiredInputStub = sinon.stub(actionsUtil, "getRequiredInput"); |
| 104 | + requiredInputStub.withArgs("token").returns("fake-token"); |
| 105 | + requiredInputStub.withArgs("upload-database").returns("false"); |
| 106 | + requiredInputStub.withArgs("output").returns("out"); |
| 107 | + const optionalInputStub = sinon.stub(actionsUtil, "getOptionalInput"); |
| 108 | + optionalInputStub.withArgs("expect-error").returns("false"); |
| 109 | + sinon.stub(api, "getGitHubVersion").resolves(gitHubVersion); |
| 110 | + sinon.stub(gitUtils, "isAnalyzingDefaultBranch").resolves(true); |
| 111 | + mockFeatureFlagApiEndpoint(200, {}); |
| 112 | + |
| 113 | + process.env["CODEQL_THREADS"] = "1"; |
| 114 | + process.env["CODEQL_RAM"] = "4992"; |
| 115 | + |
| 116 | + // Action inputs have precedence over environment variables. |
| 117 | + optionalInputStub.withArgs("threads").returns("-1"); |
| 118 | + optionalInputStub.withArgs("ram").returns("3012"); |
| 119 | + |
| 120 | + const runFinalizeStub = sinon.stub(analyze, "runFinalize"); |
| 121 | + const runQueriesStub = sinon.stub(analyze, "runQueries"); |
| 122 | + |
| 123 | + await runWrapper(); |
| 124 | + |
| 125 | + t.assert( |
| 126 | + runFinalizeStub.calledOnceWith( |
| 127 | + sinon.match.any, |
| 128 | + sinon.match.any, |
| 129 | + "--threads=-1", |
| 130 | + "--ram=3012", |
| 131 | + ), |
| 132 | + ); |
| 133 | + t.assert( |
| 134 | + runQueriesStub.calledOnceWith( |
| 135 | + sinon.match.any, |
| 136 | + "--ram=3012", |
| 137 | + "--threads=-1", |
| 138 | + ), |
| 139 | + ); |
| 140 | + }); |
| 141 | + }, |
| 142 | +); |
0 commit comments