|
| 1 | +import test from "ava"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | + |
| 4 | +import * as actionsUtil from "./actions-util"; |
| 5 | +import * as properties from "./feature-flags/properties"; |
| 6 | +import { RepositoryPropertyName } from "./feature-flags/properties"; |
| 7 | +import { RepositoryNwo } from "./repository"; |
| 8 | +import { resolveToolsInput } from "./resolve-tools-input"; |
| 9 | +import { getRecordingLogger, LoggedMessage, setupTests } from "./testing-utils"; |
| 10 | +import { Success } from "./util"; |
| 11 | + |
| 12 | +setupTests(test); |
| 13 | + |
| 14 | +const repositoryNwo = { owner: "owner", repo: "repo" } as RepositoryNwo; |
| 15 | + |
| 16 | +test.serial( |
| 17 | + "resolveToolsInput returns undefined when no tools input or repository property is set", |
| 18 | + async (t) => { |
| 19 | + const loggedMessages: LoggedMessage[] = []; |
| 20 | + const logger = getRecordingLogger(loggedMessages); |
| 21 | + |
| 22 | + sinon |
| 23 | + .stub(actionsUtil, "getOptionalInput") |
| 24 | + .withArgs("tools") |
| 25 | + .returns(undefined); |
| 26 | + |
| 27 | + sinon |
| 28 | + .stub(properties, "loadRepositoryProperties") |
| 29 | + .withArgs(repositoryNwo, logger) |
| 30 | + .resolves(undefined); |
| 31 | + |
| 32 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 33 | + |
| 34 | + t.is(result, undefined); |
| 35 | + t.is(loggedMessages.length, 0); // No logging when no tools input is resolved |
| 36 | + }, |
| 37 | +); |
| 38 | + |
| 39 | +test.serial( |
| 40 | + "resolveToolsInput returns workflow input when only workflow input is provided", |
| 41 | + async (t) => { |
| 42 | + const loggedMessages: LoggedMessage[] = []; |
| 43 | + const logger = getRecordingLogger(loggedMessages); |
| 44 | + |
| 45 | + sinon |
| 46 | + .stub(actionsUtil, "getOptionalInput") |
| 47 | + .withArgs("tools") |
| 48 | + .returns("latest"); |
| 49 | + sinon |
| 50 | + .stub(properties, "loadRepositoryProperties") |
| 51 | + .withArgs(repositoryNwo, logger) |
| 52 | + .resolves(undefined); |
| 53 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 54 | + |
| 55 | + t.is(result, "latest"); |
| 56 | + t.is(loggedMessages.length, 1); |
| 57 | + t.is( |
| 58 | + loggedMessages[0].message, |
| 59 | + "Setting tools: latest based on workflow input.", |
| 60 | + ); |
| 61 | + }, |
| 62 | +); |
| 63 | + |
| 64 | +test.serial( |
| 65 | + "resolveToolsInput returns repository property when only repository property is provided", |
| 66 | + async (t) => { |
| 67 | + const loggedMessages: LoggedMessage[] = []; |
| 68 | + const logger = getRecordingLogger(loggedMessages); |
| 69 | + |
| 70 | + sinon |
| 71 | + .stub(actionsUtil, "getOptionalInput") |
| 72 | + .withArgs("tools") |
| 73 | + .returns(undefined); |
| 74 | + |
| 75 | + const repositoryPropertiesResult = new Success({ |
| 76 | + [RepositoryPropertyName.TOOLS]: "toolcache", |
| 77 | + }); |
| 78 | + sinon |
| 79 | + .stub(properties, "loadRepositoryProperties") |
| 80 | + .withArgs(repositoryNwo, logger) |
| 81 | + .resolves(repositoryPropertiesResult); |
| 82 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 83 | + |
| 84 | + t.is(result, "toolcache"); |
| 85 | + t.is(loggedMessages.length, 2); |
| 86 | + t.is( |
| 87 | + loggedMessages[1].message, |
| 88 | + "Setting tools: toolcache based on the 'github-codeql-tools' repository property.", |
| 89 | + ); |
| 90 | + }, |
| 91 | +); |
| 92 | + |
| 93 | +test.serial( |
| 94 | + "resolveToolsInput prioritizes workflow input over repository property", |
| 95 | + async (t) => { |
| 96 | + const loggedMessages: LoggedMessage[] = []; |
| 97 | + const logger = getRecordingLogger(loggedMessages); |
| 98 | + |
| 99 | + sinon |
| 100 | + .stub(actionsUtil, "getOptionalInput") |
| 101 | + .withArgs("tools") |
| 102 | + .returns("nightly"); |
| 103 | + |
| 104 | + const repositoryPropertiesResult = new Success({ |
| 105 | + [RepositoryPropertyName.TOOLS]: "toolcache", |
| 106 | + }); |
| 107 | + sinon |
| 108 | + .stub(properties, "loadRepositoryProperties") |
| 109 | + .withArgs(repositoryNwo, logger) |
| 110 | + .resolves(repositoryPropertiesResult); |
| 111 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 112 | + |
| 113 | + t.is(result, "nightly"); |
| 114 | + t.is(loggedMessages.length, 2); |
| 115 | + t.is( |
| 116 | + loggedMessages[1].message, |
| 117 | + "Setting tools: nightly based on workflow input.", |
| 118 | + ); |
| 119 | + }, |
| 120 | +); |
| 121 | + |
| 122 | +test.serial( |
| 123 | + "resolveToolsInput handles empty string values correctly", |
| 124 | + async (t) => { |
| 125 | + const loggedMessages: LoggedMessage[] = []; |
| 126 | + const logger = getRecordingLogger(loggedMessages); |
| 127 | + |
| 128 | + sinon.stub(actionsUtil, "getOptionalInput").withArgs("tools").returns(""); |
| 129 | + |
| 130 | + const repositoryPropertiesResult = new Success({ |
| 131 | + [RepositoryPropertyName.TOOLS]: "toolcache", |
| 132 | + }); |
| 133 | + sinon |
| 134 | + .stub(properties, "loadRepositoryProperties") |
| 135 | + .withArgs(repositoryNwo, logger) |
| 136 | + .resolves(repositoryPropertiesResult); |
| 137 | + |
| 138 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 139 | + |
| 140 | + // Empty string is falsy, so should fall back to repository property |
| 141 | + t.is(result, "toolcache"); |
| 142 | + t.is(loggedMessages.length, 2); |
| 143 | + t.is( |
| 144 | + loggedMessages[1].message, |
| 145 | + "Setting tools: toolcache based on the 'github-codeql-tools' repository property.", |
| 146 | + ); |
| 147 | + }, |
| 148 | +); |
| 149 | + |
| 150 | +test.serial( |
| 151 | + "resolveToolsInput handles various tools input values correctly", |
| 152 | + async (t) => { |
| 153 | + const loggedMessages: LoggedMessage[] = []; |
| 154 | + const logger = getRecordingLogger(loggedMessages); |
| 155 | + |
| 156 | + // Test with specific version |
| 157 | + sinon |
| 158 | + .stub(actionsUtil, "getOptionalInput") |
| 159 | + .withArgs("tools") |
| 160 | + .returns("2.15.0"); |
| 161 | + |
| 162 | + const repositoryPropertiesResult = new Success({ |
| 163 | + [RepositoryPropertyName.TOOLS]: "2.20.0", |
| 164 | + }); |
| 165 | + sinon |
| 166 | + .stub(properties, "loadRepositoryProperties") |
| 167 | + .withArgs(repositoryNwo, logger) |
| 168 | + .resolves(repositoryPropertiesResult); |
| 169 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 170 | + t.is(result, "2.15.0"); |
| 171 | + t.is( |
| 172 | + loggedMessages[loggedMessages.length - 1].message, |
| 173 | + "Setting tools: 2.15.0 based on workflow input.", |
| 174 | + ); |
| 175 | + }, |
| 176 | +); |
| 177 | + |
| 178 | +test.serial( |
| 179 | + "resolveToolsInput handles URL input values correctly", |
| 180 | + async (t) => { |
| 181 | + const loggedMessages: LoggedMessage[] = []; |
| 182 | + const logger = getRecordingLogger(loggedMessages); |
| 183 | + |
| 184 | + // Test with URL |
| 185 | + sinon |
| 186 | + .stub(actionsUtil, "getOptionalInput") |
| 187 | + .withArgs("tools") |
| 188 | + .returns("https://example.com/codeql-bundle.tar.gz"); |
| 189 | + const repositoryPropertiesResult = new Success({ |
| 190 | + [RepositoryPropertyName.TOOLS]: |
| 191 | + "https://example.com/old-codeql-bundle.tar.gz", |
| 192 | + }); |
| 193 | + sinon |
| 194 | + .stub(properties, "loadRepositoryProperties") |
| 195 | + .withArgs(repositoryNwo, logger) |
| 196 | + .resolves(repositoryPropertiesResult); |
| 197 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 198 | + t.is(result, "https://example.com/codeql-bundle.tar.gz"); |
| 199 | + t.is( |
| 200 | + loggedMessages[loggedMessages.length - 1].message, |
| 201 | + "Setting tools: https://example.com/codeql-bundle.tar.gz based on workflow input.", |
| 202 | + ); |
| 203 | + }, |
| 204 | +); |
| 205 | + |
| 206 | +test.serial( |
| 207 | + "resolveToolsInput handles repository property with different values", |
| 208 | + async (t) => { |
| 209 | + const loggedMessages: LoggedMessage[] = []; |
| 210 | + const logger = getRecordingLogger(loggedMessages); |
| 211 | + |
| 212 | + sinon |
| 213 | + .stub(actionsUtil, "getOptionalInput") |
| 214 | + .withArgs("tools") |
| 215 | + .returns(undefined); |
| 216 | + |
| 217 | + // Test with "latest" |
| 218 | + const repositoryProperties = { |
| 219 | + [RepositoryPropertyName.TOOLS]: "latest", |
| 220 | + }; |
| 221 | + const repositoryPropertiesResult = new Success(repositoryProperties); |
| 222 | + sinon |
| 223 | + .stub(properties, "loadRepositoryProperties") |
| 224 | + .withArgs(repositoryNwo, logger) |
| 225 | + .resolves(repositoryPropertiesResult); |
| 226 | + |
| 227 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 228 | + t.is(result, "latest"); |
| 229 | + t.is( |
| 230 | + loggedMessages[loggedMessages.length - 1].message, |
| 231 | + "Setting tools: latest based on the 'github-codeql-tools' repository property.", |
| 232 | + ); |
| 233 | + }, |
| 234 | +); |
| 235 | + |
| 236 | +test.serial( |
| 237 | + "resolveToolsInput handles repository property with specific version", |
| 238 | + async (t) => { |
| 239 | + const loggedMessages: LoggedMessage[] = []; |
| 240 | + const logger = getRecordingLogger(loggedMessages); |
| 241 | + |
| 242 | + sinon |
| 243 | + .stub(actionsUtil, "getOptionalInput") |
| 244 | + .withArgs("tools") |
| 245 | + .returns(undefined); |
| 246 | + |
| 247 | + const repositoryProperties = { |
| 248 | + [RepositoryPropertyName.TOOLS]: "2.16.1", |
| 249 | + }; |
| 250 | + const repositoryPropertiesResult = new Success(repositoryProperties); |
| 251 | + sinon |
| 252 | + .stub(properties, "loadRepositoryProperties") |
| 253 | + .withArgs(repositoryNwo, logger) |
| 254 | + .resolves(repositoryPropertiesResult); |
| 255 | + |
| 256 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 257 | + t.is(result, "2.16.1"); |
| 258 | + t.is( |
| 259 | + loggedMessages[loggedMessages.length - 1].message, |
| 260 | + "Setting tools: 2.16.1 based on the 'github-codeql-tools' repository property.", |
| 261 | + ); |
| 262 | + }, |
| 263 | +); |
| 264 | + |
| 265 | +test.serial( |
| 266 | + "resolveToolsInput handles undefined repository property correctly", |
| 267 | + async (t) => { |
| 268 | + const loggedMessages: LoggedMessage[] = []; |
| 269 | + const logger = getRecordingLogger(loggedMessages); |
| 270 | + |
| 271 | + sinon |
| 272 | + .stub(actionsUtil, "getOptionalInput") |
| 273 | + .withArgs("tools") |
| 274 | + .returns(undefined); |
| 275 | + |
| 276 | + const repositoryProperties = { |
| 277 | + [RepositoryPropertyName.TOOLS]: undefined, |
| 278 | + }; |
| 279 | + |
| 280 | + const repositoryPropertiesResult = new Success(repositoryProperties); |
| 281 | + sinon |
| 282 | + .stub(properties, "loadRepositoryProperties") |
| 283 | + .withArgs(repositoryNwo, logger) |
| 284 | + .resolves(repositoryPropertiesResult); |
| 285 | + |
| 286 | + const result = await resolveToolsInput(repositoryNwo, logger); |
| 287 | + |
| 288 | + t.is(result, undefined); |
| 289 | + t.is(loggedMessages.length, 1); |
| 290 | + t.is( |
| 291 | + loggedMessages[0].message, |
| 292 | + "Loaded repository properties: github-codeql-tools", |
| 293 | + ); |
| 294 | + }, |
| 295 | +); |
0 commit comments