Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 2f9a61a

Browse files
inancgumusankur22Ivan Mirić
committed
Apply review requests (#608)
Co-authored-by: Ankur <ankur.agarwal@grafana.com> Co-authored-by: Ivan Mirić <ivan.miric@grafana.com>
1 parent 8e21295 commit 2f9a61a

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

common/launch.go renamed to common/browser_options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/grafana/xk6-browser/k6ext"
1111
)
1212

13+
// ProxyOptions allows configuring a proxy server.
1314
type ProxyOptions struct {
1415
Server string
1516
Bypass string
@@ -38,6 +39,7 @@ type LaunchPersistentContextOptions struct {
3839
BrowserContextOptions
3940
}
4041

42+
// NewLaunchOptions returns a new LaunchOptions.
4143
func NewLaunchOptions() *LaunchOptions {
4244
launchOpts := LaunchOptions{
4345
Env: make(map[string]string),

common/launch_test.go renamed to common/browser_options_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/grafana/xk6-browser/k6ext/k6test"
1111
)
1212

13-
func TestLaunchOptionsParse(t *testing.T) {
13+
func TestBrowserLaunchOptionsParse(t *testing.T) {
1414
t.Parallel()
1515

1616
for name, tt := range map[string]struct {
@@ -56,7 +56,7 @@ func TestLaunchOptionsParse(t *testing.T) {
5656
},
5757
},
5858
"debug_err": {
59-
opts: map[string]any{"debug": "hello"},
59+
opts: map[string]any{"debug": "true"},
6060
err: "debug should be a boolean",
6161
},
6262
"devtools": {
@@ -69,7 +69,7 @@ func TestLaunchOptionsParse(t *testing.T) {
6969
},
7070
},
7171
"devtools_err": {
72-
opts: map[string]any{"devtools": "hello"},
72+
opts: map[string]any{"devtools": "true"},
7373
err: "devtools should be a boolean",
7474
},
7575
"env": {
@@ -108,7 +108,7 @@ func TestLaunchOptionsParse(t *testing.T) {
108108
},
109109
},
110110
"headless_err": {
111-
opts: map[string]any{"headless": "ABC"},
111+
opts: map[string]any{"headless": "true"},
112112
err: "headless should be a boolean",
113113
},
114114
"ignoreDefaultArgs": {
@@ -196,7 +196,8 @@ func TestLaunchOptionsParse(t *testing.T) {
196196
vu = k6test.NewVU(t)
197197
lo = NewLaunchOptions()
198198
)
199-
if err := lo.Parse(vu.Context(), vu.ToGojaValue(tt.opts)); tt.err != "" {
199+
err := lo.Parse(vu.Context(), vu.ToGojaValue(tt.opts))
200+
if tt.err != "" {
200201
require.ErrorContains(t, err, tt.err)
201202
} else {
202203
require.NoError(t, err)

common/options.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ package common
33
import (
44
"fmt"
55
"reflect"
6-
"strconv"
76
"time"
87

98
"github.com/dop251/goja"
9+
10+
"go.k6.io/k6/lib/types"
1011
)
1112

1213
func parseBoolOpt(key string, val goja.Value) (b bool, err error) {
13-
if b, err = strconv.ParseBool(val.String()); err != nil {
14-
return false, fmt.Errorf("%s should be a boolean: %w", key, err)
14+
if val.ExportType().Kind() != reflect.Bool {
15+
return false, fmt.Errorf("%s should be a boolean", key)
1516
}
16-
return
17+
b, _ = val.Export().(bool)
18+
return b, nil
1719
}
1820

1921
func parseStrOpt(key string, val goja.Value) (s string, err error) {
@@ -24,7 +26,7 @@ func parseStrOpt(key string, val goja.Value) (s string, err error) {
2426
}
2527

2628
func parseTimeOpt(key string, val goja.Value) (t time.Duration, err error) {
27-
if t, err = time.ParseDuration(val.String()); err != nil {
29+
if t, err = types.GetDurationValue(val.String()); err != nil {
2830
return time.Duration(0), fmt.Errorf("%s should be a time duration value: %w", key, err)
2931
}
3032
return
@@ -45,7 +47,7 @@ func exportOpt[T any](rt *goja.Runtime, key string, src goja.Value, dst T) error
4547
reflect.Slice: "an array of",
4648
}[kind]
4749
if !ok {
48-
panic("src should be one of: map, struct, slice")
50+
panic("dst should be one of: map, struct, slice")
4951
}
5052
if err := rt.ExportTo(src, dst); err != nil {
5153
if kind == reflect.Slice {

0 commit comments

Comments
 (0)