From 993fc7bbc331a83f47dbd857b98eb53cc98040d1 Mon Sep 17 00:00:00 2001 From: Wilken Rivera Date: Fri, 21 Jun 2024 15:09:08 -0400 Subject: [PATCH] Add test to validate behavior for parseProtobufFlag ``` ~> 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 ``` --- plugin/set_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/plugin/set_test.go b/plugin/set_test.go index 02d65e115..c21d4dee3 100644 --- a/plugin/set_test.go +++ b/plugin/set_test.go @@ -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) + } + }) + + } +}