GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — codeql-action/src/codeql.test.ts at codeql-bundle-v2.25.0 · github/codeql-action Expand file tree/ Copy pathcodeql.test.tsMore file actions 1110 lines (1003 loc) · 31.2 KB / Copy pathcodeql.test.tsFile metadata and controls1110 lines (1003 loc) · 31.2 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 import * as fs from "fs"; import { ExecOptions } from "@actions/exec"; import * as toolrunner from "@actions/exec/lib/toolrunner"; import * as io from "@actions/io"; import * as toolcache from "@actions/tool-cache"; import test, { ExecutionContext } from "ava"; import * as yaml from "js-yaml"; import nock from "nock"; import * as sinon from "sinon"; import * as actionsUtil from "./actions-util"; import { GitHubApiDetails } from "./api-client"; import { CliError } from "./cli-errors"; import * as codeql from "./codeql"; import { AugmentationProperties, generateCodeScanningConfig, defaultAugmentationProperties, } from "./config/db-config"; import type { Config } from "./config-utils"; import * as defaults from "./defaults.json"; import { DocUrl } from "./doc-url"; import { KnownLanguage } from "./languages"; import { getRunnerLogger } from "./logging"; import { ToolsSource } from "./setup-codeql"; import { setupTests, createFeatures, setupActionsVars, SAMPLE_DOTCOM_API_DETAILS, SAMPLE_DEFAULT_CLI_VERSION, mockBundleDownloadApi, makeVersionInfo, createTestConfig, } from "./testing-utils"; import { ToolsDownloadStatusReport } from "./tools-download"; import * as util from "./util"; import { initializeEnvironment } from "./util"; setupTests(test); let stubConfig: Config; test.beforeEach(() => { initializeEnvironment("1.2.3"); stubConfig = createTestConfig({ languages: [KnownLanguage.cpp], }); }); async function installIntoToolcache({ apiDetails = SAMPLE_DOTCOM_API_DETAILS, cliVersion, isPinned, tagName, tmpDir, }: { apiDetails?: GitHubApiDetails; cliVersion?: string; isPinned: boolean; tagName: string; tmpDir: string; }) { const url = mockBundleDownloadApi({ apiDetails, isPinned, tagName }); await codeql.setupCodeQL( cliVersion !== undefined ? undefined : url, apiDetails, tmpDir, util.GitHubVariant.GHES, cliVersion !== undefined ? { cliVersion, tagName } : SAMPLE_DEFAULT_CLI_VERSION, createFeatures([]), getRunnerLogger(true), false, ); } function mockReleaseApi({ apiDetails = SAMPLE_DOTCOM_API_DETAILS, assetNames, tagName, }: { apiDetails?: GitHubApiDetails; assetNames: string[]; tagName: string; }): nock.Scope { return nock(apiDetails.apiURL!) .get(`/repos/github/codeql-action/releases/tags/${tagName}`) .reply(200, { assets: assetNames.map((name) => ({ name, })), tag_name: tagName, }); } function mockApiDetails(apiDetails: GitHubApiDetails) { // This is a workaround to mock `api.getApiDetails()` since it doesn't seem to be possible to // mock this directly. The difficulty is that `getApiDetails()` is called locally in // `api-client.ts`, but `sinon.stub(api, "getApiDetails")` only affects calls to // `getApiDetails()` via an imported `api` module. sinon .stub(actionsUtil, "getRequiredInput") .withArgs("token") .returns(apiDetails.auth); process.env["GITHUB_SERVER_URL"] = apiDetails.url; process.env["GITHUB_API_URL"] = apiDetails.apiURL || ""; } async function stubCodeql(): Promise<codeql.CodeQL> { const codeqlObject = await codeql.getCodeQLForTesting(); sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6")); sinon .stub(codeqlObject, "isTracedLanguage") .withArgs(KnownLanguage.cpp) .resolves(true); return codeqlObject; } test.serial( "downloads and caches explicitly requested bundles that aren't in the toolcache", async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); const versions = ["20200601", "20200610"]; for (let i = 0; i < versions.length; i++) { const version = versions[i]; const url = mockBundleDownloadApi({ tagName: `codeql-bundle-${version}`, isPinned: false, }); const result = await codeql.setupCodeQL( url, SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.DOTCOM, SAMPLE_DEFAULT_CLI_VERSION, features, getRunnerLogger(true), false, ); t.assert(toolcache.find("CodeQL", `0.0.0-${version}`)); t.is(result.toolsVersion, `0.0.0-${version}`); t.is(result.toolsSource, ToolsSource.Download); } t.is(toolcache.findAllVersions("CodeQL").length, 2); }); }, ); test.serial( "caches semantically versioned bundles using their semantic version number", async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); const url = mockBundleDownloadApi({ tagName: `codeql-bundle-v2.15.0`, isPinned: false, }); const result = await codeql.setupCodeQL( url, SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.DOTCOM, SAMPLE_DEFAULT_CLI_VERSION, features, getRunnerLogger(true), false, ); t.is(toolcache.findAllVersions("CodeQL").length, 1); t.assert(toolcache.find("CodeQL", `2.15.0`)); t.is(result.toolsVersion, `2.15.0`); t.is(result.toolsSource, ToolsSource.Download); if (result.toolsDownloadStatusReport) { assertDurationsInteger(t, result.toolsDownloadStatusReport); } }); }, ); test.serial( "downloads an explicitly requested bundle even if a different version is cached", async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); await installIntoToolcache({ tagName: "codeql-bundle-20200601", isPinned: true, tmpDir, }); const url = mockBundleDownloadApi({ tagName: "codeql-bundle-20200610", }); const result = await codeql.setupCodeQL( url, SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.DOTCOM, SAMPLE_DEFAULT_CLI_VERSION, features, getRunnerLogger(true), false, ); t.assert(toolcache.find("CodeQL", "0.0.0-20200610")); t.deepEqual(result.toolsVersion, "0.0.0-20200610"); t.is(result.toolsSource, ToolsSource.Download); if (result.toolsDownloadStatusReport) { assertDurationsInteger(t, result.toolsDownloadStatusReport); } }); }, ); const EXPLICITLY_REQUESTED_BUNDLE_TEST_CASES = [ { tagName: "codeql-bundle-2.17.6", expectedToolcacheVersion: "2.17.6", }, { tagName: "codeql-bundle-20240805", expectedToolcacheVersion: "0.0.0-20240805", }, ]; for (const { tagName, expectedToolcacheVersion, } of EXPLICITLY_REQUESTED_BUNDLE_TEST_CASES) { test.serial( `caches explicitly requested bundle ${tagName} as ${expectedToolcacheVersion}`, async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); mockApiDetails(SAMPLE_DOTCOM_API_DETAILS); sinon.stub(actionsUtil, "isRunningLocalAction").returns(true); const url = mockBundleDownloadApi({ tagName, }); const result = await codeql.setupCodeQL( url, SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.DOTCOM, SAMPLE_DEFAULT_CLI_VERSION, features, getRunnerLogger(true), false, ); t.assert(toolcache.find("CodeQL", expectedToolcacheVersion)); t.deepEqual(result.toolsVersion, expectedToolcacheVersion); t.is(result.toolsSource, ToolsSource.Download); t.assert( Number.isInteger( result.toolsDownloadStatusReport?.downloadDurationMs, ), ); }); }, ); } for (const toolcacheVersion of [ // Test that we use the tools from the toolcache when `SAMPLE_DEFAULT_CLI_VERSION` is requested // and `SAMPLE_DEFAULT_CLI_VERSION-` is in the toolcache. SAMPLE_DEFAULT_CLI_VERSION.cliVersion, `${SAMPLE_DEFAULT_CLI_VERSION.cliVersion}-20230101`, ]) { test.serial( `uses tools from toolcache when ${SAMPLE_DEFAULT_CLI_VERSION.cliVersion} is requested and ` + `${toolcacheVersion} is installed`, async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); sinon .stub(toolcache, "find") .withArgs("CodeQL", toolcacheVersion) .returns("path/to/cached/codeql"); sinon.stub(toolcache, "findAllVersions").returns([toolcacheVersion]); const result = await codeql.setupCodeQL( undefined, SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.DOTCOM, SAMPLE_DEFAULT_CLI_VERSION, features, getRunnerLogger(true), false, ); t.is(result.toolsVersion, SAMPLE_DEFAULT_CLI_VERSION.cliVersion); t.is(result.toolsSource, ToolsSource.Toolcache); t.is(result.toolsDownloadStatusReport?.combinedDurationMs, undefined); t.is(result.toolsDownloadStatusReport?.downloadDurationMs, undefined); t.is(result.toolsDownloadStatusReport?.extractionDurationMs, undefined); }); }, ); } test.serial( `uses a cached bundle when no tools input is given on GHES`, async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); await installIntoToolcache({ tagName: "codeql-bundle-20200601", isPinned: true, tmpDir, }); const result = await codeql.setupCodeQL( undefined, SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.GHES, { cliVersion: defaults.cliVersion, tagName: defaults.bundleVersion, }, features, getRunnerLogger(true), false, ); t.deepEqual(result.toolsVersion, "0.0.0-20200601"); t.is(result.toolsSource, ToolsSource.Toolcache); t.is(result.toolsDownloadStatusReport?.combinedDurationMs, undefined); t.is(result.toolsDownloadStatusReport?.downloadDurationMs, undefined); t.is(result.toolsDownloadStatusReport?.extractionDurationMs, undefined); const cachedVersions = toolcache.findAllVersions("CodeQL"); t.is(cachedVersions.length, 1); }); }, ); test.serial( `downloads bundle if only an unpinned version is cached on GHES`, async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); await installIntoToolcache({ tagName: "codeql-bundle-20200601", isPinned: false, tmpDir, }); mockBundleDownloadApi({ tagName: defaults.bundleVersion, }); const result = await codeql.setupCodeQL( undefined, SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.GHES, { cliVersion: defaults.cliVersion, tagName: defaults.bundleVersion, }, features, getRunnerLogger(true), false, ); t.deepEqual(result.toolsVersion, defaults.cliVersion); t.is(result.toolsSource, ToolsSource.Download); if (result.toolsDownloadStatusReport) { assertDurationsInteger(t, result.toolsDownloadStatusReport); } const cachedVersions = toolcache.findAllVersions("CodeQL"); t.is(cachedVersions.length, 2); }); }, ); test.serial( 'downloads bundle if "latest" tools specified but not cached', async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); await installIntoToolcache({ tagName: "codeql-bundle-20200601", isPinned: true, tmpDir, }); mockBundleDownloadApi({ tagName: defaults.bundleVersion, }); const result = await codeql.setupCodeQL( "latest", SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.DOTCOM, SAMPLE_DEFAULT_CLI_VERSION, features, getRunnerLogger(true), false, ); t.deepEqual(result.toolsVersion, defaults.cliVersion); t.is(result.toolsSource, ToolsSource.Download); if (result.toolsDownloadStatusReport) { assertDurationsInteger(t, result.toolsDownloadStatusReport); } const cachedVersions = toolcache.findAllVersions("CodeQL"); t.is(cachedVersions.length, 2); }); }, ); test.serial( "bundle URL from another repo is cached as 0.0.0-bundleVersion", async (t) => { const features = createFeatures([]); await util.withTmpDir(async (tmpDir) => { setupActionsVars(tmpDir, tmpDir); mockApiDetails(SAMPLE_DOTCOM_API_DETAILS); sinon.stub(actionsUtil, "isRunningLocalAction").returns(true); const releasesApiMock = mockReleaseApi({ assetNames: ["cli-version-2.14.6.txt"], tagName: "codeql-bundle-20230203", }); mockBundleDownloadApi({ repo: "codeql-testing/codeql-cli-nightlies", platformSpecific: false, tagName: "codeql-bundle-20230203", }); const result = await codeql.setupCodeQL( "https://github.com/codeql-testing/codeql-cli-nightlies/releases/download/codeql-bundle-20230203/codeql-bundle.tar.gz", SAMPLE_DOTCOM_API_DETAILS, tmpDir, util.GitHubVariant.DOTCOM, SAMPLE_DEFAULT_CLI_VERSION, features, getRunnerLogger(true), false, ); t.is(result.toolsVersion, "0.0.0-20230203"); t.is(result.toolsSource, ToolsSource.Download); if (result.toolsDownloadStatusReport) { assertDurationsInteger(t, result.toolsDownloadStatusReport); } const cachedVersions = toolcache.findAllVersions("CodeQL"); t.is(cachedVersions.length, 1); t.is(cachedVersions[0], "0.0.0-20230203"); t.false(releasesApiMock.isDone()); }); }, ); function assertDurationsInteger( t: ExecutionContext<unknown>, statusReport: ToolsDownloadStatusReport, ) { t.assert(Number.isInteger(statusReport?.combinedDurationMs)); if (statusReport.downloadDurationMs !== undefined) { t.assert(Number.isInteger(statusReport?.downloadDurationMs)); t.assert(Number.isInteger(statusReport?.extractionDurationMs)); } } test.serial("getExtraOptions works for explicit paths", (t) => { t.deepEqual(codeql.getExtraOptions({}, ["foo"], []), []); t.deepEqual(codeql.getExtraOptions({ foo: [42] }, ["foo"], []), ["42"]); t.deepEqual( codeql.getExtraOptions({ foo: { bar: [42] } }, ["foo", "bar"], []), ["42"], ); }); test.serial("getExtraOptions works for wildcards", (t) => { t.deepEqual(codeql.getExtraOptions({ "*": [42] }, ["foo"], []), ["42"]); }); test.serial("getExtraOptions works for wildcards and explicit paths", (t) => { const o1 = { "*": [42], foo: [87] }; t.deepEqual(codeql.getExtraOptions(o1, ["foo"], []), ["42", "87"]); const o2 = { "*": [42], foo: [87] }; t.deepEqual(codeql.getExtraOptions(o2, ["foo", "bar"], []), ["42"]); const o3 = { "*": [42], foo: { "*": [87], bar: [99] } }; const p = ["foo", "bar"]; t.deepEqual(codeql.getExtraOptions(o3, p, []), ["42", "87", "99"]); }); test.serial("getExtraOptions throws for bad content", (t) => { t.throws(() => codeql.getExtraOptions({ "*": 42 }, ["foo"], [])); t.throws(() => codeql.getExtraOptions({ foo: 87 }, ["foo"], [])); t.throws(() => codeql.getExtraOptions( { "*": [42], foo: { "*": 87, bar: [99] } }, ["foo", "bar"], [], ), ); }); // Test macro for ensuring different variants of injected augmented configurations const injectedConfigMacro = test.macro({ exec: async ( t: ExecutionContext<unknown>, augmentationProperties: AugmentationProperties, configOverride: Partial<Config>, expectedConfig: any, ) => { await util.withTmpDir(async (tempDir) => { sinon.stub(actionsUtil, "isDefaultSetup").resolves(false); const runnerConstructorStub = stubToolRunnerConstructor(); const codeqlObject = await stubCodeql(); const thisStubConfig: Config = { ...stubConfig, ...configOverride, tempDir, }; thisStubConfig.computedConfig = generateCodeScanningConfig( getRunnerLogger(true), thisStubConfig.originalUserInput, augmentationProperties, ); await codeqlObject.databaseInitCluster( thisStubConfig, "", undefined, undefined, getRunnerLogger(true), ); const args = runnerConstructorStub.firstCall.args[1] as string[]; // should have used an config file const configArg = args.find((arg: string) => arg.startsWith("--codescanning-config="), ); t.truthy(configArg, "Should have injected a codescanning config"); const configFile = configArg!.split("=")[1]; const augmentedConfig = yaml.load(fs.readFileSync(configFile, "utf8")); t.deepEqual(augmentedConfig, expectedConfig); await fs.promises.rm(configFile, { force: true }); }); }, title: (providedTitle = "") => `databaseInitCluster() injected config: ${providedTitle}`, }); test.serial( "basic", injectedConfigMacro, { ...defaultAugmentationProperties, }, {}, {}, ); test.serial( "injected packs from input", injectedConfigMacro, { ...defaultAugmentationProperties, packsInput: ["xxx", "yyy"], }, {}, { packs: ["xxx", "yyy"], }, ); test.serial( "injected packs from input with existing packs combines", injectedConfigMacro, { ...defaultAugmentationProperties, packsInputCombines: true, packsInput: ["xxx", "yyy"], }, { originalUserInput: { packs: { cpp: ["codeql/something-else"], }, }, }, { packs: { cpp: ["codeql/something-else", "xxx", "yyy"], }, }, ); test.serial( "injected packs from input with existing packs overrides", injectedConfigMacro, { ...defaultAugmentationProperties, packsInput: ["xxx", "yyy"], }, { originalUserInput: { packs: { cpp: ["codeql/something-else"], }, }, }, { packs: ["xxx", "yyy"], }, ); // similar, but with queries test.serial( "injected queries from input", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInput: [{ uses: "xxx" }, { uses: "yyy" }], }, {}, { queries: [ { uses: "xxx", }, { uses: "yyy", }, ], }, ); test.serial( "injected queries from input overrides", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInput: [{ uses: "xxx" }, { uses: "yyy" }], }, { originalUserInput: { queries: [{ uses: "zzz" }], }, }, { queries: [ { uses: "xxx", }, { uses: "yyy", }, ], }, ); test.serial( "injected queries from input combines", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInputCombines: true, queriesInput: [{ uses: "xxx" }, { uses: "yyy" }], }, { originalUserInput: { queries: [{ uses: "zzz" }], }, }, { queries: [ { uses: "xxx", }, { uses: "yyy", }, { uses: "zzz", }, ], }, ); test.serial( "injected queries from input combines 2", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInputCombines: true, packsInputCombines: true, queriesInput: [{ uses: "xxx" }, { uses: "yyy" }], }, {}, { queries: [ { uses: "xxx", }, { uses: "yyy", }, ], }, ); test.serial( "injected queries and packs, but empty", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInputCombines: true, packsInputCombines: true, queriesInput: [], packsInput: [], }, { originalUserInput: { packs: [], queries: [], }, }, {}, ); test.serial( "repo property queries have the highest precedence", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInputCombines: true, queriesInput: [{ uses: "xxx" }, { uses: "yyy" }], repoPropertyQueries: { combines: false, input: [{ uses: "zzz" }, { uses: "aaa" }], }, }, { originalUserInput: { queries: [{ uses: "uu" }, { uses: "vv" }], }, }, { queries: [{ uses: "zzz" }, { uses: "aaa" }], }, ); test.serial( "repo property queries combines with queries input", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInputCombines: false, queriesInput: [{ uses: "xxx" }, { uses: "yyy" }], repoPropertyQueries: { combines: true, input: [{ uses: "zzz" }, { uses: "aaa" }], }, }, { originalUserInput: { queries: [{ uses: "uu" }, { uses: "vv" }], }, }, { queries: [ { uses: "zzz" }, { uses: "aaa" }, { uses: "xxx" }, { uses: "yyy" }, ], }, ); test.serial( "repo property queries combines everything else", injectedConfigMacro, { ...defaultAugmentationProperties, queriesInputCombines: true, queriesInput: [{ uses: "xxx" }, { uses: "yyy" }], repoPropertyQueries: { combines: true, input: [{ uses: "zzz" }, { uses: "aaa" }], }, }, { originalUserInput: { queries: [{ uses: "uu" }, { uses: "vv" }], }, }, { queries: [ { uses: "zzz" }, { uses: "aaa" }, { uses: "xxx" }, { uses: "yyy" }, { uses: "uu" }, { uses: "vv" }, ], }, ); test.serial( "passes a code scanning config AND qlconfig to the CLI", async (t: ExecutionContext<unknown>) => { await util.withTmpDir(async (tempDir) => { const runnerConstructorStub = stubToolRunnerConstructor(); const codeqlObject = await stubCodeql(); await codeqlObject.databaseInitCluster( { ...stubConfig, tempDir }, "", undefined, "/path/to/qlconfig.yml", getRunnerLogger(true), ); const args = runnerConstructorStub.firstCall.args[1] as string[]; // should have used a config file const hasCodeScanningConfigArg = args.some((arg: string) => arg.startsWith("--codescanning-config="), ); t.true(hasCodeScanningConfigArg, "Should have injected a qlconfig"); // should have passed a qlconfig file const hasQlconfigArg = args.some((arg: string) => arg.startsWith("--qlconfig-file="), ); t.truthy(hasQlconfigArg, "Should have injected a codescanning config"); }); }, ); test.serial( "does not pass a qlconfig to the CLI when it is undefined", async (t: ExecutionContext<unknown>) => { await util.withTmpDir(async (tempDir) => { const runnerConstructorStub = stubToolRunnerConstructor(); const codeqlObject = await stubCodeql(); await codeqlObject.databaseInitCluster( { ...stubConfig, tempDir }, "", undefined, undefined, // undefined qlconfigFile getRunnerLogger(true), ); const args = runnerConstructorStub.firstCall.args[1] as any[]; const hasQlconfigArg = args.some((arg: string) => arg.startsWith("--qlconfig-file="), ); t.false(hasQlconfigArg, "should NOT have injected a qlconfig"); }); }, ); test.serial("runTool summarizes several fatal errors", async (t) => { const heapError = "A fatal error occurred: Evaluator heap must be at least 384.00 MiB"; const datasetImportError = "A fatal error occurred: Dataset import for /home/runner/work/_temp/codeql_databases/javascript/db-javascript failed with code 2"; const cliStderr = `Running TRAP import for CodeQL database at /home/runner/work/_temp/codeql_databases/javascript...\n` + `${heapError}\n${datasetImportError}.`; stubToolRunnerConstructor(32, cliStderr); const codeqlObject = await stubCodeql(); // io throws because of the test CodeQL object. sinon.stub(io, "which").resolves(""); await t.throwsAsync( async () => await codeqlObject.finalizeDatabase( "db", "--threads=2", "--ram=2048", false, ), { instanceOf: util.ConfigurationError, message: new RegExp( 'Encountered a fatal error while running \\"codeql-for-testing database finalize --finalize-dataset --threads=2 --ram=2048 db\\"\\. ' + `Exit code was 32 and error was: ${datasetImportError.replaceAll( ".", "\\.", )}\\. Context: ${heapError.replaceAll( ".", "\\.", )}\\. See the logs for more details\\.`, ), }, ); }); test.serial("runTool summarizes autobuilder errors", async (t) => { const stderr = ` [2019-09-18 12:00:00] [autobuild] A non-error message [2019-09-18 12:00:00] Untagged message [2019-09-18 12:00:00] [autobuild] [ERROR] Start of the error message [2019-09-18 12:00:00] [autobuild] An interspersed non-error message [2019-09-18 12:00:01] [autobuild] [ERROR] Some more context about the error message [2019-09-18 12:00:01] [autobuild] [ERROR] continued [2019-09-18 12:00:01] [autobuild] [ERROR] and finished here. [2019-09-18 12:00:01] [autobuild] A non-error message `; stubToolRunnerConstructor(1, stderr); const codeqlObject = await codeql.getCodeQLForTesting(); sinon.stub(codeqlObject, "getVersion").resolves(makeVersionInfo("2.17.6")); sinon.stub(codeqlObject, "resolveExtractor").resolves("/path/to/extractor"); // io throws because of the test CodeQL object. sinon.stub(io, "which").resolves(""); await t.throwsAsync( async () => await codeqlObject.runAutobuild(stubConfig, KnownLanguage.java), { instanceOf: util.ConfigurationError, message: "We were unable to automatically build your code. Please provide manual build steps. " + `See ${DocUrl.AUTOMATIC_BUILD_FAILED} for more information. ` + "Encountered the following error: Start of the error message\n" + " Some more context about the error message\n" + " continued\n" + " and finished here.", }, ); }); test.serial("runTool truncates long autobuilder errors", async (t) => { const stderr = Array.from( { length: 20 }, (_, i) => `[2019-09-18 12:00:00] [autobuild] [ERROR] line${i + 1}`, ).join("\n"); stubToolRunnerConstructor(1, stderr); const codeqlObject = await stubCodeql(); sinon.stub(codeqlObject, "resolveExtractor").resolves("/path/to/extractor"); // io throws because of the test CodeQL object. sinon.stub(io, "which").resolves(""); await t.throwsAsync( async () => await codeqlObject.runAutobuild(stubConfig, KnownLanguage.java), { instanceOf: util.ConfigurationError, message: "We were unable to automatically build your code. Please provide manual build steps. " + `See ${DocUrl.AUTOMATIC_BUILD_FAILED} for more information. ` + "Encountered the following error: " + `${Array.from({ length: 10 }, (_, i) => `line${i + 1}`).join( "\n", )}\n(truncated)`, }, ); }); test.serial("runTool recognizes fatal internal errors", async (t) => { const stderr = ` |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |