mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 20:03:01 +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:
parent
f1dc9eb4ef
commit
f97f5bb602
@ -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 != "" {
|
||||
|
78
v2/pkg/commands/buildtags/buildtags_test.go
Normal file
78
v2/pkg/commands/buildtags/buildtags_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user