5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 06:51:26 +08:00

[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 <lea.anthony@gmail.com>
This commit is contained in:
stffabi 2022-09-15 09:04:00 +02:00 committed by GitHub
parent f1dc9eb4ef
commit f97f5bb602
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 3 deletions

View File

@ -2,20 +2,19 @@ package buildtags
import ( import (
"errors" "errors"
"github.com/samber/lo"
"strings" "strings"
"github.com/samber/lo"
) )
// Parse parses the given tags string and returns // Parse parses the given tags string and returns
// a cleaned slice of strings. Both comma and space delimeted // a cleaned slice of strings. Both comma and space delimeted
// tags are supported but not mixed. If mixed, an error is returned. // tags are supported but not mixed. If mixed, an error is returned.
func Parse(tags string) ([]string, error) { func Parse(tags string) ([]string, error) {
if tags == "" { if tags == "" {
return nil, nil return nil, nil
} }
var userTags []string
separator := "" separator := ""
if strings.Contains(tags, ",") { if strings.Contains(tags, ",") {
separator = "," separator = ","
@ -26,6 +25,14 @@ func Parse(tags string) ([]string, error) {
} }
separator = " " 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) { for _, tag := range strings.Split(tags, separator) {
thisTag := strings.TrimSpace(tag) thisTag := strings.TrimSpace(tag)
if thisTag != "" { if thisTag != "" {

View File

@ -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)
}
})
}
}