Skip to content

Commit ad93ecc

Browse files
feat: ACI-4284 add --cache-skip-flags (#194)
1 parent 84c8375 commit ad93ecc

File tree

5 files changed

+135
-8
lines changed

5 files changed

+135
-8
lines changed

cmd/xcode/activate_xcode.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ Useful if there are multiple Xcode versions installed and you want to use a spec
127127
"timestamps",
128128
activateXcodeParams.XcodebuildTimestampsEnabled,
129129
"Enable xcodebuild timestamps. This will add timestamps to the xcodebuild output.")
130+
activateXcodeCmd.Flags().BoolVar(&activateXcodeParams.BuildCacheSkipFlags,
131+
"cache-skip-flags",
132+
activateXcodeParams.BuildCacheSkipFlags,
133+
`Skip passing cache flags to xcodebuild except the COMPILATION_CACHE_REMOTE_SERVICE_PATH.
134+
Cache will have to be enabled manually in the Xcode project settings.`)
130135
}
131136

132137
func ActivateXcodeCommandFn(

cmd/xcode/xcodebuild.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,18 @@ func getArgsToPass(config xcelerate.Config, xcodeArgs xcodeargs.XcodeArgs) []str
332332
additional := map[string]string{}
333333

334334
if config.BuildCacheEnabled {
335-
additional = maps.Clone(xcodeargs.CacheArgs)
336-
diagnosticRemarks := "NO"
337-
if config.DebugLogging {
338-
diagnosticRemarks = "YES"
335+
additional["COMPILATION_CACHE_REMOTE_SERVICE_PATH"] = config.ProxySocketPath
336+
337+
if !config.BuildCacheSkipFlags {
338+
maps.Copy(additional, xcodeargs.CacheArgs)
339+
diagnosticRemarks := "NO"
340+
if config.DebugLogging {
341+
diagnosticRemarks = "YES"
342+
}
343+
maps.Copy(additional, map[string]string{
344+
"COMPILATION_CACHE_ENABLE_DIAGNOSTIC_REMARKS": diagnosticRemarks,
345+
})
339346
}
340-
maps.Copy(additional, map[string]string{
341-
"COMPILATION_CACHE_REMOTE_SERVICE_PATH": config.ProxySocketPath,
342-
"COMPILATION_CACHE_ENABLE_DIAGNOSTIC_REMARKS": diagnosticRemarks,
343-
})
344347
}
345348

346349
return xcodeArgs.Args(additional)

cmd/xcode/xcodebuild_test.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,117 @@ func Test_xcodebuildCmdFn(t *testing.T) {
126126
require.EqualError(t, actual.Error, expected.Error())
127127
require.Equal(t, 55, actual.ExitCode)
128128
})
129+
130+
t.Run("xcodebuildCmdFn adds cache flags only if build cache is enabled and skip flag is not enabled", func(t *testing.T) {
131+
testCases := []struct {
132+
name string
133+
buildCacheEnabled bool
134+
buildCacheSkipFlags bool
135+
expectCacheFlags bool
136+
}{
137+
{"enabled & not skipped", true, false, true},
138+
{"enabled & skipped", true, true, false},
139+
{"disabled & not skipped", false, false, false},
140+
{"disabled & skipped", false, true, false},
141+
}
142+
143+
for _, tc := range testCases {
144+
t.Run(tc.name, func(t *testing.T) {
145+
var receivedAdditional map[string]string
146+
xcodeArgProvider := xcodeargsMocks.XcodeArgsMock{
147+
ArgsFunc: func(additional map[string]string) []string {
148+
receivedAdditional = additional
149+
150+
return []string{"xcodebuild"}
151+
},
152+
CommandFunc: func() string { return "xcodebuild" },
153+
ShortCommandFunc: func() string { return "xcodebuild" },
154+
}
155+
156+
xcodeRunner := &mocks.XcodeRunnerMock{
157+
RunFunc: func(_ context.Context, args []string) xcodeargs.RunStats { return xcodeargs.RunStats{} },
158+
}
159+
160+
sessionClientMock := &mocks.SessionClientMock{}
161+
162+
SUT := xcode.XcodebuildCmdFn
163+
config := xcelerate.Config{
164+
BuildCacheEnabled: tc.buildCacheEnabled,
165+
BuildCacheSkipFlags: tc.buildCacheSkipFlags,
166+
ProxySocketPath: "/tmp/proxy.sock",
167+
}
168+
169+
_ = SUT(
170+
context.Background(),
171+
uuid.NewString(),
172+
mockLogger,
173+
xcodeRunner,
174+
sessionClientMock,
175+
config,
176+
common.CacheConfigMetadata{},
177+
&xcodeArgProvider,
178+
)
179+
180+
for k, v := range xcodeargs.CacheArgs {
181+
if tc.expectCacheFlags {
182+
assert.Equal(t, v, receivedAdditional[k], "Expected cache flag %s to be present", k)
183+
} else {
184+
assert.NotContains(t, receivedAdditional, k, "Did not expect cache flag %s", k)
185+
}
186+
}
187+
if tc.buildCacheEnabled {
188+
assert.Equal(t, "/tmp/proxy.sock", receivedAdditional["COMPILATION_CACHE_REMOTE_SERVICE_PATH"], "Proxy path should be set when build cache is enabled")
189+
} else {
190+
assert.NotContains(t, receivedAdditional, "COMPILATION_CACHE_REMOTE_SERVICE_PATH", "Proxy path should not be set when build cache is disabled")
191+
}
192+
})
193+
}
194+
})
195+
196+
t.Run("xcodebuildCmdFn sets diagnostic remarks only if debug flag is set", func(t *testing.T) {
197+
cases := []struct {
198+
name string
199+
debugLogging bool
200+
expected string
201+
}{
202+
{"debug enabled", true, "YES"},
203+
{"debug disabled", false, "NO"},
204+
}
205+
for _, tc := range cases {
206+
t.Run(tc.name, func(t *testing.T) {
207+
var receivedAdditional map[string]string
208+
xcodeArgProvider := xcodeargsMocks.XcodeArgsMock{
209+
ArgsFunc: func(additional map[string]string) []string {
210+
receivedAdditional = additional
211+
212+
return []string{"xcodebuild"}
213+
},
214+
CommandFunc: func() string { return "xcodebuild" },
215+
ShortCommandFunc: func() string { return "xcodebuild" },
216+
}
217+
xcodeRunner := &mocks.XcodeRunnerMock{
218+
RunFunc: func(_ context.Context, args []string) xcodeargs.RunStats { return xcodeargs.RunStats{} },
219+
}
220+
sessionClientMock := &mocks.SessionClientMock{}
221+
SUT := xcode.XcodebuildCmdFn
222+
config := xcelerate.Config{
223+
BuildCacheEnabled: true,
224+
BuildCacheSkipFlags: false,
225+
DebugLogging: tc.debugLogging,
226+
ProxySocketPath: "/tmp/proxy.sock",
227+
}
228+
_ = SUT(
229+
context.Background(),
230+
uuid.NewString(),
231+
mockLogger,
232+
xcodeRunner,
233+
sessionClientMock,
234+
config,
235+
common.CacheConfigMetadata{},
236+
&xcodeArgProvider,
237+
)
238+
assert.Equal(t, tc.expected, receivedAdditional["COMPILATION_CACHE_ENABLE_DIAGNOSTIC_REMARKS"], "Diagnostic remarks should be %s if debug is %v", tc.expected, tc.debugLogging)
239+
})
240+
}
241+
})
129242
}

