Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Js package optimizaitons #1429

Merged
merged 3 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions js/common/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,23 @@ func TestBind(t *testing.T) {
{"Methods", bridgeTestMethodsType{}, func(t *testing.T, obj interface{}, rt *goja.Runtime) {
t.Run("unexportedFn", func(t *testing.T) {
_, err := RunString(rt, `obj.unexportedFn()`)
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedFn' at <eval>:1:30(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedFn' at <eval>:1:17(3)")
})
t.Run("ExportedFn", func(t *testing.T) {
_, err := RunString(rt, `obj.exportedFn()`)
assert.NoError(t, err)
})
t.Run("unexportedPtrFn", func(t *testing.T) {
_, err := RunString(rt, `obj.unexportedPtrFn()`)
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedPtrFn' at <eval>:1:33(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'unexportedPtrFn' at <eval>:1:20(3)")
})
t.Run("ExportedPtrFn", func(t *testing.T) {
_, err := RunString(rt, `obj.exportedPtrFn()`)
switch obj.(type) {
case *bridgeTestMethodsType:
assert.NoError(t, err)
case bridgeTestMethodsType:
assert.EqualError(t, err, "TypeError: Object has no member 'exportedPtrFn' at <eval>:1:31(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'exportedPtrFn' at <eval>:1:18(3)")
default:
assert.Fail(t, "INVALID TYPE")
}
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestBind(t *testing.T) {
_, err := RunString(rt, `obj.contextInject()`)
switch impl := obj.(type) {
case bridgeTestContextInjectType:
assert.EqualError(t, err, "TypeError: Object has no member 'contextInject' at <eval>:1:31(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'contextInject' at <eval>:1:18(3)")
case *bridgeTestContextInjectType:
assert.EqualError(t, err, "GoError: contextInject() can only be called from within default()")
assert.Equal(t, nil, impl.ctx)
Expand All @@ -546,7 +546,7 @@ func TestBind(t *testing.T) {
_, err := RunString(rt, `obj.contextInjectPtr()`)
switch impl := obj.(type) {
case bridgeTestContextInjectPtrType:
assert.EqualError(t, err, "TypeError: Object has no member 'contextInjectPtr' at <eval>:1:34(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'contextInjectPtr' at <eval>:1:21(3)")
case *bridgeTestContextInjectPtrType:
assert.NoError(t, err)
assert.Equal(t, ctxPtr, impl.ctxPtr)
Expand All @@ -566,7 +566,7 @@ func TestBind(t *testing.T) {
}
case bridgeTestCounterType:
_, err := RunString(rt, `obj.count()`)
assert.EqualError(t, err, "TypeError: Object has no member 'count' at <eval>:1:23(3)")
assert.EqualError(t, err, "TypeError: Object has no member 'count' at <eval>:1:10(3)")
default:
assert.Fail(t, "UNKNOWN TYPE")
}
Expand Down
9 changes: 7 additions & 2 deletions js/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,20 @@ import (
"github.com/loadimpact/k6/js/compiler"
)

// Runs an ES6 string in the given runtime. Use this rather than writing ES5 in tests.
// RunString Runs an string in the given runtime. Use this if writing ES5 in tests isn't a problem.
func RunString(rt *goja.Runtime, src string) (goja.Value, error) {
return rt.RunString(src)
}

// RunES6String Runs an ES6 string in the given runtime. Use this rather than writing ES5 in tests.
func RunES6String(rt *goja.Runtime, src string) (goja.Value, error) {
var err error
c := compiler.New()
src, _, err = c.Transform(src, "__string__")
if err != nil {
return goja.Undefined(), err
}
return rt.RunString(src)
return RunString(rt, src)
}

// Throws a JS error; avoids re-wrapping GoErrors.
Expand Down
4 changes: 2 additions & 2 deletions js/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import (

func TestRunString(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
_, err := RunString(goja.New(), `let a = 1;`)
_, err := RunES6String(goja.New(), `let a = 1;`)
assert.NoError(t, err)
})
t.Run("Invalid", func(t *testing.T) {
_, err := RunString(goja.New(), `let a = #;`)
_, err := RunES6String(goja.New(), `let a = #;`)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "SyntaxError: __string__: Unexpected character '#' (1:8)\n> 1 | let a = #;\n")
})
Expand Down
46 changes: 23 additions & 23 deletions js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,29 @@ func TestConsoleContext(t *testing.T) {
assert.Equal(t, "b", entry.Message)
}
}
func getSimpleRunner(path, data string) (*Runner, error) {
return getSimpleRunnerWithFileFs(path, data, afero.NewMemMapFs())
}

func getSimpleRunnerWithOptions(path, data string, options lib.RuntimeOptions) (*Runner, error) {
return New(&loader.SourceData{
URL: &url.URL{Path: path, Scheme: "file"},
Data: []byte(data),
}, map[string]afero.Fs{
"file": afero.NewMemMapFs(),
"https": afero.NewMemMapFs()},
options)
func getSimpleRunner(filename, data string, opts ...interface{}) (*Runner, error) {
var (
fs = afero.NewMemMapFs()
rtOpts = lib.RuntimeOptions{CompatibilityMode: null.NewString("base", true)}
)
for _, o := range opts {
switch opt := o.(type) {
case afero.Fs:
fs = opt
case lib.RuntimeOptions:
rtOpts = opt
}
}
return New(
&loader.SourceData{
URL: &url.URL{Path: filename, Scheme: "file"},
Data: []byte(data),
},
map[string]afero.Fs{"file": fs, "https": afero.NewMemMapFs()},
rtOpts,
)
}

func getSimpleRunnerWithFileFs(path, data string, fileFs afero.Fs) (*Runner, error) {
return New(&loader.SourceData{
URL: &url.URL{Path: path, Scheme: "file"},
Data: []byte(data),
}, map[string]afero.Fs{
"file": fileFs,
"https": afero.NewMemMapFs()},
lib.RuntimeOptions{})
}
func TestConsole(t *testing.T) {
levels := map[string]logrus.Level{
"log": logrus.InfoLevel,
Expand All @@ -117,7 +117,7 @@ func TestConsole(t *testing.T) {
args, result := args, result
t.Run(args, func(t *testing.T) {
r, err := getSimpleRunner("/script.js", fmt.Sprintf(
`export default function() { console.%s(%s); }`,
`exports.default = function() { console.%s(%s); }`,
name, args,
))
assert.NoError(t, err)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestFileConsole(t *testing.T) {
}
r, err := getSimpleRunner("/script",
fmt.Sprintf(
`export default function() { console.%s(%s); }`,
`exports.default = function() { console.%s(%s); }`,
name, args,
))
assert.NoError(t, err)
Expand Down
31 changes: 16 additions & 15 deletions js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/spf13/afero"
"github.com/stretchr/testify/require"
null "gopkg.in/guregu/null.v3"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/testutils/httpmultibin"
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestLoadOnceGlobalVars(t *testing.T) {
return c.C();
}
`), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", `
r1, err := getSimpleRunner("/script.js", `
import { A } from "./A.js";
import { B } from "./B.js";

Expand All @@ -99,7 +100,7 @@ func TestLoadOnceGlobalVars(t *testing.T) {
throw new Error("A() != B() (" + A() + ") != (" + B() + ")");
}
}
`, fs)
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down Expand Up @@ -136,7 +137,7 @@ func TestLoadExportsIsUsableInModule(t *testing.T) {
return exports.A() + "B";
}
`), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", `
r1, err := getSimpleRunner("/script.js", `
import { A, B } from "./A.js";

export default function(data) {
Expand All @@ -148,7 +149,7 @@ func TestLoadExportsIsUsableInModule(t *testing.T) {
throw new Error("wrong value of B() " + B());
}
}
`, fs)
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestLoadDoesntBreakHTTPGet(t *testing.T) {
return http.get("HTTPBIN_URL/get");
}
`)), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", `
r1, err := getSimpleRunner("/script.js", `
import { A } from "./A.js";

export default function(data) {
Expand All @@ -194,7 +195,7 @@ func TestLoadDoesntBreakHTTPGet(t *testing.T) {
throw new Error("wrong status "+ resp.status);
}
}
`, fs)
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

require.NoError(t, r1.SetOptions(lib.Options{Hosts: tb.Dialer.Hosts}))
Expand Down Expand Up @@ -228,7 +229,7 @@ func TestLoadGlobalVarsAreNotSharedBetweenVUs(t *testing.T) {
return globalVar;
}
`), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", `
r1, err := getSimpleRunner("/script.js", `
import { A } from "./A.js";

export default function(data) {
Expand All @@ -239,7 +240,7 @@ func TestLoadGlobalVarsAreNotSharedBetweenVUs(t *testing.T) {
throw new Error("wrong value of a " + a);
}
}
`, fs)
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down Expand Up @@ -302,7 +303,7 @@ func TestLoadCycle(t *testing.T) {
`), os.ModePerm))
data, err := afero.ReadFile(fs, "/main.js")
require.NoError(t, err)
r1, err := getSimpleRunnerWithFileFs("/main.js", string(data), fs)
r1, err := getSimpleRunner("/main.js", string(data), fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down Expand Up @@ -351,7 +352,7 @@ func TestLoadCycleBinding(t *testing.T) {
}
`), os.ModePerm))

r1, err := getSimpleRunnerWithFileFs("/main.js", `
r1, err := getSimpleRunner("/main.js", `
import {foo} from './a.js';
import {bar} from './b.js';
export default function() {
Expand All @@ -364,7 +365,7 @@ func TestLoadCycleBinding(t *testing.T) {
throw new Error("Wrong value of bar() "+ barMessage);
}
}
`, fs)
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down Expand Up @@ -410,7 +411,7 @@ func TestBrowserified(t *testing.T) {
});
`), os.ModePerm))

r1, err := getSimpleRunnerWithFileFs("/script.js", `
r1, err := getSimpleRunner("/script.js", `
import {alpha, bravo } from "./browserified.js";

export default function(data) {
Expand All @@ -428,7 +429,7 @@ func TestBrowserified(t *testing.T) {
throw new Error("bravo.B() != 'b' (" + bravo.B() + ") != 'b'");
}
}
`, fs)
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down Expand Up @@ -459,13 +460,13 @@ func TestLoadingUnexistingModuleDoesntPanic(t *testing.T) {
} catch (err) {
b = "correct";
}
export default function() {
exports.default = function() {
if (b != "correct") {
throw new Error("wrong b "+ JSON.stringify(b));
}
}`
require.NoError(t, afero.WriteFile(fs, "/script.js", []byte(data), 0644))
r1, err := getSimpleRunnerWithFileFs("/script.js", data, fs)
r1, err := getSimpleRunner("/script.js", data, fs)
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down
Loading