Skip to content

Commit

Permalink
js: refactor path based tests to table tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jan 11, 2024
1 parent 0131c72 commit 9a8caa8
Showing 1 changed file with 126 additions and 138 deletions.
264 changes: 126 additions & 138 deletions js/path_resolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,153 +12,141 @@ import (

func TestOpenPathResolution(t *testing.T) {
t.Parallel()
t.Run("simple", func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, map[string]any{
"/path/to/data.txt": "data file",
"/path/scripts/script.js": `
export let data = open("../to/data.txt");
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
})
require.NoError(t, err)
b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/scripts/script.js"`, fs)
require.NoError(t, err)

_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})
testCases := map[string]struct {
fsMap map[string]any
}{
"simple": {
fsMap: map[string]any{
"/path/to/data.txt": "data file",
"/path/totally/different/directory/script.js": `
export let data = open("../../../to/data.txt");
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
"intermediate": {
fsMap: map[string]any{
"/path/to/data.txt": "data file",
"/path/another/script/script.js": `
module.exports = open("../../to/data.txt");
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../another/script/script.js")
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
"complex": {
fsMap: map[string]any{
"/path/to/data.txt": "data file",
"/path/another/script/script.js": `
// Here the path is relative to this module but to the one calling
module.exports = () => open("./../data.txt");
`,
"/path/to/script/script.js": `
module.exports = require("./../../another/script/script.js")();
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../to/script/script.js");
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
}

t.Run("intermediate", func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, map[string]any{
"/path/to/data.txt": "data file",
"/path/another/script/script.js": `
module.exports = open("../../to/data.txt");
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../another/script/script.js")
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
})
require.NoError(t, err)
b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/totally/different/directory/script.js"`, fs)
require.NoError(t, err)
for name, testCase := range testCases {
name, testCase := name, testCase

_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})
t.Run(name, func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, testCase.fsMap)
require.NoError(t, err)
b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/totally/different/directory/script.js"`, fs)
require.NoError(t, err)

t.Run("complex", func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, map[string]any{
"/path/to/data.txt": "data file",
"/path/another/script/script.js": `
module.exports = () => open("./../data.txt"); // Here the path is relative to this module but to the one calling
`,
"/path/to/script/script.js": `
module.exports = require("./../../another/script/script.js")();
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../to/script/script.js");
if (data != "data file") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})
require.NoError(t, err)
b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/totally/different/directory/script.js"`, fs)
require.NoError(t, err)

_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})
}
}

func TestRequirePathResolution(t *testing.T) {
t.Parallel()
t.Run("simple", func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, map[string]any{
"/path/to/data.js": "module.exports='export content'",
"/path/scripts/script.js": `
let data = require("../to/data.js");
if (data != "export content") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
})
require.NoError(t, err)
b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/scripts/script.js"`, fs)
require.NoError(t, err)

_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})

t.Run("intermediate", func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, map[string]any{
"/path/to/data.js": "module.exports='export content'",
"/path/another/script/script.js": `
module.exports = require("../../to/data.js");
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../another/script/script.js")
if (data != "export content") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
})
require.NoError(t, err)
b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/totally/different/directory/script.js"`, fs)
require.NoError(t, err)

_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})

t.Run("complex", func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, map[string]any{
"/path/to/data.js": "module.exports='export content'",
"/path/another/script/script.js": `
module.exports = () => require("./../data.js"); // Here the path is relative to this module but to the one calling
`,
"/path/to/script/script.js": `
module.exports = require("./../../another/script/script.js")();
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../to/script/script.js");
if (data != "export content") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
testCases := map[string]struct {
fsMap map[string]any
}{
"simple": {
fsMap: map[string]any{
"/path/to/data.js": "module.exports='export content'",
"/path/totally/different/directory/script.js": `
let data = require("../../../to/data.js");
if (data != "export content") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
"intermediate": {
fsMap: map[string]any{
"/path/to/data.js": "module.exports='export content'",
"/path/another/script/script.js": `
module.exports = require("../../to/data.js");
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../another/script/script.js")
if (data != "export content") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
"complex": {
fsMap: map[string]any{
"/path/to/data.js": "module.exports='export content'",
"/path/another/script/script.js": `
// Here the path is relative to this module but to the one calling
module.exports = () => require("./../data.js");
`,
"/path/to/script/script.js": `
module.exports = require("./../../another/script/script.js")();
`,
"/path/totally/different/directory/script.js": `
let data = require("./../../../to/script/script.js");
if (data != "export content") {
throw new Error("wrong content " + data);
}
export default function() {}
`,
},
},
}
for name, testCase := range testCases {
name, testCase := name, testCase

t.Run(name, func(t *testing.T) {
t.Parallel()
fs := fsext.NewMemMapFs()
err := writeToFs(fs, testCase.fsMap)
require.NoError(t, err)
b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/totally/different/directory/script.js"`, fs)
require.NoError(t, err)

_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})
require.NoError(t, err)

b, err := getSimpleBundle(t, "/main.js", `export { default } from "/path/totally/different/directory/script.js"`, fs)
require.NoError(t, err)

_, err = b.Instantiate(context.Background(), 0)
require.NoError(t, err)
})
}
}

// writeToFs is a small helper to write a map of paths to contents to the filesystem provided.
Expand Down

0 comments on commit 9a8caa8

Please sign in to comment.