Skip to content

Commit

Permalink
fix(js): unpack guregu/null value when setting script options
Browse files Browse the repository at this point in the history
Closes #886
  • Loading branch information
Ivan Mirić committed Sep 11, 2019
1 parent a2077dd commit 98d4d70
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"runtime"

"github.com/loadimpact/k6/lib/consts"
"gopkg.in/guregu/null.v3"

"github.com/dop251/goja"
"github.com/loadimpact/k6/js/common"
Expand Down Expand Up @@ -217,6 +218,18 @@ func (b *Bundle) Instantiate() (bi *BundleInstance, instErr error) {
jsOptionsObj = jsOptions.ToObject(rt)
}
b.Options.ForEachSpecified("json", func(key string, val interface{}) {
switch v := val.(type) {
case null.Bool:
val = v.ValueOrZero()
case null.Float:
val = v.ValueOrZero()
case null.Int:
val = v.ValueOrZero()
case null.String:
val = v.ValueOrZero()
case null.Time:
val = v.ValueOrZero()
}
if err := jsOptionsObj.Set(key, val); err != nil {
instErr = err
}
Expand Down
10 changes: 10 additions & 0 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ func TestOpen(t *testing.T) {

func TestBundleInstantiate(t *testing.T) {
b, err := getSimpleBundle("/script.js", `
export let options = {
vus: 5,
};
let val = true;
export default function() { return val; }
`)
Expand All @@ -614,6 +617,13 @@ func TestBundleInstantiate(t *testing.T) {
assert.Equal(t, false, v.Export())
}
})

t.Run("Options", func(t *testing.T) {
// Ensure `options` properties are correctly marshalled
jsOptions := bi.Runtime.Get("options").ToObject(bi.Runtime)
val := jsOptions.Get("vus").Export()
assert.Equal(t, int64(5), val)
})
}

func TestBundleEnv(t *testing.T) {
Expand Down

0 comments on commit 98d4d70

Please sign in to comment.