44package crosscompile
55
66import (
7+ "errors"
78 "os"
89 "path/filepath"
910 "runtime"
@@ -21,6 +22,69 @@ const (
2122 libPrefix = "-L"
2223)
2324
25+ func resetMacOSSysrootForTest (t * testing.T ) {
26+ t .Helper ()
27+ macOSSysrootMu .Lock ()
28+ oldCached := macOSSysrootCached
29+ oldLookup := macOSSysrootLookup
30+ macOSSysrootCached = ""
31+ macOSSysrootMu .Unlock ()
32+ t .Cleanup (func () {
33+ macOSSysrootMu .Lock ()
34+ macOSSysrootCached = oldCached
35+ macOSSysrootLookup = oldLookup
36+ macOSSysrootMu .Unlock ()
37+ })
38+ }
39+
40+ func TestGetMacOSSysrootCachesSuccess (t * testing.T ) {
41+ resetMacOSSysrootForTest (t )
42+ calls := 0
43+ macOSSysrootLookup = func () (string , error ) {
44+ calls ++
45+ return "/test/sdk" , nil
46+ }
47+ first , err := getMacOSSysroot ()
48+ if err != nil {
49+ t .Fatalf ("first getMacOSSysroot: %v" , err )
50+ }
51+ second , err := getMacOSSysroot ()
52+ if err != nil {
53+ t .Fatalf ("second getMacOSSysroot: %v" , err )
54+ }
55+ if first != "/test/sdk" || second != "/test/sdk" {
56+ t .Fatalf ("sysroots = %q, %q; want cached /test/sdk" , first , second )
57+ }
58+ if calls != 1 {
59+ t .Fatalf ("lookup calls = %d, want 1" , calls )
60+ }
61+ }
62+
63+ func TestGetMacOSSysrootDoesNotCacheErrors (t * testing.T ) {
64+ resetMacOSSysrootForTest (t )
65+ calls := 0
66+ macOSSysrootLookup = func () (string , error ) {
67+ calls ++
68+ if calls == 1 {
69+ return "" , errors .New ("temporary xcrun failure" )
70+ }
71+ return "/test/sdk" , nil
72+ }
73+ if _ , err := getMacOSSysroot (); err == nil {
74+ t .Fatal ("first getMacOSSysroot succeeded, want error" )
75+ }
76+ got , err := getMacOSSysroot ()
77+ if err != nil {
78+ t .Fatalf ("second getMacOSSysroot: %v" , err )
79+ }
80+ if got != "/test/sdk" {
81+ t .Fatalf ("sysroot = %q, want /test/sdk" , got )
82+ }
83+ if calls != 2 {
84+ t .Fatalf ("lookup calls = %d, want retry after error" , calls )
85+ }
86+ }
87+
2488func TestUseCrossCompileSDK (t * testing.T ) {
2589 // Skip long-running tests unless explicitly enabled
2690 if testing .Short () {
0 commit comments