Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browser empty string flag parsing #1386

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion chromium/browser_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func parseArgs(flags map[string]any) ([]string, error) {
for name, value := range flags {
switch value := value.(type) {
case string:
args = append(args, fmt.Sprintf("--%s=%s", name, value))
args = append(args, parseStringArg(name, value))
case bool:
if value {
args = append(args, fmt.Sprintf("--%s", name))
Expand All @@ -324,6 +324,15 @@ func parseArgs(flags map[string]any) ([]string, error) {
return args, nil
}

func parseStringArg(flag string, value string) string {
if strings.TrimSpace(value) == "" {
// If the value is empty, we don't include it in the args list.
// Otherwise, it will produce "--name=" which is invalid.
return fmt.Sprintf("--%s", flag)
}
return fmt.Sprintf("--%s=%s", flag, value)
}

func prepareFlags(lopts *common.BrowserOptions, k6opts *k6lib.Options) (map[string]any, error) {
// After Puppeteer's and Playwright's default behavior.
f := map[string]any{
Expand Down
84 changes: 84 additions & 0 deletions chromium/browser_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/fs"
"net"
"path/filepath"
"sort"
"testing"

"github.com/grafana/xk6-browser/common"
Expand Down Expand Up @@ -251,3 +252,86 @@ func TestExecutablePath(t *testing.T) {
})
}
}

func TestParseArgs(t *testing.T) {
t.Parallel()

tests := []struct {
name string
flags map[string]any
want []string
}{
{
name: "string_flag_with_value",
flags: map[string]any{
"flag1": "value1",
"flag2": "value2",
},
want: []string{
"--flag1=value1",
"--flag2=value2",
"--remote-debugging-port=0",
},
},
{
name: "string_flag_with_empty_value",
flags: map[string]any{
"flag1": "",
"flag2": "value2",
},
want: []string{
"--flag1",
"--flag2=value2",
"--remote-debugging-port=0",
},
},
{
name: "bool_flag_true",
flags: map[string]any{
"flag1": true,
"flag2": true,
},
want: []string{
"--flag1",
"--flag2",
"--remote-debugging-port=0",
},
},
{
name: "bool_flag_false",
flags: map[string]any{
"flag1": false,
"flag2": true,
},
want: []string{
"--flag2",
"--remote-debugging-port=0",
},
},
{
name: "invalid_flag_type",
flags: map[string]any{
"flag1": 123,
},
want: nil,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := parseArgs(tt.flags)

if tt.want == nil {
assert.Error(t, err)
return
}
require.NoError(t, err)
sort.StringSlice(tt.want).Sort()
sort.StringSlice(got).Sort()
assert.Equal(t, tt.want, got)
})
}
}
Loading