|
| 1 | +import test, { ExecutionContext } from "ava"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | + |
| 4 | +import * as actionsUtil from "./actions-util"; |
| 5 | +import type { PullRequestBranches } from "./actions-util"; |
| 6 | +import * as apiClient from "./api-client"; |
| 7 | +import { shouldPerformDiffInformedAnalysis } from "./diff-informed-analysis-utils"; |
| 8 | +import { Feature, Features } from "./feature-flags"; |
| 9 | +import { getRunnerLogger } from "./logging"; |
| 10 | +import { parseRepositoryNwo } from "./repository"; |
| 11 | +import { |
| 12 | + setupTests, |
| 13 | + mockCodeQLVersion, |
| 14 | + mockFeatureFlagApiEndpoint, |
| 15 | + setupActionsVars, |
| 16 | +} from "./testing-utils"; |
| 17 | +import { GitHubVariant, withTmpDir } from "./util"; |
| 18 | +import type { GitHubVersion } from "./util"; |
| 19 | + |
| 20 | +setupTests(test); |
| 21 | + |
| 22 | +interface DiffInformedAnalysisTestCase { |
| 23 | + featureEnabled: boolean; |
| 24 | + gitHubVersion: GitHubVersion; |
| 25 | + pullRequestBranches: PullRequestBranches; |
| 26 | + codeQLVersion: string; |
| 27 | + diffInformedQueriesEnvVar?: boolean; |
| 28 | +} |
| 29 | + |
| 30 | +const defaultTestCase: DiffInformedAnalysisTestCase = { |
| 31 | + featureEnabled: true, |
| 32 | + gitHubVersion: { |
| 33 | + type: GitHubVariant.DOTCOM, |
| 34 | + }, |
| 35 | + pullRequestBranches: { |
| 36 | + base: "main", |
| 37 | + head: "feature-branch", |
| 38 | + }, |
| 39 | + codeQLVersion: "2.21.0", |
| 40 | +}; |
| 41 | + |
| 42 | +const testShouldPerformDiffInformedAnalysis = test.macro({ |
| 43 | + exec: async ( |
| 44 | + t: ExecutionContext, |
| 45 | + _title: string, |
| 46 | + partialTestCase: Partial<DiffInformedAnalysisTestCase>, |
| 47 | + expectedResult: boolean, |
| 48 | + ) => { |
| 49 | + return await withTmpDir(async (tmpDir) => { |
| 50 | + setupActionsVars(tmpDir, tmpDir); |
| 51 | + |
| 52 | + const testCase = { ...defaultTestCase, ...partialTestCase }; |
| 53 | + const logger = getRunnerLogger(true); |
| 54 | + const codeql = mockCodeQLVersion(testCase.codeQLVersion); |
| 55 | + |
| 56 | + if (testCase.diffInformedQueriesEnvVar !== undefined) { |
| 57 | + process.env.CODEQL_ACTION_DIFF_INFORMED_QUERIES = |
| 58 | + testCase.diffInformedQueriesEnvVar.toString(); |
| 59 | + } else { |
| 60 | + delete process.env.CODEQL_ACTION_DIFF_INFORMED_QUERIES; |
| 61 | + } |
| 62 | + |
| 63 | + const features = new Features( |
| 64 | + testCase.gitHubVersion, |
| 65 | + parseRepositoryNwo("github/example"), |
| 66 | + tmpDir, |
| 67 | + logger, |
| 68 | + ); |
| 69 | + mockFeatureFlagApiEndpoint(200, { |
| 70 | + [Feature.DiffInformedQueries]: testCase.featureEnabled, |
| 71 | + }); |
| 72 | + |
| 73 | + const getGitHubVersionStub = sinon |
| 74 | + .stub(apiClient, "getGitHubVersion") |
| 75 | + .resolves(testCase.gitHubVersion); |
| 76 | + const getPullRequestBranchesStub = sinon |
| 77 | + .stub(actionsUtil, "getPullRequestBranches") |
| 78 | + .returns(testCase.pullRequestBranches); |
| 79 | + |
| 80 | + const result = await shouldPerformDiffInformedAnalysis( |
| 81 | + codeql, |
| 82 | + features, |
| 83 | + logger, |
| 84 | + ); |
| 85 | + |
| 86 | + t.is(result, expectedResult); |
| 87 | + |
| 88 | + delete process.env.CODEQL_ACTION_DIFF_INFORMED_QUERIES; |
| 89 | + |
| 90 | + getGitHubVersionStub.restore(); |
| 91 | + getPullRequestBranchesStub.restore(); |
| 92 | + }); |
| 93 | + }, |
| 94 | + title: (_, title) => `shouldPerformDiffInformedAnalysis: ${title}`, |
| 95 | +}); |
| 96 | + |
| 97 | +test( |
| 98 | + testShouldPerformDiffInformedAnalysis, |
| 99 | + "returns true in the default test case", |
| 100 | + {}, |
| 101 | + true, |
| 102 | +); |
| 103 | + |
| 104 | +test( |
| 105 | + testShouldPerformDiffInformedAnalysis, |
| 106 | + "returns false when feature flag is disabled from the API", |
| 107 | + { |
| 108 | + featureEnabled: false, |
| 109 | + }, |
| 110 | + false, |
| 111 | +); |
| 112 | + |
| 113 | +test( |
| 114 | + testShouldPerformDiffInformedAnalysis, |
| 115 | + "returns false when CODEQL_ACTION_DIFF_INFORMED_QUERIES is set to false", |
| 116 | + { |
| 117 | + featureEnabled: true, |
| 118 | + diffInformedQueriesEnvVar: false, |
| 119 | + }, |
| 120 | + false, |
| 121 | +); |
| 122 | + |
| 123 | +test( |
| 124 | + testShouldPerformDiffInformedAnalysis, |
| 125 | + "returns true when CODEQL_ACTION_DIFF_INFORMED_QUERIES is set to true", |
| 126 | + { |
| 127 | + featureEnabled: false, |
| 128 | + diffInformedQueriesEnvVar: true, |
| 129 | + }, |
| 130 | + true, |
| 131 | +); |
| 132 | + |
| 133 | +test( |
| 134 | + testShouldPerformDiffInformedAnalysis, |
| 135 | + "returns false for CodeQL version 2.20.0", |
| 136 | + { |
| 137 | + codeQLVersion: "2.20.0", |
| 138 | + }, |
| 139 | + false, |
| 140 | +); |
| 141 | + |
| 142 | +test( |
| 143 | + testShouldPerformDiffInformedAnalysis, |
| 144 | + "returns false for invalid GHES version", |
| 145 | + { |
| 146 | + gitHubVersion: { |
| 147 | + type: GitHubVariant.GHES, |
| 148 | + version: "invalid-version", |
| 149 | + }, |
| 150 | + }, |
| 151 | + false, |
| 152 | +); |
| 153 | + |
| 154 | +test( |
| 155 | + testShouldPerformDiffInformedAnalysis, |
| 156 | + "returns false for GHES version 3.18.5", |
| 157 | + { |
| 158 | + gitHubVersion: { |
| 159 | + type: GitHubVariant.GHES, |
| 160 | + version: "3.18.5", |
| 161 | + }, |
| 162 | + }, |
| 163 | + false, |
| 164 | +); |
| 165 | + |
| 166 | +test( |
| 167 | + testShouldPerformDiffInformedAnalysis, |
| 168 | + "returns true for GHES version 3.19.0", |
| 169 | + { |
| 170 | + gitHubVersion: { |
| 171 | + type: GitHubVariant.GHES, |
| 172 | + version: "3.19.0", |
| 173 | + }, |
| 174 | + }, |
| 175 | + true, |
| 176 | +); |
| 177 | + |
| 178 | +test( |
| 179 | + testShouldPerformDiffInformedAnalysis, |
| 180 | + "returns false when not a pull request", |
| 181 | + { |
| 182 | + pullRequestBranches: undefined, |
| 183 | + }, |
| 184 | + false, |
| 185 | +); |
0 commit comments