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 2 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
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