Skip to content

Commit

Permalink
Handle the case where --protobuf is specified multiple times
Browse files Browse the repository at this point in the history
```
~>  go test ./plugin/... -v
=== RUN   TestPluginServerRandom
--- PASS: TestPluginServerRandom (0.00s)
=== RUN   TestSet
--- PASS: TestSet (0.00s)
=== RUN   TestSetProtobufArgParsing
=== RUN   TestSetProtobufArgParsing/no_--protobuf_argument_provided
=== RUN   TestSetProtobufArgParsing/providing_--protobuf_as_first_argument
=== RUN   TestSetProtobufArgParsing/providing_--protobuf_as_last_argument
=== RUN   TestSetProtobufArgParsing/providing_--protobuf_as_middle_argument
=== RUN   TestSetProtobufArgParsing/providing_--protobuf_multiple_times
--- PASS: TestSetProtobufArgParsing (0.00s)
    --- PASS: TestSetProtobufArgParsing/no_--protobuf_argument_provided (0.00s)
    --- PASS: TestSetProtobufArgParsing/providing_--protobuf_as_first_argument (0.00s)
    --- PASS: TestSetProtobufArgParsing/providing_--protobuf_as_last_argument (0.00s)
    --- PASS: TestSetProtobufArgParsing/providing_--protobuf_as_middle_argument (0.00s)
    --- PASS: TestSetProtobufArgParsing/providing_--protobuf_multiple_times (0.00s)
PASS
ok      github.com/hashicorp/packer-plugin-sdk/plugin   0.250s

```
  • Loading branch information
nywilken authored and lbajolet-hashicorp committed Dec 17, 2024
1 parent 2014e92 commit e0c1a4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
17 changes: 6 additions & 11 deletions plugin/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,15 @@ func (i *Set) Run() error {
//
// It then returns the args without it for the commands to process them.
func (i *Set) parseProtobufFlag(args ...string) []string {
protobufPos := -1
for i, arg := range args {
parsedArgs := make([]string, 0, len(args))
for _, arg := range args {
if arg == "--protobuf" {
protobufPos = i
break
i.useProto = true
continue
}
parsedArgs = append(parsedArgs, arg)
}

if protobufPos == -1 {
return args
}

i.useProto = true
return append(args[:protobufPos], args[protobufPos+1:]...)
return parsedArgs
}

func (i *Set) RunCommand(args ...string) error {
Expand Down
28 changes: 14 additions & 14 deletions plugin/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,34 @@ func TestSetProtobufArgParsing(t *testing.T) {
}{
{
name: "no --protobuf argument provided",
in: []string{"example", "example-2"},
out: []string{"example", "example-2"},
in: []string{"start", "builder", "example"},
out: []string{"start", "builder", "example"},
useProto: false,
},
{
name: "providing --protobuf as first argument",
in: []string{"--protobuf", "example", "example-2"},
out: []string{"example", "example-2"},
in: []string{"--protobuf", "start", "builder", "example"},
out: []string{"start", "builder", "example"},
useProto: true,
},
{
name: "providing --protobuf as last argument",
in: []string{"example", "example-2", "--protobuf"},
out: []string{"example", "example-2"},
in: []string{"start", "builder", "example", "--protobuf"},
out: []string{"start", "builder", "example"},
useProto: true,
},
{
name: "providing --protobuf as middle argument",
in: []string{"example", "--protobuf", "example-2"},
out: []string{"example", "example-2"},
in: []string{"start", "builder", "--protobuf", "example"},
out: []string{"start", "builder", "example"},
useProto: true,
},
{
name: "providing --protobuf multiple times",
in: []string{"--protobuf", "start", "builder", "--protobuf", "example", "--protobuf"},
out: []string{"start", "builder", "example"},
useProto: true,
},
//{
//name: "providing --protobuf multiple times",
//in: []string{"--protobuf", "--protobuf", "example-2"},
//out: []string{"example-2"},
//useProto: true,
//},
}

for _, tc := range testCases {
Expand Down

0 comments on commit e0c1a4f

Please sign in to comment.