mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
Add tests for urls
This commit is contained in:
parent
0fe6ab91df
commit
59534fb9e3
3 changed files with 68 additions and 15 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
12
util.go
12
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)
|
||||
|
|
Loading…
Reference in a new issue