@@ -58,6 +58,81 @@ function getSwiftToolchainBlock(): string {
5858 return script . slice ( start , end ) ;
5959}
6060
61+ function runSwiftToolchainHarness ( options : {
62+ swiftVersion : string ;
63+ selectedDeveloperDir : "command-line-tools" | "custom-xcode" | "invalid" | "xcode" ;
64+ developerDirOverride ?: "custom-xcode" | "invalid" | "xcode" ;
65+ } ) {
66+ const root = tempDirs . make ( "openclaw-package-swift-root-" ) ;
67+ const toolsDir = path . join ( root , "tools" ) ;
68+ const commandLineToolsDir = path . join ( root , "Library" , "Developer" , "CommandLineTools" ) ;
69+ const xcodeDeveloperDir = path . join ( root , "Applications" , "Xcode.app" , "Contents" , "Developer" ) ;
70+ const customXcodeDeveloperDir = path . join ( root , "MountedToolchains" , "CustomDeveloper" ) ;
71+ const invalidDeveloperDir = path . join ( root , "InvalidDeveloper" ) ;
72+ const developerDirs = {
73+ "command-line-tools" : commandLineToolsDir ,
74+ "custom-xcode" : customXcodeDeveloperDir ,
75+ invalid : invalidDeveloperDir ,
76+ xcode : xcodeDeveloperDir ,
77+ } as const ;
78+ const selectedDeveloperDir = developerDirs [ options . selectedDeveloperDir ] ;
79+
80+ mkdirSync ( toolsDir , { recursive : true } ) ;
81+ mkdirSync ( commandLineToolsDir , { recursive : true } ) ;
82+ for ( const developerDir of [ xcodeDeveloperDir , customXcodeDeveloperDir ] ) {
83+ const xcodebuild = path . join ( developerDir , "usr" , "bin" , "xcodebuild" ) ;
84+ mkdirSync ( path . dirname ( xcodebuild ) , { recursive : true } ) ;
85+ writeFileSync (
86+ xcodebuild ,
87+ [ "#!/usr/bin/env bash" , '[[ "$*" == "-version" ]] || exit 2' , "echo 'Xcode 26.0'" , "" ] . join (
88+ "\n" ,
89+ ) ,
90+ "utf8" ,
91+ ) ;
92+ chmodSync ( xcodebuild , 0o755 ) ;
93+ }
94+ writeFileSync (
95+ path . join ( toolsDir , "xcrun" ) ,
96+ [
97+ "#!/usr/bin/env bash" ,
98+ '[[ "${1:-}" == "xcodebuild" && "${2:-}" == "-version" ]] || exit 2' ,
99+ 'developer_dir="${DEVELOPER_DIR:-$MOCK_SELECTED_DEVELOPER_DIR}"' ,
100+ 'xcodebuild="$developer_dir/usr/bin/xcodebuild"' ,
101+ 'if [[ ! -x "$xcodebuild" ]]; then' ,
102+ ' echo "xcrun: error: unable to find utility xcodebuild" >&2' ,
103+ " exit 1" ,
104+ "fi" ,
105+ 'exec "$xcodebuild" "${@:2}"' ,
106+ "" ,
107+ ] . join ( "\n" ) ,
108+ "utf8" ,
109+ ) ;
110+ writeFileSync (
111+ path . join ( toolsDir , "swift" ) ,
112+ [
113+ "#!/usr/bin/env bash" ,
114+ `echo 'swift-driver version: 1.120.0 Apple Swift version ${ options . swiftVersion } (swiftlang-${ options . swiftVersion } clang-1700.0.13.5)'` ,
115+ "" ,
116+ ] . join ( "\n" ) ,
117+ "utf8" ,
118+ ) ;
119+ for ( const tool of [ "xcrun" , "swift" ] ) {
120+ chmodSync ( path . join ( toolsDir , tool ) , 0o755 ) ;
121+ }
122+
123+ const developerDirOverride = options . developerDirOverride
124+ ? `export DEVELOPER_DIR=${ JSON . stringify ( developerDirs [ options . developerDirOverride ] ) } `
125+ : "unset DEVELOPER_DIR" ;
126+ return runHelper ( `
127+ set -euo pipefail
128+ PATH=${ JSON . stringify ( `${ toolsDir } :/usr/bin:/bin` ) }
129+ export MOCK_SELECTED_DEVELOPER_DIR=${ JSON . stringify ( selectedDeveloperDir ) }
130+ ${ developerDirOverride }
131+ ${ getSwiftToolchainBlock ( ) }
132+ require_swift_toolchain
133+ ` ) ;
134+ }
135+
61136function getSparkleBuildHelperBlock ( ) : string {
62137 const script = readFileSync ( scriptPath , "utf8" ) ;
63138 const start = script . indexOf ( "sparkle_canonical_build_from_version()" ) ;
@@ -734,60 +809,74 @@ describe("package-mac-app plist stamping", () => {
734809 } ) ;
735810
736811 it ( "fails with an actionable error when Swift tools are too old" , ( ) => {
737- const helperBlock = getSwiftToolchainBlock ( ) ;
738- const toolsDir = tempDirs . make ( "openclaw-package-swift-tools-" ) ;
739-
740- const swiftPath = path . join ( toolsDir , "swift" ) ;
741- writeFileSync (
742- swiftPath ,
743- [
744- "#!/usr/bin/env bash" ,
745- "echo 'swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)'" ,
746- "" ,
747- ] . join ( "\n" ) ,
748- "utf8" ,
749- ) ;
750- chmodSync ( swiftPath , 0o755 ) ;
751-
752- const result = runHelper ( `
753- set -euo pipefail
754- PATH=${ JSON . stringify ( `${ toolsDir } :/usr/bin:/bin` ) }
755- ${ helperBlock }
756- require_swift_toolchain
757- ` ) ;
812+ const result = runSwiftToolchainHarness ( {
813+ swiftVersion : "6.0.3" ,
814+ selectedDeveloperDir : "xcode" ,
815+ } ) ;
758816
759817 expect ( result . status ) . toBe ( 1 ) ;
760818 expect ( result . stderr ) . toContain ( "OpenClaw macOS app packaging requires Swift tools 6.2+" ) ;
761819 expect ( result . stderr ) . toContain ( "Current Swift is 6.0" ) ;
762820 } ) ;
763821
764- it ( "accepts Swift tools 6.2 or newer" , ( ) => {
765- const helperBlock = getSwiftToolchainBlock ( ) ;
766- const toolsDir = tempDirs . make ( "openclaw-package-swift-tools-" ) ;
822+ it ( "rejects Command Line Tools even when they provide Swift 6.2" , ( ) => {
823+ const result = runSwiftToolchainHarness ( {
824+ swiftVersion : "6.2.1" ,
825+ selectedDeveloperDir : "command-line-tools" ,
826+ } ) ;
767827
768- const swiftPath = path . join ( toolsDir , "swift" ) ;
769- writeFileSync (
770- swiftPath ,
771- [
772- "#!/usr/bin/env bash" ,
773- "echo 'swift-driver version: 1.120.0 Apple Swift version 6.2.1 (swiftlang-6.2.1 clang-1700.0.13.5)'" ,
774- "" ,
775- ] . join ( "\n" ) ,
776- "utf8" ,
828+ expect ( result . status ) . toBe ( 1 ) ;
829+ expect ( result . stderr ) . toContain ( "requires a full Xcode developer directory" ) ;
830+ expect ( result . stderr ) . toContain (
831+ "Command Line Tools do not include the required SwiftUI macro plugins" ,
777832 ) ;
778- chmodSync ( swiftPath , 0o755 ) ;
833+ expect ( result . stderr ) . toContain (
834+ "sudo xcode-select -s /Applications/Xcode.app/Contents/Developer" ,
835+ ) ;
836+ expect ( result . stderr ) . toContain ( "DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer" ) ;
837+ } ) ;
779838
780- const result = runHelper ( `
781- set -euo pipefail
782- PATH=${ JSON . stringify ( `${ toolsDir } :/usr/bin:/bin` ) }
783- ${ helperBlock }
784- require_swift_toolchain
785- ` ) ;
839+ it ( "accepts Swift 6.2 from a selected full Xcode developer directory" , ( ) => {
840+ const result = runSwiftToolchainHarness ( {
841+ swiftVersion : "6.2.1" ,
842+ selectedDeveloperDir : "xcode" ,
843+ } ) ;
786844
787845 expect ( result . status ) . toBe ( 0 ) ;
788846 expect ( result . stderr ) . toBe ( "" ) ;
789847 } ) ;
790848
849+ it ( "honors DEVELOPER_DIR when the global selection is Command Line Tools" , ( ) => {
850+ const result = runSwiftToolchainHarness ( {
851+ swiftVersion : "6.2.1" ,
852+ selectedDeveloperDir : "command-line-tools" ,
853+ developerDirOverride : "xcode" ,
854+ } ) ;
855+
856+ expect ( result . status ) . toBe ( 0 ) ;
857+ expect ( result . stderr ) . toBe ( "" ) ;
858+ } ) ;
859+
860+ it ( "accepts usable full Xcode tooling from a custom developer directory" , ( ) => {
861+ const result = runSwiftToolchainHarness ( {
862+ swiftVersion : "6.2.1" ,
863+ selectedDeveloperDir : "custom-xcode" ,
864+ } ) ;
865+
866+ expect ( result . status ) . toBe ( 0 ) ;
867+ expect ( result . stderr ) . toBe ( "" ) ;
868+ } ) ;
869+
870+ it ( "rejects an unusable selected developer directory" , ( ) => {
871+ const result = runSwiftToolchainHarness ( {
872+ swiftVersion : "6.2.1" ,
873+ selectedDeveloperDir : "invalid" ,
874+ } ) ;
875+
876+ expect ( result . status ) . toBe ( 1 ) ;
877+ expect ( result . stderr ) . toContain ( "requires a full Xcode developer directory" ) ;
878+ } ) ;
879+
791880 it ( "runs Sparkle build metadata derivation from the repository root" , ( ) => {
792881 const helperBlock = getSparkleBuildHelperBlock ( ) ;
793882 const tempRoot = tempDirs . make ( "openclaw-package-sparkle-root-" ) ;
0 commit comments