From 570fc363e0e992cd02256498298380537e9bea51 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Fri, 29 Mar 2019 10:18:20 +0200 Subject: [PATCH] Enable console in init context fix #951 This commit does the bare minimal to enable useage of console init context. Unfortunetely this means that redirection to file is currently not supported in init context. --- js/bundle.go | 1 + js/runner_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/js/bundle.go b/js/bundle.go index 9214d6a1bec..c0eac23aca2 100644 --- a/js/bundle.go +++ b/js/bundle.go @@ -246,6 +246,7 @@ func (b *Bundle) instantiate(rt *goja.Runtime, init *InitContext) error { rt.Set("module", module) rt.Set("__ENV", b.Env) + rt.Set("console", common.Bind(rt, newConsole(), init.ctxPtr)) *init.ctxPtr = common.WithRuntime(context.Background(), rt) unbindInit := common.BindToGlobal(rt, common.Bind(rt, init, init.ctxPtr)) diff --git a/js/runner_test.go b/js/runner_test.go index ddcb80fc2f9..14ae4926c0f 100644 --- a/js/runner_test.go +++ b/js/runner_test.go @@ -357,6 +357,28 @@ func TestSetupDataNoSetup(t *testing.T) { };`) } +func TestConsoleInInitContext(t *testing.T) { + r1, err := getSimpleRunner("/script.js", ` + console.log("1"); + export default function(data) { + }; + `) + require.NoError(t, err) + + testdata := map[string]*Runner{"Source": r1} + for name, r := range testdata { + r := r + t.Run(name, func(t *testing.T) { + samples := make(chan stats.SampleContainer, 100) + vu, err := r.NewVU(samples) + if assert.NoError(t, err) { + err := vu.RunOnce(context.Background()) + assert.NoError(t, err) + } + }) + } +} + func TestSetupDataNoReturn(t *testing.T) { testSetupDataHelper(t, ` export let options = { setupTimeout: "1s", teardownTimeout: "1s" };