Skip to content

Commit 94e5081

Browse files
authored
Added Windows paths support when requiring local files (dop251#61)
* added os specific abs path check for requiring local files * added .\ and ..\ Windows only checks
1 parent 9ba67d7 commit 94e5081

File tree

2 files changed

+76
-13
lines changed

2 files changed

+76
-13
lines changed

require/module_test.go

+57-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"io"
88
"os"
99
"path"
10+
"path/filepath"
11+
"runtime"
1012
"testing"
1113

1214
js "github.com/dop251/goja"
@@ -182,23 +184,68 @@ func TestRequireRegistryNativeModule(t *testing.T) {
182184
}
183185

184186
func TestRequire(t *testing.T) {
187+
absPath, err := filepath.Abs("./testdata/m.js")
188+
if err != nil {
189+
t.Fatal(err)
190+
}
191+
192+
isWindows := runtime.GOOS == "windows"
193+
194+
tests := []struct {
195+
path string
196+
ok bool
197+
}{
198+
{
199+
"./testdata/m.js",
200+
true,
201+
},
202+
{
203+
"../require/testdata/m.js",
204+
true,
205+
},
206+
{
207+
absPath,
208+
true,
209+
},
210+
{
211+
`.\testdata\m.js`,
212+
isWindows,
213+
},
214+
{
215+
`..\require\testdata\m.js`,
216+
isWindows,
217+
},
218+
}
219+
185220
const SCRIPT = `
186-
var m = require("./testdata/m.js");
221+
var m = require(testPath);
187222
m.test();
188223
`
189224

190-
vm := js.New()
225+
for _, test := range tests {
226+
t.Run(test.path, func(t *testing.T) {
227+
vm := js.New()
228+
vm.Set("testPath", test.path)
191229

192-
registry := new(Registry)
193-
registry.Enable(vm)
230+
registry := new(Registry)
231+
registry.Enable(vm)
194232

195-
v, err := vm.RunString(SCRIPT)
196-
if err != nil {
197-
t.Fatal(err)
198-
}
233+
v, err := vm.RunString(SCRIPT)
199234

200-
if !v.StrictEquals(vm.ToValue("passed")) {
201-
t.Fatalf("Unexpected result: %v", v)
235+
ok := err == nil
236+
237+
if ok != test.ok {
238+
t.Fatalf("Expected ok to be %v, got %v (%v)", test.ok, ok, err)
239+
}
240+
241+
if !ok {
242+
return
243+
}
244+
245+
if !v.StrictEquals(vm.ToValue("passed")) {
246+
t.Fatalf("Unexpected result: %v", v)
247+
}
248+
})
202249
}
203250
}
204251

require/resolve.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"encoding/json"
55
"errors"
66
"path"
7+
"path/filepath"
8+
"runtime"
79
"strings"
810

911
js "github.com/dop251/goja"
@@ -28,9 +30,7 @@ func (r *RequireModule) resolve(modpath string) (module *js.Object, err error) {
2830
}
2931

3032
p := path.Join(start, modpath)
31-
if strings.HasPrefix(origPath, "./") ||
32-
strings.HasPrefix(origPath, "/") || strings.HasPrefix(origPath, "../") ||
33-
origPath == "." || origPath == ".." {
33+
if isFileOrDirectoryPath(origPath) {
3434
if module = r.modules[p]; module != nil {
3535
return
3636
}
@@ -258,3 +258,19 @@ func (r *RequireModule) loadModuleFile(path string, jsModule *js.Object) error {
258258

259259
return nil
260260
}
261+
262+
func isFileOrDirectoryPath(path string) bool {
263+
result := path == "." || path == ".." ||
264+
strings.HasPrefix(path, "/") ||
265+
strings.HasPrefix(path, "./") ||
266+
strings.HasPrefix(path, "../")
267+
268+
if runtime.GOOS == "windows" {
269+
result = result ||
270+
strings.HasPrefix(path, `.\`) ||
271+
strings.HasPrefix(path, `..\`) ||
272+
filepath.IsAbs(path)
273+
}
274+
275+
return result
276+
}

0 commit comments

Comments
 (0)