|
1 | 1 | import fs from "fs"; |
2 | 2 |
|
| 3 | +import type { ExecOptions } from "@actions/exec"; |
3 | 4 | import * as toolrunner from "@actions/exec/lib/toolrunner"; |
4 | 5 | import * as io from "@actions/io"; |
5 | 6 | import test from "ava"; |
@@ -546,6 +547,104 @@ test.serial( |
546 | 547 | }), |
547 | 548 | ); |
548 | 549 |
|
| 550 | +// `addDiagnostic` changes global state and we must stub it, so this test must be serial. |
| 551 | +test.serial( |
| 552 | + "checkRunnerSupportsTracedAnalysis logs and adds diagnostic if Xcode SDK version is unsupported", |
| 553 | + async (t) => |
| 554 | + withTmpDir(async (tmpDir) => { |
| 555 | + const logger = new RecordingLogger(); |
| 556 | + const env = getTestEnv(); |
| 557 | + env.set(ActionsEnvVars.RUNNER_TEMP, tmpDir); |
| 558 | + const features = createFeatures([Feature.CheckUnsupportedTracedAnalysis]); |
| 559 | + |
| 560 | + const readlinkSync = sinon |
| 561 | + .stub(fs, "readlinkSync") |
| 562 | + .throws(new Error("Not found")); |
| 563 | + const which = sinon.stub(io, "which").resolves("/path/to/xcrun"); |
| 564 | + |
| 565 | + let stdoutListener: ((data: Buffer) => void) | undefined = undefined; |
| 566 | + const exec = sinon.stub<[], Promise<number>>().callsFake(async () => { |
| 567 | + if (stdoutListener !== undefined) { |
| 568 | + stdoutListener(Buffer.from("Xcode 27.5")); |
| 569 | + stdoutListener(Buffer.from("Build version 27F9")); |
| 570 | + return 0; |
| 571 | + } else { |
| 572 | + throw new Error("Error to force next fallback"); |
| 573 | + } |
| 574 | + }); |
| 575 | + const runner = sinon.createStubInstance(toolrunner.ToolRunner, { |
| 576 | + exec, |
| 577 | + }); |
| 578 | + const toolrunnerStub = sinon |
| 579 | + .stub(toolrunner, "ToolRunner") |
| 580 | + .callsFake((_cmd, args: string[], opts: ExecOptions) => { |
| 581 | + if (args[0] !== "xcodebuild") { |
| 582 | + stdoutListener = opts.listeners?.stdout; |
| 583 | + } |
| 584 | + return runner; |
| 585 | + }); |
| 586 | + |
| 587 | + const codeql = await getCodeQLWithTracedStub( |
| 588 | + "codeql-for-testing", |
| 589 | + logger, |
| 590 | + env, |
| 591 | + ); |
| 592 | + sinon.stub(codeql, "getVersion").resolves(makeVersionInfo("2.27.0")); |
| 593 | + |
| 594 | + const addDiagnostic = sinon.stub(diagnostics, "addDiagnostic"); |
| 595 | + |
| 596 | + const config = createTestConfig({ |
| 597 | + tempDir: tmpDir, |
| 598 | + languages: [BuiltInLanguage.swift], |
| 599 | + }); |
| 600 | + await checkRunnerSupportsTracedAnalysis( |
| 601 | + initAllState({ |
| 602 | + logger, |
| 603 | + platform: "darwin", |
| 604 | + osRelease: "26.0.0", |
| 605 | + env, |
| 606 | + features, |
| 607 | + }), |
| 608 | + config, |
| 609 | + codeql, |
| 610 | + ); |
| 611 | + |
| 612 | + t.is(readlinkSync.callCount, 1); |
| 613 | + t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]); |
| 614 | + |
| 615 | + t.is(which.callCount, 2); |
| 616 | + t.is(toolrunnerStub.callCount, 2); |
| 617 | + t.is(exec.callCount, 2); |
| 618 | + |
| 619 | + checkExpectedLogMessages(t, logger.messages, [ |
| 620 | + "Unable to determine Xcode version from xcodebuild", |
| 621 | + "Traced analysis is not supported on Xcode SDK 27", |
| 622 | + ]); |
| 623 | + |
| 624 | + t.is(addDiagnostic.callCount, 1); |
| 625 | + t.like(addDiagnostic.args[0], [ |
| 626 | + config, |
| 627 | + BuiltInLanguage.swift, |
| 628 | + { |
| 629 | + attributes: { |
| 630 | + languages: [BuiltInLanguage.swift], |
| 631 | + xcodeSdkVersion: "27.5.0", |
| 632 | + }, |
| 633 | + severity: "warning", |
| 634 | + source: { |
| 635 | + id: "codeql-action/unsupported-traced-analysis-xcode-sdk", |
| 636 | + name: "Traced analysis is not supported on this version of Xcode SDK", |
| 637 | + }, |
| 638 | + visibility: { |
| 639 | + cliSummaryTable: true, |
| 640 | + statusPage: true, |
| 641 | + telemetry: true, |
| 642 | + }, |
| 643 | + } satisfies Partial<diagnostics.DiagnosticMessage>, |
| 644 | + ]); |
| 645 | + }), |
| 646 | +); |
| 647 | + |
549 | 648 | // `addDiagnostic` changes global state and we must stub it, so this test must be serial. |
550 | 649 | test.serial( |
551 | 650 | "checkRunnerSupportsTracedAnalysis throws if Xcode version is unsupported and FF is enabled", |
|
0 commit comments