Skip to content

Commit 6b912cd

Browse files
authored
Getter for xcodebuild command arguments (#264)
* Added CommandArgs function for the xcodebuild CommandBuilder, this removes the dependency on the v1 command package and helps with xcbeautify support integration. * Use pointer receiver consistently everywhere
1 parent e968028 commit 6b912cd

File tree

4 files changed

+98
-17
lines changed

4 files changed

+98
-17
lines changed

xcodebuild/build.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,14 @@ func (c *CommandBuilder) SetTestPlan(testPlan string) *CommandBuilder {
133133

134134
func (c *CommandBuilder) cmdSlice() []string {
135135
slice := []string{toolName}
136-
slice = append(slice, c.actions...)
136+
slice = append(slice, c.CommandArgs()...)
137+
138+
return slice
139+
}
140+
141+
// CommandArgs returns the xcodebuild command arguments, including actions and options
142+
func (c *CommandBuilder) CommandArgs() []string {
143+
slice := append([]string{}, c.actions...)
137144

138145
if c.projectPath != "" {
139146
if filepath.Ext(c.projectPath) == XCWorkspaceExtension {
@@ -190,25 +197,25 @@ func (c *CommandBuilder) cmdSlice() []string {
190197
}
191198

192199
// PrintableCmd ...
193-
func (c CommandBuilder) PrintableCmd() string {
200+
func (c *CommandBuilder) PrintableCmd() string {
194201
cmdSlice := c.cmdSlice()
195202
return command.PrintableCommandArgs(false, cmdSlice)
196203
}
197204

198205
// Command ...
199-
func (c CommandBuilder) Command() *command.Model {
206+
func (c *CommandBuilder) Command() *command.Model {
200207
cmdSlice := c.cmdSlice()
201208
return command.New(cmdSlice[0], cmdSlice[1:]...)
202209
}
203210

204211
// ExecCommand ...
205-
func (c CommandBuilder) ExecCommand() *exec.Cmd {
212+
func (c *CommandBuilder) ExecCommand() *exec.Cmd {
206213
command := c.Command()
207214
return command.GetCmd()
208215
}
209216

210217
// Run ...
211-
func (c CommandBuilder) Run() error {
218+
func (c *CommandBuilder) Run() error {
212219
command := c.Command()
213220

214221
command.SetStdout(os.Stdout)

xcodebuild/build_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package xcodebuild
22

33
import (
4-
"reflect"
5-
"strings"
64
"testing"
5+
6+
"github.com/stretchr/testify/require"
77
)
88

99
func TestCommandBuilder_cmdSlice(t *testing.T) {
@@ -187,9 +187,10 @@ func TestCommandBuilder_cmdSlice(t *testing.T) {
187187
for _, tt := range tests {
188188
t.Run(tt.name, func(t *testing.T) {
189189
got := tt.builder().cmdSlice()
190-
if !reflect.DeepEqual(got, tt.want) {
191-
t.Errorf("CommandBuilder.cmdSlice() = %v\nwant %v", strings.Join(got, "\n"), strings.Join(tt.want, "\n"))
192-
}
190+
require.Equal(t, tt.want, got)
191+
192+
got2 := tt.builder().cmdSlice()
193+
require.Equal(t, tt.want, got2, "Second run should return the same result")
193194
})
194195
}
195196
}

xcodebuild/export.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ func (c *ExportCommandModel) SetAuthentication(authenticationParams Authenticati
5353
return c
5454
}
5555

56-
func (c ExportCommandModel) cmdSlice() []string {
57-
slice := []string{toolName, "-exportArchive"}
56+
func (c *ExportCommandModel) cmdSlice() []string {
57+
slice := []string{toolName}
58+
slice = append(slice, c.CommandArgs()...)
59+
60+
return slice
61+
}
62+
63+
// CommandArgs returns the xcodebuild command arguments for the export action
64+
func (c *ExportCommandModel) CommandArgs() []string {
65+
slice := []string{"-exportArchive"}
5866
if c.archivePath != "" {
5967
slice = append(slice, "-archivePath", c.archivePath)
6068
}
@@ -75,25 +83,25 @@ func (c ExportCommandModel) cmdSlice() []string {
7583
}
7684

7785
// PrintableCmd ...
78-
func (c ExportCommandModel) PrintableCmd() string {
86+
func (c *ExportCommandModel) PrintableCmd() string {
7987
cmdSlice := c.cmdSlice()
8088
return command.PrintableCommandArgs(false, cmdSlice)
8189
}
8290

8391
// Command ...
84-
func (c ExportCommandModel) Command() *command.Model {
92+
func (c *ExportCommandModel) Command() *command.Model {
8593
cmdSlice := c.cmdSlice()
8694
return command.New(cmdSlice[0], cmdSlice[1:]...)
8795
}
8896

8997
// Cmd ...
90-
func (c ExportCommandModel) Cmd() *exec.Cmd {
98+
func (c *ExportCommandModel) Cmd() *exec.Cmd {
9199
command := c.Command()
92100
return command.GetCmd()
93101
}
94102

95103
// Run ...
96-
func (c ExportCommandModel) Run() error {
104+
func (c *ExportCommandModel) Run() error {
97105
command := c.Command()
98106

99107
command.SetStdout(os.Stdout)
@@ -103,7 +111,7 @@ func (c ExportCommandModel) Run() error {
103111
}
104112

105113
// RunAndReturnOutput ...
106-
func (c ExportCommandModel) RunAndReturnOutput() (string, error) {
114+
func (c *ExportCommandModel) RunAndReturnOutput() (string, error) {
107115
command := c.Command()
108116

109117
var outBuffer bytes.Buffer

xcodebuild/export_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package xcodebuild
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestExportCommandModel_cmdSlice(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
archivePath string
13+
exportDir string
14+
exportOptionsPlist string
15+
authentication *AuthenticationParams
16+
want []string
17+
}{
18+
{
19+
name: "basic export",
20+
archivePath: "sample.xcarchive",
21+
exportDir: "/var/exported",
22+
exportOptionsPlist: "/var/export_options.plist",
23+
want: []string{"xcodebuild",
24+
"-exportArchive",
25+
"-archivePath", "sample.xcarchive",
26+
"-exportPath", "/var/exported",
27+
"-exportOptionsPlist", "/var/export_options.plist",
28+
},
29+
},
30+
{
31+
name: "export with authentication",
32+
archivePath: "sample.xcarchive",
33+
authentication: &AuthenticationParams{
34+
KeyID: "keyID",
35+
IsssuerID: "issuerID",
36+
KeyPath: "/key/path",
37+
},
38+
want: []string{"xcodebuild",
39+
"-exportArchive",
40+
"-archivePath", "sample.xcarchive",
41+
"-allowProvisioningUpdates",
42+
"-authenticationKeyPath", "/key/path",
43+
"-authenticationKeyID", "keyID",
44+
"-authenticationKeyIssuerID", "issuerID",
45+
},
46+
},
47+
}
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
c := NewExportCommand()
51+
c.SetArchivePath(tt.archivePath)
52+
c.SetExportDir(tt.exportDir)
53+
c.SetExportOptionsPlist(tt.exportOptionsPlist)
54+
if tt.authentication != nil {
55+
c.SetAuthentication(*tt.authentication)
56+
}
57+
58+
got := c.cmdSlice()
59+
require.Equal(t, tt.want, got)
60+
61+
got2 := c.cmdSlice()
62+
require.Equal(t, tt.want, got2, "Second run should return the same result")
63+
})
64+
}
65+
}

0 commit comments

Comments
 (0)