|
1 | 1 | import * as fs from "fs";
|
2 | 2 | import path from "path";
|
3 | 3 |
|
4 |
| -import test from "ava"; |
| 4 | +import test, { ExecutionContext } from "ava"; |
5 | 5 |
|
6 |
| -import { cleanupDatabaseClusterDirectory } from "./init"; |
| 6 | +import { ResolvePacksStep, setCodeQL } from "./codeql"; |
| 7 | +import { |
| 8 | + checkPacksForOverlayCompatibility, |
| 9 | + cleanupDatabaseClusterDirectory, |
| 10 | +} from "./init"; |
| 11 | +import { Language } from "./languages"; |
7 | 12 | import {
|
8 | 13 | LoggedMessage,
|
9 | 14 | createTestConfig,
|
@@ -106,3 +111,294 @@ for (const { runnerEnv, ErrorConstructor, message } of [
|
106 | 111 | });
|
107 | 112 | });
|
108 | 113 | }
|
| 114 | + |
| 115 | +type PackInfo = { |
| 116 | + kind: "query" | "library"; |
| 117 | + packinfoContents?: string; |
| 118 | +}; |
| 119 | + |
| 120 | +const testCheckPacksForOverlayCompatibility = test.macro({ |
| 121 | + exec: async ( |
| 122 | + t: ExecutionContext, |
| 123 | + _title: string, |
| 124 | + { |
| 125 | + cliOverlayVersion, |
| 126 | + languages, |
| 127 | + packs, |
| 128 | + expectedResult, |
| 129 | + }: { |
| 130 | + cliOverlayVersion: number | undefined; |
| 131 | + languages: Language[]; |
| 132 | + packs: Record<string, PackInfo>; |
| 133 | + expectedResult: boolean; |
| 134 | + }, |
| 135 | + ) => { |
| 136 | + await withTmpDir(async (tmpDir) => { |
| 137 | + const byNameFound = {}; |
| 138 | + const byNameAndVersionFound = {}; |
| 139 | + const steps: ResolvePacksStep[] = [ |
| 140 | + { |
| 141 | + type: "by-name", |
| 142 | + scans: [ |
| 143 | + { |
| 144 | + paths: [], |
| 145 | + found: byNameFound, |
| 146 | + }, |
| 147 | + ], |
| 148 | + }, |
| 149 | + { |
| 150 | + type: "by-name-and-version", |
| 151 | + found: byNameAndVersionFound, |
| 152 | + }, |
| 153 | + ]; |
| 154 | + |
| 155 | + for (const [packName, packInfo] of Object.entries(packs)) { |
| 156 | + const packPath = path.join(tmpDir, packName); |
| 157 | + fs.mkdirSync(packPath, { recursive: true }); |
| 158 | + if (packInfo.packinfoContents) { |
| 159 | + fs.writeFileSync( |
| 160 | + path.join(packPath, ".packinfo"), |
| 161 | + packInfo.packinfoContents, |
| 162 | + ); |
| 163 | + } |
| 164 | + if (packName.startsWith("codeql/")) { |
| 165 | + byNameFound[packName] = { |
| 166 | + kind: packInfo.kind, |
| 167 | + path: path.join(packPath, "qlpack.yml"), |
| 168 | + version: "1.0.0", |
| 169 | + }; |
| 170 | + } else { |
| 171 | + byNameAndVersionFound[packName] = { |
| 172 | + "1.0.0": { |
| 173 | + kind: packInfo.kind, |
| 174 | + path: path.join(packPath, "qlpack.yml"), |
| 175 | + }, |
| 176 | + }; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + const codeql = setCodeQL({ |
| 181 | + getVersion: async () => ({ |
| 182 | + version: "2.22.2", |
| 183 | + overlayVersion: cliOverlayVersion, |
| 184 | + }), |
| 185 | + resolvePacks: async () => ({ |
| 186 | + steps, |
| 187 | + }), |
| 188 | + }); |
| 189 | + |
| 190 | + const messages: LoggedMessage[] = []; |
| 191 | + const result = await checkPacksForOverlayCompatibility( |
| 192 | + codeql, |
| 193 | + createTestConfig({ dbLocation: tmpDir, languages }), |
| 194 | + getRecordingLogger(messages), |
| 195 | + ); |
| 196 | + t.is(result, expectedResult); |
| 197 | + t.deepEqual( |
| 198 | + messages.length, |
| 199 | + expectedResult ? 0 : 1, |
| 200 | + "Expected log messages", |
| 201 | + ); |
| 202 | + }); |
| 203 | + }, |
| 204 | + title: (_, title) => `checkPacksForOverlayCompatibility: ${title}`, |
| 205 | +}); |
| 206 | + |
| 207 | +test( |
| 208 | + testCheckPacksForOverlayCompatibility, |
| 209 | + "returns false when CLI does not support overlay", |
| 210 | + { |
| 211 | + cliOverlayVersion: undefined, |
| 212 | + languages: [Language.java], |
| 213 | + packs: { |
| 214 | + "codeql/java-queries": { |
| 215 | + kind: "query", |
| 216 | + packinfoContents: '{"overlayVersion":2}', |
| 217 | + }, |
| 218 | + }, |
| 219 | + expectedResult: false, |
| 220 | + }, |
| 221 | +); |
| 222 | + |
| 223 | +test( |
| 224 | + testCheckPacksForOverlayCompatibility, |
| 225 | + "returns true when there are no bundled query packs", |
| 226 | + { |
| 227 | + cliOverlayVersion: 2, |
| 228 | + languages: [Language.java], |
| 229 | + packs: { |
| 230 | + "custom/queries": { |
| 231 | + kind: "query", |
| 232 | + packinfoContents: '{"overlayVersion":2}', |
| 233 | + }, |
| 234 | + }, |
| 235 | + expectedResult: true, |
| 236 | + }, |
| 237 | +); |
| 238 | + |
| 239 | +test( |
| 240 | + testCheckPacksForOverlayCompatibility, |
| 241 | + "returns true when bundled query pack has expected overlay version", |
| 242 | + { |
| 243 | + cliOverlayVersion: 2, |
| 244 | + languages: [Language.java], |
| 245 | + packs: { |
| 246 | + "codeql/java-queries": { |
| 247 | + kind: "query", |
| 248 | + packinfoContents: '{"overlayVersion":2}', |
| 249 | + }, |
| 250 | + }, |
| 251 | + expectedResult: true, |
| 252 | + }, |
| 253 | +); |
| 254 | + |
| 255 | +test( |
| 256 | + testCheckPacksForOverlayCompatibility, |
| 257 | + "returns true when bundled query pack for another language is missing overlayVersion in .packinfo", |
| 258 | + { |
| 259 | + cliOverlayVersion: 2, |
| 260 | + languages: [Language.java], |
| 261 | + packs: { |
| 262 | + "codeql/cpp-queries": { |
| 263 | + kind: "query", |
| 264 | + packinfoContents: "{}", |
| 265 | + }, |
| 266 | + }, |
| 267 | + expectedResult: true, |
| 268 | + }, |
| 269 | +); |
| 270 | + |
| 271 | +test( |
| 272 | + testCheckPacksForOverlayCompatibility, |
| 273 | + "returns false when bundled query pack has different overlay version", |
| 274 | + { |
| 275 | + cliOverlayVersion: 2, |
| 276 | + languages: [Language.java], |
| 277 | + packs: { |
| 278 | + "codeql/java-queries": { |
| 279 | + kind: "query", |
| 280 | + packinfoContents: '{"overlayVersion":1}', |
| 281 | + }, |
| 282 | + }, |
| 283 | + expectedResult: false, |
| 284 | + }, |
| 285 | +); |
| 286 | + |
| 287 | +test( |
| 288 | + testCheckPacksForOverlayCompatibility, |
| 289 | + "returns true when there are local library packs", |
| 290 | + { |
| 291 | + cliOverlayVersion: 2, |
| 292 | + languages: [Language.java], |
| 293 | + packs: { |
| 294 | + "codeql/java-queries": { |
| 295 | + kind: "query", |
| 296 | + packinfoContents: '{"overlayVersion":2}', |
| 297 | + }, |
| 298 | + "custom/library": { |
| 299 | + kind: "library", |
| 300 | + }, |
| 301 | + }, |
| 302 | + expectedResult: true, |
| 303 | + }, |
| 304 | +); |
| 305 | + |
| 306 | +test( |
| 307 | + testCheckPacksForOverlayCompatibility, |
| 308 | + "returns true when local query pack has expected overlay version", |
| 309 | + { |
| 310 | + cliOverlayVersion: 2, |
| 311 | + languages: [Language.java], |
| 312 | + packs: { |
| 313 | + "codeql/java-queries": { |
| 314 | + kind: "query", |
| 315 | + packinfoContents: '{"overlayVersion":2}', |
| 316 | + }, |
| 317 | + "custom/queries": { |
| 318 | + kind: "query", |
| 319 | + packinfoContents: '{"overlayVersion":2}', |
| 320 | + }, |
| 321 | + }, |
| 322 | + expectedResult: true, |
| 323 | + }, |
| 324 | +); |
| 325 | + |
| 326 | +test( |
| 327 | + testCheckPacksForOverlayCompatibility, |
| 328 | + "returns false when local query pack is missing .packinfo", |
| 329 | + { |
| 330 | + cliOverlayVersion: 2, |
| 331 | + languages: [Language.java], |
| 332 | + packs: { |
| 333 | + "codeql/java-queries": { |
| 334 | + kind: "query", |
| 335 | + packinfoContents: '{"overlayVersion":2}', |
| 336 | + }, |
| 337 | + "custom/queries": { |
| 338 | + kind: "query", |
| 339 | + packinfoContents: undefined, |
| 340 | + }, |
| 341 | + }, |
| 342 | + expectedResult: false, |
| 343 | + }, |
| 344 | +); |
| 345 | + |
| 346 | +test( |
| 347 | + testCheckPacksForOverlayCompatibility, |
| 348 | + "returns false when local query pack has different overlay version", |
| 349 | + { |
| 350 | + cliOverlayVersion: 2, |
| 351 | + languages: [Language.java], |
| 352 | + packs: { |
| 353 | + "codeql/java-queries": { |
| 354 | + kind: "query", |
| 355 | + packinfoContents: '{"overlayVersion":2}', |
| 356 | + }, |
| 357 | + "custom/queries": { |
| 358 | + kind: "query", |
| 359 | + packinfoContents: '{"overlayVersion":1}', |
| 360 | + }, |
| 361 | + }, |
| 362 | + expectedResult: false, |
| 363 | + }, |
| 364 | +); |
| 365 | + |
| 366 | +test( |
| 367 | + testCheckPacksForOverlayCompatibility, |
| 368 | + "returns false when local query pack is missing overlayVersion in .packinfo", |
| 369 | + { |
| 370 | + cliOverlayVersion: 2, |
| 371 | + languages: [Language.java], |
| 372 | + packs: { |
| 373 | + "codeql/java-queries": { |
| 374 | + kind: "query", |
| 375 | + packinfoContents: '{"overlayVersion":2}', |
| 376 | + }, |
| 377 | + "custom/queries": { |
| 378 | + kind: "query", |
| 379 | + packinfoContents: "{}", |
| 380 | + }, |
| 381 | + }, |
| 382 | + expectedResult: false, |
| 383 | + }, |
| 384 | +); |
| 385 | + |
| 386 | +test( |
| 387 | + testCheckPacksForOverlayCompatibility, |
| 388 | + "returns false when .packinfo is not valid JSON", |
| 389 | + { |
| 390 | + cliOverlayVersion: 2, |
| 391 | + languages: [Language.java], |
| 392 | + packs: { |
| 393 | + "codeql/java-queries": { |
| 394 | + kind: "query", |
| 395 | + packinfoContents: '{"overlayVersion":2}', |
| 396 | + }, |
| 397 | + "custom/queries": { |
| 398 | + kind: "query", |
| 399 | + packinfoContents: "this_is_not_valid_json", |
| 400 | + }, |
| 401 | + }, |
| 402 | + expectedResult: false, |
| 403 | + }, |
| 404 | +); |
0 commit comments