diff --git a/integration_test.go b/integration_test.go index f1a60f499d..90aad560b5 100644 --- a/integration_test.go +++ b/integration_test.go @@ -12,6 +12,8 @@ import ( "testing" ) +var denoFn string + // Some tests require an HTTP server. We start one here. // Because we process tests synchronously in this program we must run // the server as a subprocess. @@ -45,7 +47,7 @@ func listTestFiles() []string { return out } -func checkOutput(t *testing.T, outFile string, denoFn string) { +func checkOutput(t *testing.T, outFile string) { outFile = path.Join("testdata", outFile) jsFile := strings.TrimSuffix(outFile, ".out") @@ -54,19 +56,10 @@ func checkOutput(t *testing.T, outFile string, denoFn string) { t.Fatal(err.Error()) } - dir, err := ioutil.TempDir("", "TestIntegration") - if err != nil { - panic(err) - } - - cmd := exec.Command(denoFn, "--cachedir="+dir, jsFile) - var out bytes.Buffer - cmd.Stdout = &out - err = cmd.Run() + actual, _, err := deno(jsFile) if err != nil { t.Fatal(err.Error()) } - actual := out.Bytes() if bytes.Compare(actual, expected) != 0 { t.Fatalf(`Actual output does not match expected. -----Actual------------------- @@ -75,17 +68,65 @@ func checkOutput(t *testing.T, outFile string, denoFn string) { } } -func TestIntegration(t *testing.T) { +func deno(inputFn string) (actual []byte, cachedir string, err error) { + cachedir, err = ioutil.TempDir("", "TestIntegration") + if err != nil { + panic(err) + } + + cmd := exec.Command(denoFn, "--cachedir="+cachedir, inputFn) + var out bytes.Buffer + cmd.Stdout = &out + err = cmd.Run() + if err == nil { + actual = out.Bytes() + } + return +} + +func integrationTestSetup() { startServer() cwd, err := os.Getwd() if err != nil { panic(err) } - denoFn := path.Join(cwd, "deno") + denoFn = path.Join(cwd, "deno") +} + +func TestIntegration(t *testing.T) { + integrationTestSetup() outFiles := listTestFiles() for _, outFile := range outFiles { t.Run(outFile, func(t *testing.T) { - checkOutput(t, outFile, denoFn) + checkOutput(t, outFile) }) } } + +func TestUrlArgs(t *testing.T) { + integrationTestSetup() + + // Using good port 4545 + _, cachedir, err := deno("http://localhost:4545/testdata/001_hello.js") + if err != nil { + t.Fatalf("Expected success. %s", err.Error()) + } + cacheFn := path.Join(cachedir, "src/localhost:4545/testdata/001_hello.js") + println("good cacheFn", cacheFn) + if !exists(cacheFn) { + t.Fatalf("Expected 200 at '%s'", cacheFn) + } + // TODO check output + + // Using bad port 4546 instead of 4545. + _, cachedir, err = deno("http://localhost:4546/testdata/001_hello.js") + if err == nil { + t.Fatalf("Expected 404. %s", err.Error()) + } + // Check that cache dir is emtpy. + cacheFn = path.Join(cachedir, "src/localhost:4546/testdata/001_hello.js") + println("bad cacheFn", cacheFn) + if exists(cacheFn) { + t.Fatalf("Expected 404 at '%s'", cacheFn) + } +} diff --git a/runtime.ts b/runtime.ts index 9613ab117f..e95ac4b589 100644 --- a/runtime.ts +++ b/runtime.ts @@ -125,10 +125,10 @@ export function resolveModule( moduleSpecifier: string, containingFile: string ): FileModule { + util.log("resolveModule", { moduleSpecifier, containingFile }); util.assert(moduleSpecifier != null && moduleSpecifier.length > 0); // We ask golang to sourceCodeFetch. It will load the sourceCode and if // there is any outputCode cached, it will return that as well. - util.log("resolveModule", { moduleSpecifier, containingFile }); const { filename, sourceCode, outputCode } = os.sourceCodeFetch( moduleSpecifier, containingFile diff --git a/util.go b/util.go index 1871156d32..5878d87b9e 100644 --- a/util.go +++ b/util.go @@ -13,6 +13,18 @@ func logDebug(format string, v ...interface{}) { } } +// exists returns whether the given file or directory exists or not +func exists(path string) bool { + _, err := os.Stat(path) + if err == nil { + return true + } + if os.IsNotExist(err) { + return false + } + panic(err) +} + func assert(cond bool, msg string) { if !cond { panic(msg)