internal/config/xcelerate/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
type Params struct {
2828
BuildCacheEnabled bool
2929
BuildCacheEndpoint string
30+
BuildCacheSkipFlags bool
3031
DebugLogging bool
3132
Silent bool
3233
XcodePathOverride string
@@ -44,6 +45,7 @@ type Config struct {
4445
OriginalXcodebuildPath string `json:"originalXcodebuildPath"`
4546
OriginalXcrunPath string `json:"originalXcrunPath"`
4647
BuildCacheEnabled bool `json:"buildCacheEnabled"`
48+
BuildCacheSkipFlags bool `json:"buildCacheSkipFlags"`
4749
BuildCacheEndpoint string `json:"buildCacheEndpoint"`
4850
PushEnabled bool `json:"pushEnabled"`
4951
DebugLogging bool `json:"debugLogging,omitempty"`
@@ -73,6 +75,7 @@ func ReadConfig(osProxy utils.OsProxy, decoderFactory utils.DecoderFactory) (Con
7375
func DefaultParams() Params {
7476
return Params{
7577
BuildCacheEnabled: true,
78+
BuildCacheSkipFlags: false,
7679
BuildCacheEndpoint: "",
7780
Silent: false,
7881
DebugLogging: false,
@@ -156,6 +159,7 @@ func NewConfig(ctx context.Context,
156159
OriginalXcodebuildPath: xcodePath,
157160
OriginalXcrunPath: xcrunPath,
158161
BuildCacheEnabled: params.BuildCacheEnabled,
162+
BuildCacheSkipFlags: params.BuildCacheSkipFlags,
159163
BuildCacheEndpoint: params.BuildCacheEndpoint,
160164
PushEnabled: params.PushEnabled,
161165
DebugLogging: params.DebugLogging,

internal/config/xcelerate/config_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ func TestConfig_NewConfig(t *testing.T) {
206206

207207
actual, err := xcelerate.NewConfig(context.Background(), mockLogger, xcelerate.Params{
208208
BuildCacheEnabled: true,
209+
BuildCacheSkipFlags: true,
209210
DebugLogging: true,
210211
XcodebuildTimestampsEnabled: true,
211212
}, envs, osProxyMock, func(_ context.Context, command string, args ...string) utils.Command {
@@ -228,6 +229,7 @@ func TestConfig_NewConfig(t *testing.T) {
228229
OriginalXcrunPath: "/usr/bin/xcodebuild2",
229230
BuildCacheEndpoint: "grpcs://bitrise-accelerate.services.bitrise.io",
230231
BuildCacheEnabled: true,
232+
BuildCacheSkipFlags: true,
231233
DebugLogging: true,
232234
XcodebuildTimestamps: true,
233235
AuthConfig: common.CacheAuthConfig{

0 commit comments

Comments
 (0)