Skip to content

Commit

Permalink
Add to showcase how currently options is accessible everywhere
Browse files Browse the repository at this point in the history
This is also true for anything defined in the main module exported or
not. It is just much easier to showcase with options particularly.

This IMO should never have been the case and should be fixed.

Additionally ESM in practice will "fix" this as modules all have their
own scopes. So it makes sense to maybe break this before we add ESM
support
  • Loading branch information
mstoykov committed Jul 13, 2022
1 parent aff3572 commit e5941d5
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"context"
"os"
"testing"
"time"

"github.com/spf13/afero"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -606,3 +607,88 @@ func TestLoadingSourceMapsDoesntErrorOut(t *testing.T) {
})
}
}

func TestShowcasingHowOptionsAreGlobalReadable(t *testing.T) {
t.Parallel()
fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "/A.js", []byte(`
export function A() {
// we can technically get a field set from outside of js this way
return options.someField;
}
`), os.ModePerm))
r1, err := getSimpleRunner(t, "/script.js", `
import { A } from "./A.js";
export let options = {
someField: "here is an option",
}
export default function(data) {
if (A() != "here is an option") {
throw "oops"
}
}
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

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

runners := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range runners {
r := r
t.Run(name, func(t *testing.T) {
t.Parallel()
ch := newDevNullSampleChannel()
defer close(ch)
initVU, err := r.NewVU(1, 1, ch)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vu := initVU.Activate(&lib.VUActivationParams{RunContext: ctx})
require.NoError(t, err)
err = vu.RunOnce()
require.NoError(t, err)
})
}
}

func TestShowcasingHowOptionsAreGlobalWritable(t *testing.T) {
t.Parallel()
fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "/A.js", []byte(`
export function A() {
// this requires that this is defined
options.minIterationDuration = "1h"
}
`), os.ModePerm))
r1, err := getSimpleRunner(t, "/script.js", `
import {A} from "/A.js"
export let options = {minIterationDuration: "5m"}
export default () =>{}
A()
`, fs, lib.RuntimeOptions{CompatibilityMode: null.StringFrom("extended")})
require.NoError(t, err)

// here it exists
require.EqualValues(t, time.Hour, r1.GetOptions().MinIterationDuration.Duration)
arc := r1.MakeArchive()
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
r2, err := NewFromArchive(&lib.RuntimeState{
Logger: testutils.NewLogger(t),
BuiltinMetrics: builtinMetrics,
Registry: registry,
}, arc)
require.NoError(t, err)

require.EqualValues(t, time.Hour, r2.GetOptions().MinIterationDuration.Duration)
}

0 comments on commit e5941d5

Please sign in to comment.