Skip to content

Commit

Permalink
Add test to validate behavior for parseProtobufFlag
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
--- 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
ok      github.com/hashicorp/packer-plugin-sdk/plugin   0.249s

```
  • Loading branch information
nywilken authored and lbajolet-hashicorp committed Dec 17, 2024
1 parent 687645d commit 993fc7b
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions plugin/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,58 @@ func TestSet(t *testing.T) {
t.Fatalf("Unexpected error: %s", diff)
}
}

func TestSetProtobufArgParsing(t *testing.T) {
testCases := []struct {
name string
useProto bool
in, out []string
}{
{
name: "no --protobuf argument provided",
in: []string{"example", "example-2"},
out: []string{"example", "example-2"},
useProto: false,
},
{
name: "providing --protobuf as first argument",
in: []string{"--protobuf", "example", "example-2"},
out: []string{"example", "example-2"},
useProto: true,
},
{
name: "providing --protobuf as last argument",
in: []string{"example", "example-2", "--protobuf"},
out: []string{"example", "example-2"},
useProto: true,
},
{
name: "providing --protobuf as middle argument",
in: []string{"example", "--protobuf", "example-2"},
out: []string{"example", "example-2"},
useProto: true,
},
//{
//name: "providing --protobuf multiple times",
//in: []string{"--protobuf", "--protobuf", "example-2"},
//out: []string{"example-2"},
//useProto: true,
//},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
set := NewSet()
got := set.parseProtobufFlag(tc.in...)

if diff := cmp.Diff(got, tc.out); diff != "" {
t.Errorf("Unexpected args: %s", diff)
}

if set.useProto != tc.useProto {
t.Errorf("expected useProto to be %t when %s but got %t", tc.useProto, tc.name, set.useProto)
}
})

}
}

0 comments on commit 993fc7b

Please sign in to comment.