Skip to content

Commit

Permalink
js: DRY creation of a runner from archive in the tests
Browse files Browse the repository at this point in the history
This was copy-pasted around and similar to how getSimpleRunner makes it
hard to refactor if you add new fields that need to be around always.
  • Loading branch information
mstoykov committed Sep 10, 2024
1 parent 741610b commit 39bddb2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 312 deletions.
31 changes: 31 additions & 0 deletions js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,37 @@ func getSimpleRunner(tb testing.TB, filename, data string, opts ...interface{})
)
}

func getSimpleArchiveRunner(tb testing.TB, arc *lib.Archive, opts ...interface{}) (*Runner, error) {
var (
rtOpts = lib.RuntimeOptions{CompatibilityMode: null.NewString("base", true)}
logger = testutils.NewLogger(tb)
fsResolvers = map[string]fsext.Fs{"file": fsext.NewMemMapFs(), "https": fsext.NewMemMapFs()}
)
for _, o := range opts {
switch opt := o.(type) {
case fsext.Fs:
fsResolvers["file"] = opt
case map[string]fsext.Fs:
fsResolvers = opt
case lib.RuntimeOptions:
rtOpts = opt
case logrus.FieldLogger:
logger = opt
default:
tb.Fatalf("unknown test option %q", opt)
}
}
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
return NewFromArchive(
&lib.TestPreInitState{
Logger: logger,
RuntimeOptions: rtOpts,
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
}

// TODO: remove the need for this function, see https://github.com/grafana/k6/issues/2968
//
//nolint:forbidigo
Expand Down
113 changes: 13 additions & 100 deletions js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/httpmultibin"
"go.k6.io/k6/metrics"
)
Expand Down Expand Up @@ -89,13 +88,7 @@ func TestLoadOnceGlobalVars(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -146,14 +139,7 @@ func TestLoadExportsIsntUsableInModule(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -200,14 +186,7 @@ func TestLoadDoesntBreakHTTPGet(t *testing.T) {

require.NoError(t, r1.SetOptions(lib.Options{Hosts: types.NullHosts{Trie: tb.Dialer.Hosts}}))
arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -252,14 +231,7 @@ func TestLoadGlobalVarsAreNotSharedBetweenVUs(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -321,14 +293,7 @@ func TestLoadCycle(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -390,14 +355,7 @@ func TestLoadCycleBinding(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -461,14 +419,7 @@ func TestBrowserified(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -511,14 +462,7 @@ func TestLoadingUnexistingModuleDoesntPanic(t *testing.T) {
require.NoError(t, arc.Write(buf))
arc, err = lib.ReadArchive(buf)
require.NoError(t, err)
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -554,14 +498,7 @@ func TestLoadingSourceMapsDoesntErrorOut(t *testing.T) {
arc, err = lib.ReadArchive(buf)
require.NoError(t, err)

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(
&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -614,13 +551,7 @@ func TestOptionsAreGloballyReadable(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
Expand Down Expand Up @@ -671,13 +602,7 @@ func TestOptionsAreNotGloballyWritable(t *testing.T) {
// here it exists
require.EqualValues(t, time.Minute*5, r1.GetOptions().MinIterationDuration.Duration)
arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
r2, err := getSimpleArchiveRunner(t, arc)
require.NoError(t, err)

require.EqualValues(t, time.Minute*5, r2.GetOptions().MinIterationDuration.Duration)
Expand Down Expand Up @@ -714,13 +639,7 @@ func TestStarImport(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
_, err = NewFromArchive(&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
_, err = getSimpleArchiveRunner(t, arc)
require.NoError(t, err)
}

Expand All @@ -739,12 +658,6 @@ func TestIndirectExportDefault(t *testing.T) {
require.NoError(t, err)

arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
_, err = NewFromArchive(&lib.TestPreInitState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
_, err = getSimpleArchiveRunner(t, arc)
require.NoError(t, err)
}
Loading

0 comments on commit 39bddb2

Please sign in to comment.