Skip to content

Commit

Permalink
Enable console in init context fix #951
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mstoykov committed Mar 29, 2019
1 parent 0086a84 commit b4ba7b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,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))
Expand Down
26 changes: 26 additions & 0 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,32 @@ func TestSetupDataNoSetup(t *testing.T) {
testSetupDataHelper(t, src)
}

func TestConsoleInInitContext(t *testing.T) {
src := &lib.SourceData{
Filename: "/script.js",
Data: []byte(`
console.log("1");
export default function(data) {
};
`),
}
r1, err := New(src, afero.NewMemMapFs(), lib.RuntimeOptions{})
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) {
src := &lib.SourceData{
Filename: "/script.js",
Expand Down

0 comments on commit b4ba7b2

Please sign in to comment.