11import fs from "fs" ;
22
3+ import type { ExecOptions } from "@actions/exec" ;
4+ import * as toolrunner from "@actions/exec/lib/toolrunner" ;
5+ import * as io from "@actions/io" ;
36import test from "ava" ;
47import * as sinon from "sinon" ;
58
@@ -344,41 +347,58 @@ test.serial(
344347 } ) ,
345348) ;
346349
347- test ( "checkRunnerSupportsTracedAnalysis doesn't perform Xcode check on self-hosted runners" , async ( t ) =>
348- withTmpDir ( async ( tmpDir ) => {
349- const logger = new RecordingLogger ( ) ;
350- const env = getTestEnv ( ) ;
351- env . set ( ActionsEnvVars . RUNNER_TEMP , tmpDir ) ;
352- env . set ( ActionsEnvVars . RUNNER_ENVIRONMENT , "self-hosted" ) ;
353- const features = createFeatures ( [ Feature . CheckUnsupportedTracedAnalysis ] ) ;
350+ // `addDiagnostic` changes global state and we must stub it, so this test must be serial.
351+ test . serial (
352+ "checkRunnerSupportsTracedAnalysis doesn't add a diagnostic if Xcode version is supported" ,
353+ async ( t ) =>
354+ withTmpDir ( async ( tmpDir ) => {
355+ const logger = new RecordingLogger ( ) ;
356+ const env = getTestEnv ( ) ;
357+ env . set ( ActionsEnvVars . RUNNER_TEMP , tmpDir ) ;
358+ const features = createFeatures ( [ Feature . CheckUnsupportedTracedAnalysis ] ) ;
354359
355- const codeql = await getCodeQLWithTracedStub (
356- "codeql-for-testing" ,
357- logger ,
358- env ,
359- ) ;
360- sinon . stub ( codeql , "getVersion" ) . resolves ( makeVersionInfo ( "2.27.0" ) ) ;
360+ const readlinkSync = sinon
361+ . stub ( fs , "readlinkSync" )
362+ . returns ( "/Applications/Xcode_26.0.app/Contents/Developer" ) ;
361363
362- await checkRunnerSupportsTracedAnalysis (
363- initAllState ( {
364+ const codeql = await getCodeQLWithTracedStub (
365+ "codeql-for-testing" ,
364366 logger ,
365- platform : "darwin" ,
366- osRelease : "26.0.0" ,
367367 env ,
368- features,
369- } ) ,
370- createTestConfig ( { tempDir : tmpDir , languages : [ BuiltInLanguage . go ] } ) ,
371- codeql ,
372- ) ;
368+ ) ;
369+ sinon . stub ( codeql , "getVersion" ) . resolves ( makeVersionInfo ( "2.27.0" ) ) ;
373370
374- checkExpectedLogMessages ( t , logger . messages , [
375- "Skipping traced analysis compatibility check for Xcode because we are on a self-hosted runner." ,
376- ] ) ;
377- } ) ) ;
371+ const addDiagnostic = sinon . stub ( diagnostics , "addDiagnostic" ) ;
372+
373+ const config = createTestConfig ( {
374+ tempDir : tmpDir ,
375+ languages : [ BuiltInLanguage . swift ] ,
376+ } ) ;
377+ await checkRunnerSupportsTracedAnalysis (
378+ initAllState ( {
379+ logger,
380+ platform : "darwin" ,
381+ osRelease : "26.0.0" ,
382+ env,
383+ features,
384+ } ) ,
385+ config ,
386+ codeql ,
387+ ) ;
388+
389+ t . is ( readlinkSync . callCount , 1 ) ;
390+ t . deepEqual ( readlinkSync . args [ 0 ] , [ XCODE_SELECT_LINK_PATH ] ) ;
391+
392+ checkUnexpectedLogMessages ( t , logger . messages , [
393+ "Traced analysis is not supported on Xcode 27" ,
394+ ] ) ;
395+
396+ t . is ( addDiagnostic . callCount , 0 ) ;
397+ } ) ,
398+ ) ;
378399
379- // `addDiagnostic` changes global state and we must stub it, so this test must be serial.
380400test . serial (
381- "checkRunnerSupportsTracedAnalysis doesn't add a diagnostic if Xcode version is supported " ,
401+ "checkRunnerSupportsTracedAnalysis attempts fallbacks if needed " ,
382402 async ( t ) =>
383403 withTmpDir ( async ( tmpDir ) => {
384404 const logger = new RecordingLogger ( ) ;
@@ -388,7 +408,17 @@ test.serial(
388408
389409 const readlinkSync = sinon
390410 . stub ( fs , "readlinkSync" )
391- . returns ( "/Applications/Xcode_26.0.app/Contents/Developer" ) ;
411+ . throws ( new Error ( "Not found" ) ) ;
412+ const which = sinon . stub ( io , "which" ) . resolves ( "/path/to/xcrun" ) ;
413+ const exec = sinon . stub < [ ] , Promise < number > > ( ) . callsFake ( async ( ) => {
414+ throw new Error ( "Error to force next fallback" ) ;
415+ } ) ;
416+ const runner = sinon . createStubInstance ( toolrunner . ToolRunner , { exec } ) ;
417+ const toolrunnerStub = sinon
418+ . stub ( toolrunner , "ToolRunner" )
419+ . callsFake ( ( ) => {
420+ return runner ;
421+ } ) ;
392422
393423 const codeql = await getCodeQLWithTracedStub (
394424 "codeql-for-testing" ,
@@ -417,7 +447,28 @@ test.serial(
417447
418448 t . is ( readlinkSync . callCount , 1 ) ;
419449 t . deepEqual ( readlinkSync . args [ 0 ] , [ XCODE_SELECT_LINK_PATH ] ) ;
450+ t . is ( which . callCount , 2 ) ;
451+ t . deepEqual ( which . args [ 0 ] , [ "xcrun" , true ] ) ;
452+ t . deepEqual ( which . args [ 1 ] , [ "xcrun" , true ] ) ;
453+
454+ t . is ( toolrunnerStub . callCount , 2 ) ;
455+ t . is ( exec . callCount , 2 ) ;
456+
457+ t . is ( toolrunnerStub . args [ 0 ] [ 0 ] , "/path/to/xcrun" ) ;
458+ t . deepEqual ( toolrunnerStub . args [ 0 ] [ 1 ] , [ "xcodebuild" , "-version" ] ) ;
420459
460+ t . is ( toolrunnerStub . args [ 1 ] [ 0 ] , "/path/to/xcrun" ) ;
461+ t . deepEqual ( toolrunnerStub . args [ 1 ] [ 1 ] , [
462+ "--sdk" ,
463+ "macosx" ,
464+ "--show-sdk-version" ,
465+ ] ) ;
466+
467+ checkExpectedLogMessages ( t , logger . messages , [
468+ "Unable to determine Xcode version: Not found" ,
469+ "Unable to determine Xcode version from xcodebuild: Error to force next fallback" ,
470+ "Unable to determine Xcode SDK version: Error to force next fallback" ,
471+ ] ) ;
421472 checkUnexpectedLogMessages ( t , logger . messages , [
422473 "Traced analysis is not supported on Xcode 27" ,
423474 ] ) ;
@@ -496,6 +547,103 @@ test.serial(
496547 } ) ,
497548) ;
498549
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 ( "27.5" ) ) ;
569+ return 0 ;
570+ } else {
571+ throw new Error ( "Error to force next fallback" ) ;
572+ }
573+ } ) ;
574+ const runner = sinon . createStubInstance ( toolrunner . ToolRunner , {
575+ exec,
576+ } ) ;
577+ const toolrunnerStub = sinon
578+ . stub ( toolrunner , "ToolRunner" )
579+ . callsFake ( ( _cmd , args : string [ ] , opts : ExecOptions ) => {
580+ if ( args [ 0 ] !== "xcodebuild" ) {
581+ stdoutListener = opts . listeners ?. stdout ;
582+ }
583+ return runner ;
584+ } ) ;
585+
586+ const codeql = await getCodeQLWithTracedStub (
587+ "codeql-for-testing" ,
588+ logger ,
589+ env ,
590+ ) ;
591+ sinon . stub ( codeql , "getVersion" ) . resolves ( makeVersionInfo ( "2.27.0" ) ) ;
592+
593+ const addDiagnostic = sinon . stub ( diagnostics , "addDiagnostic" ) ;
594+
595+ const config = createTestConfig ( {
596+ tempDir : tmpDir ,
597+ languages : [ BuiltInLanguage . swift ] ,
598+ } ) ;
599+ await checkRunnerSupportsTracedAnalysis (
600+ initAllState ( {
601+ logger,
602+ platform : "darwin" ,
603+ osRelease : "26.0.0" ,
604+ env,
605+ features,
606+ } ) ,
607+ config ,
608+ codeql ,
609+ ) ;
610+
611+ t . is ( readlinkSync . callCount , 1 ) ;
612+ t . deepEqual ( readlinkSync . args [ 0 ] , [ XCODE_SELECT_LINK_PATH ] ) ;
613+
614+ t . is ( which . callCount , 2 ) ;
615+ t . is ( toolrunnerStub . callCount , 2 ) ;
616+ t . is ( exec . callCount , 2 ) ;
617+
618+ checkExpectedLogMessages ( t , logger . messages , [
619+ "Unable to determine Xcode version from xcodebuild" ,
620+ "Traced analysis is not supported on Xcode SDK 27" ,
621+ ] ) ;
622+
623+ t . is ( addDiagnostic . callCount , 1 ) ;
624+ t . like ( addDiagnostic . args [ 0 ] , [
625+ config ,
626+ BuiltInLanguage . swift ,
627+ {
628+ attributes : {
629+ languages : [ BuiltInLanguage . swift ] ,
630+ xcodeSdkVersion : "27.5.0" ,
631+ } ,
632+ severity : "warning" ,
633+ source : {
634+ id : "codeql-action/unsupported-traced-analysis-xcode-sdk" ,
635+ name : "Traced analysis is not supported on this version of Xcode SDK" ,
636+ } ,
637+ visibility : {
638+ cliSummaryTable : true ,
639+ statusPage : true ,
640+ telemetry : true ,
641+ } ,
642+ } satisfies Partial < diagnostics . DiagnosticMessage > ,
643+ ] ) ;
644+ } ) ,
645+ ) ;
646+
499647// `addDiagnostic` changes global state and we must stub it, so this test must be serial.
500648test . serial (
501649 "checkRunnerSupportsTracedAnalysis throws if Xcode version is unsupported and FF is enabled" ,
0 commit comments