From e0c1a4fe66ffc7a79c1d16cca00f853340c05f77 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Fri, 21 Jun 2024 15:38:25 -0400 Subject: [PATCH] Handle the case where --protobuf is specified multiple times ``` ~> 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 ``` --- plugin/set.go | 17 ++++++----------- plugin/set_test.go | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/plugin/set.go b/plugin/set.go index f94319bbf..d9f10f1cd 100644 --- a/plugin/set.go +++ b/plugin/set.go @@ -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 { diff --git a/plugin/set_test.go b/plugin/set_test.go index c21d4dee3..3490dc211 100644 --- a/plugin/set_test.go +++ b/plugin/set_test.go @@ -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 {