From 08b327bf3afd7ad180fe807c4292c23cc6942b56 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 23 May 2018 11:09:38 -0400 Subject: Move ResolveModule to os.go --- main.go | 22 ---------------------- main_test.go | 49 ------------------------------------------------- os.go | 22 ++++++++++++++++++++++ os_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 71 deletions(-) delete mode 100644 main_test.go create mode 100644 os_test.go diff --git a/main.go b/main.go index 2c669e322..32f011e91 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,7 @@ import ( "github.com/golang/protobuf/proto" "github.com/ry/v8worker2" "log" - "net/url" "os" - "path" "runtime/pprof" ) @@ -21,26 +19,6 @@ var DenoDir string var CompileDir string var SrcDir string -func ResolveModule(moduleSpecifier string, containingFile string) ( - moduleName string, filename string, err error) { - moduleUrl, err := url.Parse(moduleSpecifier) - if err != nil { - return - } - baseUrl, err := url.Parse(containingFile) - if err != nil { - return - } - resolved := baseUrl.ResolveReference(moduleUrl) - moduleName = resolved.String() - if moduleUrl.IsAbs() { - filename = path.Join(SrcDir, resolved.Host, resolved.Path) - } else { - filename = resolved.Path - } - return -} - func stringAsset(path string) string { data, err := Asset("dist/" + path) check(err) diff --git a/main_test.go b/main_test.go deleted file mode 100644 index ac94b5a3c..000000000 --- a/main_test.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "path" - "testing" -) - -func AssertEqual(t *testing.T, actual string, expected string) { - if actual != expected { - t.Fatalf("not equal <<%s>> <<%s>>", actual, expected) - } -} - -func TestResolveModule(t *testing.T) { - moduleName, filename, err := ResolveModule( - "http://localhost:4545/testdata/subdir/print_hello.ts", - "/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts") - if err != nil { - t.Fatalf(err.Error()) - } - AssertEqual(t, moduleName, - "http://localhost:4545/testdata/subdir/print_hello.ts") - AssertEqual(t, filename, - path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts")) - - moduleName, filename, err = ResolveModule( - "./subdir/print_hello.ts", - "/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts") - if err != nil { - t.Fatalf(err.Error()) - } - AssertEqual(t, moduleName, - "/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts") - AssertEqual(t, filename, - "/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts") - - // In the case where the containingFile is a directory (indicated with a - // trailing slash) - moduleName, filename, err = ResolveModule( - "testdata/001_hello.js", - "/Users/rld/go/src/github.com/ry/deno/") - if err != nil { - t.Fatalf(err.Error()) - } - AssertEqual(t, moduleName, - "/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js") - AssertEqual(t, filename, - "/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js") -} diff --git a/os.go b/os.go index 59c24ccdf..44ec2ba9c 100644 --- a/os.go +++ b/os.go @@ -3,7 +3,9 @@ package main import ( "github.com/golang/protobuf/proto" "io/ioutil" + "net/url" "os" + "path" "strings" ) @@ -31,6 +33,26 @@ func InitOS() { }) } +func ResolveModule(moduleSpecifier string, containingFile string) ( + moduleName string, filename string, err error) { + moduleUrl, err := url.Parse(moduleSpecifier) + if err != nil { + return + } + baseUrl, err := url.Parse(containingFile) + if err != nil { + return + } + resolved := baseUrl.ResolveReference(moduleUrl) + moduleName = resolved.String() + if moduleUrl.IsAbs() { + filename = path.Join(SrcDir, resolved.Host, resolved.Path) + } else { + filename = resolved.Path + } + return +} + func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out []byte) { assert(moduleSpecifier != "", "moduleSpecifier shouldn't be empty") res := &Msg{} diff --git a/os_test.go b/os_test.go new file mode 100644 index 000000000..ac94b5a3c --- /dev/null +++ b/os_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "path" + "testing" +) + +func AssertEqual(t *testing.T, actual string, expected string) { + if actual != expected { + t.Fatalf("not equal <<%s>> <<%s>>", actual, expected) + } +} + +func TestResolveModule(t *testing.T) { + moduleName, filename, err := ResolveModule( + "http://localhost:4545/testdata/subdir/print_hello.ts", + "/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts") + if err != nil { + t.Fatalf(err.Error()) + } + AssertEqual(t, moduleName, + "http://localhost:4545/testdata/subdir/print_hello.ts") + AssertEqual(t, filename, + path.Join(SrcDir, "localhost:4545/testdata/subdir/print_hello.ts")) + + moduleName, filename, err = ResolveModule( + "./subdir/print_hello.ts", + "/Users/rld/go/src/github.com/ry/deno/testdata/006_url_imports.ts") + if err != nil { + t.Fatalf(err.Error()) + } + AssertEqual(t, moduleName, + "/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts") + AssertEqual(t, filename, + "/Users/rld/go/src/github.com/ry/deno/testdata/subdir/print_hello.ts") + + // In the case where the containingFile is a directory (indicated with a + // trailing slash) + moduleName, filename, err = ResolveModule( + "testdata/001_hello.js", + "/Users/rld/go/src/github.com/ry/deno/") + if err != nil { + t.Fatalf(err.Error()) + } + AssertEqual(t, moduleName, + "/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js") + AssertEqual(t, filename, + "/Users/rld/go/src/github.com/ry/deno/testdata/001_hello.js") +} -- cgit v1.2.3