diff --git a/xcodebuild/build.go b/xcodebuild/build.go index 5f9d3b04..273f9376 100644 --- a/xcodebuild/build.go +++ b/xcodebuild/build.go @@ -133,7 +133,14 @@ func (c *CommandBuilder) SetTestPlan(testPlan string) *CommandBuilder { 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 { @@ -190,25 +197,25 @@ func (c *CommandBuilder) cmdSlice() []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/build_test.go b/xcodebuild/build_test.go index f1649f10..edad5cfe 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,10 @@ 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) + + got2 := tt.builder().cmdSlice() + require.Equal(t, tt.want, got2, "Second run should return the same result") }) } } diff --git a/xcodebuild/export.go b/xcodebuild/export.go index eafbdf33..6c36f8fb 100644 --- a/xcodebuild/export.go +++ b/xcodebuild/export.go @@ -53,8 +53,16 @@ func (c *ExportCommandModel) SetAuthentication(authenticationParams Authenticati return c } -func (c ExportCommandModel) cmdSlice() []string { - slice := []string{toolName, "-exportArchive"} +func (c *ExportCommandModel) cmdSlice() []string { + 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) } @@ -75,25 +83,25 @@ func (c ExportCommandModel) cmdSlice() []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) @@ -103,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 diff --git a/xcodebuild/export_test.go b/xcodebuild/export_test.go new file mode 100644 index 00000000..6b750762 --- /dev/null +++ b/xcodebuild/export_test.go @@ -0,0 +1,65 @@ +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) + + got2 := c.cmdSlice() + require.Equal(t, tt.want, got2, "Second run should return the same result") + }) + } +}