From df96679331954a71341aa064dff8fed4f72112af Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Wed, 24 Apr 2024 11:17:03 +0300 Subject: [PATCH 1/2] k6/execution:Don't panic on accessing test.options --- js/modules/k6/execution/execution.go | 6 +++++- js/modules/k6/execution/execution_test.go | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/js/modules/k6/execution/execution.go b/js/modules/k6/execution/execution.go index d014699b8bc7..e102858439b9 100644 --- a/js/modules/k6/execution/execution.go +++ b/js/modules/k6/execution/execution.go @@ -176,8 +176,12 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) { } }, "options": func() interface{} { + vuState := mi.vu.State() + if vuState == nil { + common.Throw(rt, fmt.Errorf("getting test options in the init context is not supported")) + } if optionsObject == nil { - opts, err := optionsAsObject(rt, mi.vu.State().Options) + opts, err := optionsAsObject(rt, vuState.Options) if err != nil { common.Throw(rt, err) } diff --git a/js/modules/k6/execution/execution_test.go b/js/modules/k6/execution/execution_test.go index 615d0f7f232a..35f522b4c239 100644 --- a/js/modules/k6/execution/execution_test.go +++ b/js/modules/k6/execution/execution_test.go @@ -444,6 +444,23 @@ func TestScenarioNoAvailableInInitContext(t *testing.T) { } } +func TestOptionsNoAvailableInInitContext(t *testing.T) { + t.Parallel() + + rt := goja.New() + m, ok := New().NewModuleInstance( + &modulestest.VU{ + RuntimeField: rt, + CtxField: context.Background(), + }, + ).(*ModuleInstance) + require.True(t, ok) + require.NoError(t, rt.Set("exec", m.Exports().Default)) + + _, err := rt.RunString("exec.test.options") + require.ErrorContains(t, err, "getting test options in the init context is not supported") +} + func TestVUDefaultDetails(t *testing.T) { t.Parallel() From d357f4927290340bf66eb79af512215f20839c8d Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Wed, 24 Apr 2024 12:12:00 +0300 Subject: [PATCH 2/2] k6/execution: use common.NewInitContextError --- js/modules/k6/execution/execution.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/js/modules/k6/execution/execution.go b/js/modules/k6/execution/execution.go index e102858439b9..001cc403cd1a 100644 --- a/js/modules/k6/execution/execution.go +++ b/js/modules/k6/execution/execution.go @@ -127,12 +127,15 @@ func (mi *ModuleInstance) newScenarioInfo() (*goja.Object, error) { return newInfoObj(rt, si) } +//nolint:lll,gochecknoglobals +var instanceInfoInitContextErr = common.NewInitContextError("getting instance information in the init context is not supported") + // newInstanceInfo returns a goja.Object with property accessors to retrieve // information about the local instance stats. func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) { es := lib.GetExecutionState(mi.vu.Context()) if es == nil { - return nil, errors.New("getting instance information in the init context is not supported") + return nil, instanceInfoInitContextErr } rt := mi.vu.Runtime() @@ -157,6 +160,9 @@ func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) { return newInfoObj(rt, ti) } +//nolint:gochecknoglobals +var testInfoInitContextErr = common.NewInitContextError("getting test options in the init context is not supported") + // newTestInfo returns a goja.Object with property accessors to retrieve // information and control execution of the overall test run. func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) { @@ -178,7 +184,7 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) { "options": func() interface{} { vuState := mi.vu.State() if vuState == nil { - common.Throw(rt, fmt.Errorf("getting test options in the init context is not supported")) + common.Throw(rt, testInfoInitContextErr) } if optionsObject == nil { opts, err := optionsAsObject(rt, vuState.Options) @@ -194,12 +200,15 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) { return newInfoObj(rt, ti) } +//nolint:gochecknoglobals +var vuInfoInitContextErr = common.NewInitContextError("getting VU information in the init context is not supported") + // newVUInfo returns a goja.Object with property accessors to retrieve // information about the currently executing VU. func (mi *ModuleInstance) newVUInfo() (*goja.Object, error) { vuState := mi.vu.State() if vuState == nil { - return nil, errors.New("getting VU information in the init context is not supported") + return nil, vuInfoInitContextErr } rt := mi.vu.Runtime()