Skip to content

Commit

Permalink
Use RuntimeOptions in js.Bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Jan 10, 2021
1 parent 3a0ac94 commit 10ac39f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
29 changes: 15 additions & 14 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"gopkg.in/guregu/null.v3"

"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/js/compiler"
Expand All @@ -49,8 +50,8 @@ type Bundle struct {

BaseInitContext *InitContext

Env map[string]string
CompatibilityMode lib.CompatibilityMode
RuntimeOptions lib.RuntimeOptions
CompatibilityMode lib.CompatibilityMode // parsed value

exports map[string]goja.Callable
}
Expand Down Expand Up @@ -90,7 +91,7 @@ func NewBundle(
Program: pgm,
BaseInitContext: NewInitContext(logger, rt, c, compatMode, new(context.Context),
filesystems, loader.Dir(src.URL)),
Env: rtOpts.Env,
RuntimeOptions: rtOpts,
CompatibilityMode: compatMode,
exports: make(map[string]goja.Callable),
}
Expand All @@ -112,13 +113,12 @@ func NewBundleFromArchive(logger logrus.FieldLogger, arc *lib.Archive, rtOpts li
return nil, errors.Errorf("expected bundle type 'js', got '%s'", arc.Type)
}

compatModeStr := arc.CompatibilityMode
if rtOpts.CompatibilityMode.Valid {
// `k6 run --compatibility-mode=whatever archive.tar` should override
if !rtOpts.CompatibilityMode.Valid {
// `k6 run --compatibility-mode=whatever archive.tar` should override
// whatever value is in the archive
compatModeStr = rtOpts.CompatibilityMode.String
rtOpts.CompatibilityMode = null.StringFrom(arc.CompatibilityMode)
}
compatMode, err := lib.ValidateCompatibilityMode(compatModeStr)
compatMode, err := lib.ValidateCompatibilityMode(rtOpts.CompatibilityMode.String)
if err != nil {
return nil, err
}
Expand All @@ -140,14 +140,15 @@ func NewBundleFromArchive(logger logrus.FieldLogger, arc *lib.Archive, rtOpts li
for k, v := range rtOpts.Env {
env[k] = v
}
rtOpts.Env = env

bundle := &Bundle{
Filename: arc.FilenameURL,
Source: string(arc.Data),
Program: pgm,
Options: arc.Options,
BaseInitContext: initctx,
Env: env,
RuntimeOptions: rtOpts,
CompatibilityMode: compatMode,
exports: make(map[string]goja.Callable),
}
Expand All @@ -174,13 +175,13 @@ func (b *Bundle) makeArchive() *lib.Archive {
FilenameURL: b.Filename,
Data: []byte(b.Source),
PwdURL: b.BaseInitContext.pwd,
Env: make(map[string]string, len(b.Env)),
Env: make(map[string]string, len(b.RuntimeOptions.Env)),
CompatibilityMode: b.CompatibilityMode.String(),
K6Version: consts.Version,
Goos: runtime.GOOS,
}
// Copy env so changes in the archive are not reflected in the source Bundle
for k, v := range b.Env {
for k, v := range b.RuntimeOptions.Env {
arc.Env[k] = v
}

Expand Down Expand Up @@ -245,7 +246,7 @@ func (b *Bundle) Instantiate(logger logrus.FieldLogger, vuID int64) (bi *BundleI
Runtime: rt,
Context: ctxPtr,
exports: make(map[string]goja.Callable),
env: b.Env,
env: b.RuntimeOptions.Env,
}

// Grab any exported functions that could be executed. These were
Expand Down Expand Up @@ -291,8 +292,8 @@ func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init *
_ = module.Set("exports", exports)
rt.Set("module", module)

env := make(map[string]string, len(b.Env))
for key, value := range b.Env {
env := make(map[string]string, len(b.RuntimeOptions.Env))
for key, value := range b.RuntimeOptions.Env {
env[key] = value
}
rt.Set("__ENV", env)
Expand Down
4 changes: 2 additions & 2 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,8 @@ func TestBundleEnv(t *testing.T) {
for name, b := range bundles {
b := b
t.Run(name, func(t *testing.T) {
assert.Equal(t, "1", b.Env["TEST_A"])
assert.Equal(t, "", b.Env["TEST_B"])
assert.Equal(t, "1", b.RuntimeOptions.Env["TEST_A"])
assert.Equal(t, "", b.RuntimeOptions.Env["TEST_B"])

bi, err := b.Instantiate(logger, 0)
if assert.NoError(t, err) {
Expand Down

0 comments on commit 10ac39f

Please sign in to comment.