From b2f3f849951d31f8599be58e92a1fbd822ff279c Mon Sep 17 00:00:00 2001 From: LaszloP <7979773+lpusok@users.noreply.github.com> Date: Wed, 26 Mar 2025 15:15:15 +0100 Subject: [PATCH 1/4] Added CommandArgs function for the xcodebuild CommandBuilder, this removes the dependency on the v1 command package and helps with xcbeautify support integration. --- xcodebuild/build.go | 11 +++++-- xcodebuild/build_test.go | 12 ++++--- xcodebuild/export.go | 10 +++++- xcodebuild/export_test.go | 66 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 xcodebuild/export_test.go diff --git a/xcodebuild/build.go b/xcodebuild/build.go index 5f9d3b04..3eb71eda 100644 --- a/xcodebuild/build.go +++ b/xcodebuild/build.go @@ -131,9 +131,16 @@ func (c *CommandBuilder) SetTestPlan(testPlan string) *CommandBuilder { return c } -func (c *CommandBuilder) cmdSlice() []string { +func (c CommandBuilder) cmdSlice() []string { slice := []string{toolName} - slice = append(slice, c.actions...) + slice = append(slice, c.CommandArgs()...) + + return slice +} + +// CommandArgs returns the xcodebuild command arguments, including actions and options +func (c CommandBuilder) CommandArgs() []string { + slice := append([]string{}, c.actions...) if c.projectPath != "" { if filepath.Ext(c.projectPath) == XCWorkspaceExtension { diff --git a/xcodebuild/build_test.go b/xcodebuild/build_test.go index f1649f10..fb99eb4b 100644 --- a/xcodebuild/build_test.go +++ b/xcodebuild/build_test.go @@ -1,9 +1,9 @@ package xcodebuild import ( - "reflect" - "strings" "testing" + + "github.com/stretchr/testify/require" ) func TestCommandBuilder_cmdSlice(t *testing.T) { @@ -187,9 +187,11 @@ func TestCommandBuilder_cmdSlice(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.builder().cmdSlice() - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("CommandBuilder.cmdSlice() = %v\nwant %v", strings.Join(got, "\n"), strings.Join(tt.want, "\n")) - } + require.Equal(t, tt.want, got) + + got = append(got, "extra") + got2 := tt.builder().cmdSlice() + require.Equal(t, tt.want, got2, "Second run after appending extra should not change the result") }) } } diff --git a/xcodebuild/export.go b/xcodebuild/export.go index eafbdf33..7b1003db 100644 --- a/xcodebuild/export.go +++ b/xcodebuild/export.go @@ -54,7 +54,15 @@ func (c *ExportCommandModel) SetAuthentication(authenticationParams Authenticati } func (c ExportCommandModel) cmdSlice() []string { - slice := []string{toolName, "-exportArchive"} + slice := []string{toolName} + slice = append(slice, c.CommandArgs()...) + + return slice +} + +// CommandArgs returns the xcodebuild command arguments for the export action +func (c ExportCommandModel) CommandArgs() []string { + slice := []string{"-exportArchive"} if c.archivePath != "" { slice = append(slice, "-archivePath", c.archivePath) } diff --git a/xcodebuild/export_test.go b/xcodebuild/export_test.go new file mode 100644 index 00000000..4e19237f --- /dev/null +++ b/xcodebuild/export_test.go @@ -0,0 +1,66 @@ +package xcodebuild + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestExportCommandModel_cmdSlice(t *testing.T) { + tests := []struct { + name string + archivePath string + exportDir string + exportOptionsPlist string + authentication *AuthenticationParams + want []string + }{ + { + name: "basic export", + archivePath: "sample.xcarchive", + exportDir: "/var/exported", + exportOptionsPlist: "/var/export_options.plist", + want: []string{"xcodebuild", + "-exportArchive", + "-archivePath", "sample.xcarchive", + "-exportPath", "/var/exported", + "-exportOptionsPlist", "/var/export_options.plist", + }, + }, + { + name: "export with authentication", + archivePath: "sample.xcarchive", + authentication: &AuthenticationParams{ + KeyID: "keyID", + IsssuerID: "issuerID", + KeyPath: "/key/path", + }, + want: []string{"xcodebuild", + "-exportArchive", + "-archivePath", "sample.xcarchive", + "-allowProvisioningUpdates", + "-authenticationKeyPath", "/key/path", + "-authenticationKeyID", "keyID", + "-authenticationKeyIssuerID", "issuerID", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := NewExportCommand() + c.SetArchivePath(tt.archivePath) + c.SetExportDir(tt.exportDir) + c.SetExportOptionsPlist(tt.exportOptionsPlist) + if tt.authentication != nil { + c.SetAuthentication(*tt.authentication) + } + + got := c.cmdSlice() + require.Equal(t, tt.want, got) + + got = append(got, "extra") + got2 := c.cmdSlice() + require.Equal(t, tt.want, got2, "result should not be modified") + }) + } +} From 458aa7bf254bbb74253752c959dd9a187150556f Mon Sep 17 00:00:00 2001 From: LaszloP <7979773+lpusok@users.noreply.github.com> Date: Wed, 26 Mar 2025 15:36:28 +0100 Subject: [PATCH 2/4] Disable linter --- xcodebuild/build_test.go | 2 +- xcodebuild/export_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xcodebuild/build_test.go b/xcodebuild/build_test.go index fb99eb4b..9229b6ee 100644 --- a/xcodebuild/build_test.go +++ b/xcodebuild/build_test.go @@ -189,7 +189,7 @@ func TestCommandBuilder_cmdSlice(t *testing.T) { got := tt.builder().cmdSlice() require.Equal(t, tt.want, got) - got = append(got, "extra") + got = append(got, "extra") // nolint:ineffassign got2 := tt.builder().cmdSlice() require.Equal(t, tt.want, got2, "Second run after appending extra should not change the result") }) diff --git a/xcodebuild/export_test.go b/xcodebuild/export_test.go index 4e19237f..5cf5b4b7 100644 --- a/xcodebuild/export_test.go +++ b/xcodebuild/export_test.go @@ -58,7 +58,7 @@ func TestExportCommandModel_cmdSlice(t *testing.T) { got := c.cmdSlice() require.Equal(t, tt.want, got) - got = append(got, "extra") + got = append(got, "extra") // nolint:ineffassign got2 := c.cmdSlice() require.Equal(t, tt.want, got2, "result should not be modified") }) From 4b3eddecc77502514e06325ece62bf33cc44e1e8 Mon Sep 17 00:00:00 2001 From: LaszloP <7979773+lpusok@users.noreply.github.com> Date: Wed, 26 Mar 2025 15:40:16 +0100 Subject: [PATCH 3/4] removed ineffective assignment --- xcodebuild/build_test.go | 3 +-- xcodebuild/export_test.go | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/xcodebuild/build_test.go b/xcodebuild/build_test.go index 9229b6ee..edad5cfe 100644 --- a/xcodebuild/build_test.go +++ b/xcodebuild/build_test.go @@ -189,9 +189,8 @@ func TestCommandBuilder_cmdSlice(t *testing.T) { got := tt.builder().cmdSlice() require.Equal(t, tt.want, got) - got = append(got, "extra") // nolint:ineffassign got2 := tt.builder().cmdSlice() - require.Equal(t, tt.want, got2, "Second run after appending extra should not change the result") + require.Equal(t, tt.want, got2, "Second run should return the same result") }) } } diff --git a/xcodebuild/export_test.go b/xcodebuild/export_test.go index 5cf5b4b7..6b750762 100644 --- a/xcodebuild/export_test.go +++ b/xcodebuild/export_test.go @@ -58,9 +58,8 @@ func TestExportCommandModel_cmdSlice(t *testing.T) { got := c.cmdSlice() require.Equal(t, tt.want, got) - got = append(got, "extra") // nolint:ineffassign got2 := c.cmdSlice() - require.Equal(t, tt.want, got2, "result should not be modified") + require.Equal(t, tt.want, got2, "Second run should return the same result") }) } } From 51411de7c8f347eacad45b57933fd8c99663b080 Mon Sep 17 00:00:00 2001 From: LaszloP <7979773+lpusok@users.noreply.github.com> Date: Wed, 26 Mar 2025 16:08:04 +0100 Subject: [PATCH 4/4] Use pointer receiver consistently everywhere --- xcodebuild/build.go | 12 ++++++------ xcodebuild/export.go | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/xcodebuild/build.go b/xcodebuild/build.go index 3eb71eda..273f9376 100644 --- a/xcodebuild/build.go +++ b/xcodebuild/build.go @@ -131,7 +131,7 @@ func (c *CommandBuilder) SetTestPlan(testPlan string) *CommandBuilder { return c } -func (c CommandBuilder) cmdSlice() []string { +func (c *CommandBuilder) cmdSlice() []string { slice := []string{toolName} slice = append(slice, c.CommandArgs()...) @@ -139,7 +139,7 @@ func (c CommandBuilder) cmdSlice() []string { } // CommandArgs returns the xcodebuild command arguments, including actions and options -func (c CommandBuilder) CommandArgs() []string { +func (c *CommandBuilder) CommandArgs() []string { slice := append([]string{}, c.actions...) if c.projectPath != "" { @@ -197,25 +197,25 @@ func (c CommandBuilder) CommandArgs() []string { } // PrintableCmd ... -func (c CommandBuilder) PrintableCmd() string { +func (c *CommandBuilder) PrintableCmd() string { cmdSlice := c.cmdSlice() return command.PrintableCommandArgs(false, cmdSlice) } // Command ... -func (c CommandBuilder) Command() *command.Model { +func (c *CommandBuilder) Command() *command.Model { cmdSlice := c.cmdSlice() return command.New(cmdSlice[0], cmdSlice[1:]...) } // ExecCommand ... -func (c CommandBuilder) ExecCommand() *exec.Cmd { +func (c *CommandBuilder) ExecCommand() *exec.Cmd { command := c.Command() return command.GetCmd() } // Run ... -func (c CommandBuilder) Run() error { +func (c *CommandBuilder) Run() error { command := c.Command() command.SetStdout(os.Stdout) diff --git a/xcodebuild/export.go b/xcodebuild/export.go index 7b1003db..6c36f8fb 100644 --- a/xcodebuild/export.go +++ b/xcodebuild/export.go @@ -53,7 +53,7 @@ func (c *ExportCommandModel) SetAuthentication(authenticationParams Authenticati return c } -func (c ExportCommandModel) cmdSlice() []string { +func (c *ExportCommandModel) cmdSlice() []string { slice := []string{toolName} slice = append(slice, c.CommandArgs()...) @@ -61,7 +61,7 @@ func (c ExportCommandModel) cmdSlice() []string { } // CommandArgs returns the xcodebuild command arguments for the export action -func (c ExportCommandModel) CommandArgs() []string { +func (c *ExportCommandModel) CommandArgs() []string { slice := []string{"-exportArchive"} if c.archivePath != "" { slice = append(slice, "-archivePath", c.archivePath) @@ -83,25 +83,25 @@ func (c ExportCommandModel) CommandArgs() []string { } // PrintableCmd ... -func (c ExportCommandModel) PrintableCmd() string { +func (c *ExportCommandModel) PrintableCmd() string { cmdSlice := c.cmdSlice() return command.PrintableCommandArgs(false, cmdSlice) } // Command ... -func (c ExportCommandModel) Command() *command.Model { +func (c *ExportCommandModel) Command() *command.Model { cmdSlice := c.cmdSlice() return command.New(cmdSlice[0], cmdSlice[1:]...) } // Cmd ... -func (c ExportCommandModel) Cmd() *exec.Cmd { +func (c *ExportCommandModel) Cmd() *exec.Cmd { command := c.Command() return command.GetCmd() } // Run ... -func (c ExportCommandModel) Run() error { +func (c *ExportCommandModel) Run() error { command := c.Command() command.SetStdout(os.Stdout) @@ -111,7 +111,7 @@ func (c ExportCommandModel) Run() error { } // RunAndReturnOutput ... -func (c ExportCommandModel) RunAndReturnOutput() (string, error) { +func (c *ExportCommandModel) RunAndReturnOutput() (string, error) { command := c.Command() var outBuffer bytes.Buffer