From f97f5bb602d9396fa67b84961d6a5f8f8dbc841e Mon Sep 17 00:00:00 2001 From: stffabi Date: Thu, 15 Sep 2022 09:04:00 +0200 Subject: [PATCH] [build] Fix buildtags parsing if only one tag is specified (#1858) * [build] Fix buildtags parsing if only one tag is specified Co-authored-by: Lea Anthony --- v2/pkg/commands/buildtags/buildtags.go | 13 +++- v2/pkg/commands/buildtags/buildtags_test.go | 78 +++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 v2/pkg/commands/buildtags/buildtags_test.go diff --git a/v2/pkg/commands/buildtags/buildtags.go b/v2/pkg/commands/buildtags/buildtags.go index 52eb5b9a1..70820d03d 100644 --- a/v2/pkg/commands/buildtags/buildtags.go +++ b/v2/pkg/commands/buildtags/buildtags.go @@ -2,20 +2,19 @@ package buildtags import ( "errors" - "github.com/samber/lo" "strings" + + "github.com/samber/lo" ) // Parse parses the given tags string and returns // a cleaned slice of strings. Both comma and space delimeted // tags are supported but not mixed. If mixed, an error is returned. func Parse(tags string) ([]string, error) { - if tags == "" { return nil, nil } - var userTags []string separator := "" if strings.Contains(tags, ",") { separator = "," @@ -26,6 +25,14 @@ func Parse(tags string) ([]string, error) { } separator = " " } + if separator == "" { + // We couldn't find any separator, so the whole string is used as user tag + // Otherwise we would end up with a list of every single character of the tags string, + // e.g.: `t,e,s,t` + return []string{tags}, nil + } + + var userTags []string for _, tag := range strings.Split(tags, separator) { thisTag := strings.TrimSpace(tag) if thisTag != "" { diff --git a/v2/pkg/commands/buildtags/buildtags_test.go b/v2/pkg/commands/buildtags/buildtags_test.go new file mode 100644 index 000000000..c13a158c7 --- /dev/null +++ b/v2/pkg/commands/buildtags/buildtags_test.go @@ -0,0 +1,78 @@ +package buildtags + +import ( + "reflect" + "testing" +) + +func TestParse(t *testing.T) { + tests := []struct { + name string + tags string + want []string + wantErr bool + }{ + { + name: "should support single tags", + tags: "test", + want: []string{"test"}, + wantErr: false, + }, + { + name: "should support space delimited tags", + tags: "test test2", + want: []string{"test", "test2"}, + wantErr: false, + }, + { + name: "should support comma delimited tags", + tags: "test,test2", + want: []string{"test", "test2"}, + wantErr: false, + }, + { + name: "should error if mixed tags", + tags: "test,test2 test3", + want: nil, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Parse(tt.tags) + if (err != nil) != tt.wantErr { + t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Parse() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestStringify(t *testing.T) { + tests := []struct { + name string + tags []string + want string + }{ + { + name: "should support single tags", + tags: []string{"test"}, + want: "test", + }, + { + name: "should support multiple tags", + tags: []string{"test", "test2"}, + want: "test,test2", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := Stringify(tt.tags); got != tt.want { + t.Errorf("Stringify() = %v, want %v", got, tt.want) + } + }) + } +}