Skip to content

Commit

Permalink
feat(cmd): expose compatibility mode option to CLI
Browse files Browse the repository at this point in the history
Closes #1049
  • Loading branch information
Ivan Mirić committed Oct 17, 2019
1 parent 80be3a7 commit 9a34e24
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ func optionFlagSet() *pflag.FlagSet {
flags.StringSlice("tag", nil, "add a `tag` to be applied to all samples, as `[name]=[value]`")
flags.String("console-output", "", "redirects the console logging to the provided output file")
flags.Bool("discard-response-bodies", false, "Read but don't process or save HTTP response bodies")
flags.String("compatibility-mode", "es6",
"JavaScript compiler compatibility mode, \"es6\" or \"es51\"")
return flags
}

Expand Down Expand Up @@ -102,6 +104,7 @@ func getOptions(flags *pflag.FlagSet) (lib.Options, error) {
TeardownTimeout: types.NullDuration{Duration: types.Duration(10 * time.Second), Valid: false},

MetricSamplesBufferSize: null.NewInt(1000, false),
CompatibilityMode: getNullString(flags, "compatibility-mode"),
}

// Using Changed() because GetStringSlice() doesn't differentiate between empty and no value
Expand Down
15 changes: 15 additions & 0 deletions cmd/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/loadimpact/k6/lib"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"gopkg.in/guregu/null.v3"
)

var userEnvVarName = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]*$`)
Expand All @@ -52,13 +53,21 @@ func runtimeOptionFlagSet(includeSysEnv bool) *pflag.FlagSet {
flags := pflag.NewFlagSet("", 0)
flags.SortFlags = false
flags.Bool("include-system-env-vars", includeSysEnv, "pass the real system environment variables to the runtime")
// NOTE(imiric): This flag should only be defined in cmd.optionFlagSet,
// yet because of the fragmented JS runtime options configuration
// (and flag handling?) the cmd.TestEnvVars test would fail if this
// wasn't redefined here. Functionally it shouldn't matter, but this
// should be fixed as part of #883.
flags.String("compatibility-mode", "es6",
"JavaScript compiler compatibility mode, \"es6\" or \"es51\"")
flags.StringArrayP("env", "e", nil, "add/override environment variable with `VAR=value`")
return flags
}

func getRuntimeOptions(flags *pflag.FlagSet) (lib.RuntimeOptions, error) {
opts := lib.RuntimeOptions{
IncludeSystemEnvVars: getNullBool(flags, "include-system-env-vars"),
CompatibilityMode: getNullString(flags, "compatibility-mode"),
Env: make(map[string]string),
}

Expand All @@ -82,5 +91,11 @@ func getRuntimeOptions(flags *pflag.FlagSet) (lib.RuntimeOptions, error) {
opts.Env[k] = v
}

// Fallback to env
compatMode := opts.Env["K6_COMPATIBILITY_MODE"]
if !opts.CompatibilityMode.Valid && compatMode != "" {
opts.CompatibilityMode = null.StringFrom(compatMode)
}

return opts, nil
}

0 comments on commit 9a34e24

Please sign in to comment.