From 9a34e249828a86bd46586de3a2e8bfb552e26033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Miri=C4=87?= Date: Thu, 17 Oct 2019 16:56:38 +0200 Subject: [PATCH] feat(cmd): expose compatibility mode option to CLI Closes #1049 --- cmd/options.go | 3 +++ cmd/runtime_options.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/cmd/options.go b/cmd/options.go index b643870acca..491345eadba 100644 --- a/cmd/options.go +++ b/cmd/options.go @@ -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 } @@ -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 diff --git a/cmd/runtime_options.go b/cmd/runtime_options.go index c8c82ee7a4b..179bee1f6c0 100644 --- a/cmd/runtime_options.go +++ b/cmd/runtime_options.go @@ -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_]*$`) @@ -52,6 +53,13 @@ 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 } @@ -59,6 +67,7 @@ func runtimeOptionFlagSet(includeSysEnv bool) *pflag.FlagSet { 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), } @@ -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 }