summaryrefslogtreecommitdiff
path: root/integration_test.go
blob: 5b96c0abc17ad964d9ea6f60579c1e73ff4d08a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
package deno

import (
	"bytes"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	"os/exec"
	"path"
	"strings"
	"testing"
)

var denoFn string

// Some tests require an HTTP server. We start one here.
// Note that "localhost:4545" is hardcoded into the tests at the moment,
// so if the server runs on a different port, it will fail.
func startServer() {
	l, err := net.Listen("tcp", ":4545")
	if err != nil {
		panic(err)
	}
	rootHandler := http.FileServer(http.Dir("."))
	go func() {
		if err := http.Serve(l, rootHandler); err != nil {
			panic(err)
		}
	}()
}

func listTestFiles() []string {
	files, err := ioutil.ReadDir("testdata")
	if err != nil {
		panic(err)
	}
	out := make([]string, 0)
	for _, file := range files {
		fn := file.Name()
		if strings.HasSuffix(fn, ".out") {
			out = append(out, fn)
		}
	}
	return out
}

func checkOutput(t *testing.T, outFile string) {
	outFile = path.Join("testdata", outFile)
	jsFile := strings.TrimSuffix(outFile, ".out")

	expected, err := ioutil.ReadFile(outFile)
	if err != nil {
		t.Fatal(err.Error())
	}

	actual, _, err := deno(jsFile)
	if err != nil {
		t.Fatal(err.Error())
	}
	if bytes.Compare(actual, expected) != 0 {
		t.Fatalf(`Actual output does not match expected.
-----Actual-------------------
%s-----Expected-----------------
%s------------------------------`, string(actual), string(expected))
	}
}

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() {
	if denoFn == "" {
		startServer()
		cwd, err := os.Getwd()
		if err != nil {
			panic(err)
		}
		denoFn = path.Join(cwd, "deno")
	}
}

func TestIntegrationFiles(t *testing.T) {
	integrationTestSetup()
	outFiles := listTestFiles()
	for _, outFile := range outFiles {
		t.Run(outFile, func(t *testing.T) {
			checkOutput(t, outFile)
		})
	}
}

func TestIntegrationUrlArgs(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)
	}
}

func TestErrors(t *testing.T) {
	integrationTestSetup()

	_, _, err := deno("testdata/013_async_throw.ts")
	if err == nil {
		t.Fatalf("Expected error.")
	}

	_, _, err = deno("testdata/007_stack_trace.ts")
	if err == nil {
		t.Fatalf("Expected error.")
	}
}

func TestTestsTs(t *testing.T) {
	integrationTestSetup()
	cmd := exec.Command(denoFn, "tests.ts")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	if err != nil {
		t.Fatal(err.Error())
	}
}