1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

Add tests for urls

This commit is contained in:
Ryan Dahl 2018-05-25 17:01:18 -04:00
parent 0fe6ab91df
commit 59534fb9e3
3 changed files with 68 additions and 15 deletions

View file

@ -12,6 +12,8 @@ import (
"testing" "testing"
) )
var denoFn string
// Some tests require an HTTP server. We start one here. // Some tests require an HTTP server. We start one here.
// Because we process tests synchronously in this program we must run // Because we process tests synchronously in this program we must run
// the server as a subprocess. // the server as a subprocess.
@ -45,7 +47,7 @@ func listTestFiles() []string {
return out return out
} }
func checkOutput(t *testing.T, outFile string, denoFn string) { func checkOutput(t *testing.T, outFile string) {
outFile = path.Join("testdata", outFile) outFile = path.Join("testdata", outFile)
jsFile := strings.TrimSuffix(outFile, ".out") jsFile := strings.TrimSuffix(outFile, ".out")
@ -54,19 +56,10 @@ func checkOutput(t *testing.T, outFile string, denoFn string) {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
dir, err := ioutil.TempDir("", "TestIntegration") actual, _, err := deno(jsFile)
if err != nil {
panic(err)
}
cmd := exec.Command(denoFn, "--cachedir="+dir, jsFile)
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
actual := out.Bytes()
if bytes.Compare(actual, expected) != 0 { if bytes.Compare(actual, expected) != 0 {
t.Fatalf(`Actual output does not match expected. t.Fatalf(`Actual output does not match expected.
-----Actual------------------- -----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() startServer()
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err != nil { if err != nil {
panic(err) panic(err)
} }
denoFn := path.Join(cwd, "deno") denoFn = path.Join(cwd, "deno")
}
func TestIntegration(t *testing.T) {
integrationTestSetup()
outFiles := listTestFiles() outFiles := listTestFiles()
for _, outFile := range outFiles { for _, outFile := range outFiles {
t.Run(outFile, func(t *testing.T) { 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)
}
}

View file

@ -125,10 +125,10 @@ export function resolveModule(
moduleSpecifier: string, moduleSpecifier: string,
containingFile: string containingFile: string
): FileModule { ): FileModule {
util.log("resolveModule", { moduleSpecifier, containingFile });
util.assert(moduleSpecifier != null && moduleSpecifier.length > 0); util.assert(moduleSpecifier != null && moduleSpecifier.length > 0);
// We ask golang to sourceCodeFetch. It will load the sourceCode and if // We ask golang to sourceCodeFetch. It will load the sourceCode and if
// there is any outputCode cached, it will return that as well. // there is any outputCode cached, it will return that as well.
util.log("resolveModule", { moduleSpecifier, containingFile });
const { filename, sourceCode, outputCode } = os.sourceCodeFetch( const { filename, sourceCode, outputCode } = os.sourceCodeFetch(
moduleSpecifier, moduleSpecifier,
containingFile containingFile

12
util.go
View file

@ -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) { func assert(cond bool, msg string) {
if !cond { if !cond {
panic(msg) panic(msg)