Skip to content

Commit

Permalink
dry getSimpleRunner the same way as getSimpleBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 30, 2020
1 parent 7dcbfe1 commit 284d3e3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
42 changes: 21 additions & 21 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{}
)
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 Down
16 changes: 8 additions & 8 deletions js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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 Down Expand Up @@ -136,7 +136,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 Down Expand Up @@ -185,7 +185,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 Down Expand Up @@ -228,7 +228,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 Down Expand Up @@ -302,7 +302,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)
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down Expand Up @@ -351,7 +351,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 Down Expand Up @@ -410,7 +410,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 Down Expand Up @@ -465,7 +465,7 @@ func TestLoadingUnexistingModuleDoesntPanic(t *testing.T) {
}
}`
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
10 changes: 5 additions & 5 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestOptionsSettingToScript(t *testing.T) {
throw new Error("expected teardownTimeout to be " + __ENV.expectedTeardownTimeout + " but it was " + options.teardownTimeout);
}
};`
r, err := getSimpleRunnerWithOptions("/script.js", data,
r, err := getSimpleRunner("/script.js", data,
lib.RuntimeOptions{Env: map[string]string{"expectedTeardownTimeout": "4s"}})
require.NoError(t, err)

Expand Down Expand Up @@ -192,7 +192,7 @@ func TestOptionsPropagationToScript(t *testing.T) {
};`

expScriptOptions := lib.Options{SetupTimeout: types.NullDurationFrom(1 * time.Second)}
r1, err := getSimpleRunnerWithOptions("/script.js", data,
r1, err := getSimpleRunner("/script.js", data,
lib.RuntimeOptions{Env: map[string]string{"expectedSetupTimeout": "1s"}})
require.NoError(t, err)
require.Equal(t, expScriptOptions, r1.GetOptions())
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestRunnerIntegrationImports(t *testing.T) {
for name, data := range testdata {
name, data := name, data
t.Run(name, func(t *testing.T) {
r1, err := getSimpleRunnerWithFileFs(data.filename, fmt.Sprintf(`
r1, err := getSimpleRunner(data.filename, fmt.Sprintf(`
import hi from "%s";
export default function() {
if (hi != "hi!") { throw new Error("incorrect value"); }
Expand Down Expand Up @@ -1441,7 +1441,7 @@ func TestArchiveRunningIntegrity(t *testing.T) {
`)
require.NoError(t, afero.WriteFile(fs, "/home/somebody/test.json", []byte(`42`), os.ModePerm))
require.NoError(t, afero.WriteFile(fs, "/script.js", []byte(data), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", data, fs)
r1, err := getSimpleRunner("/script.js", data, fs)
require.NoError(t, err)

buf := bytes.NewBuffer(nil)
Expand Down Expand Up @@ -1476,7 +1476,7 @@ func TestArchiveNotPanicking(t *testing.T) {

fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "/non/existent", []byte(`42`), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", tb.Replacer.Replace(`
r1, err := getSimpleRunner("/script.js", tb.Replacer.Replace(`
let fput = open("/non/existent");
export default function(data) {
}
Expand Down

0 comments on commit 284d3e3

Please sign in to comment.