-
Notifications
You must be signed in to change notification settings - Fork 119
/
live_reload_test.go
171 lines (141 loc) · 4.33 KB
/
live_reload_test.go
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
160
161
162
163
164
165
166
167
168
169
170
171
package live_reload
import (
"net/http"
"net/url"
"io/ioutil"
"reflect"
"runtime/debug"
"strings"
"testing"
"time"
bazel "github.com/bazelbuild/bazel-integration-testing/go"
"github.com/bazelbuild/bazel-watcher/e2e"
"github.com/gorilla/websocket"
)
type liveReloadHello struct {
Command string `json:"command"`
Protocols []string `json:"protocols"`
}
const printLivereload = `printf "Live reload url: ${IBAZEL_LIVERELOAD_URL}"`
func must(t *testing.T, e error) {
if e != nil {
t.Errorf("Error: %s", e)
debug.PrintStack()
}
}
func assertNotEqual(t *testing.T, want, got interface{}, msg string) {
if reflect.DeepEqual(want, got) {
t.Errorf("Wanted %s, got %s. %s", want, got, msg)
debug.PrintStack()
}
}
func assertEqual(t *testing.T, want, got interface{}, msg string) {
if !reflect.DeepEqual(want, got) {
t.Errorf("Wanted [%v], got [%v]. %s", want, got, msg)
debug.PrintStack()
}
}
func verify(t *testing.T, conn *websocket.Conn, expected interface{}) {
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
_, v, err := conn.ReadMessage()
m := strings.TrimSpace(string(v))
t.Logf("v: %s, err: %s\n", m, err)
if err != nil {
t.Errorf("Error ReadJSONing from websocket: %s", err)
}
assertEqual(t, expected, m, "Expected response match")
}
func TestLiveReload(t *testing.T) {
b, err := bazel.New()
if err != nil {
t.Fatal(err)
}
must(t, b.ScratchFile("WORKSPACE", ""))
must(t, b.ScratchFileWithMode("test.sh", printLivereload, 0777))
must(t, b.ScratchFile("test.txt", "1"))
must(t, b.ScratchFile("BUILD", `
sh_binary(
name = "live_reload",
srcs = ["test.sh"],
data = ["test.txt"],
tags = ["ibazel_live_reload"],
)
`))
ibazel := e2e.NewIBazelTester(t, b)
ibazel.Run("//:live_reload")
defer ibazel.Kill()
ibazel.ExpectOutput("Live reload url: http://.+:\\d+")
out := ibazel.GetOutput()
t.Logf("Output: '%s'", out)
if out == "" {
t.Fatal("Output was empty. Expected at least some output")
}
jsUrl := out[len("Live reload url: "):]
t.Logf("Livereload URL: '%s'", jsUrl)
url, err := url.ParseRequestURI(jsUrl)
if err != nil {
t.Error(err)
}
client := &http.Client{}
resp, err := client.Get(jsUrl)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
bodyString := string(body)
expectedStart := "(function e(t,n,r)"
actualStart := bodyString[0:len(expectedStart)]
if actualStart != expectedStart {
t.Errorf("Expected js to start with \"%s\" but got \"%s\".", expectedStart, actualStart)
}
expectedEnd := "},{}]},{},[8]);"
actualEnd := bodyString[len(bodyString)-len(expectedEnd):]
if actualEnd != expectedEnd {
t.Errorf("Expected js to end with \"%s\" but got \"%s\".", expectedEnd, actualEnd)
}
wsUrl := "ws://" + url.Hostname() + ":" + url.Port() + "/livereload"
t.Logf("wsUrl: %s", wsUrl)
conn, _, err := websocket.DefaultDialer.Dial(wsUrl, map[string][]string{})
if err != nil {
t.Error(err)
}
// Send the hello message to the client.
hello := liveReloadHello{
Command: "hello",
Protocols: []string{"http://livereload.com/protocols/official-7"},
}
if err = conn.WriteJSON(hello); err != nil {
t.Error(err)
}
// Verify the hello message
verify(t, conn, `{"command":"hello","protocols":["http://livereload.com/protocols/official-7","http://livereload.com/protocols/official-8","http://livereload.com/protocols/official-9","http://livereload.com/protocols/2.x-origin-version-negotiation","http://livereload.com/protocols/2.x-remote-control"],"serverName":"live reload"}`)
must(t, b.ScratchFile("test.txt", "2"))
ibazel.ExpectOutput("Live reload url: http://.+:\\d+")
verify(t, conn, `{"command":"reload","path":"reload","liveCSS":true}`)
must(t, b.ScratchFile("test.txt", "3"))
ibazel.ExpectOutput("Live reload url: http://.+:\\d+")
verify(t, conn, `{"command":"reload","path":"reload","liveCSS":true}`)
}
func TestNoLiveReload(t *testing.T) {
b, err := bazel.New()
if err != nil {
t.Fatal(err)
}
must(t, b.ScratchFile("WORKSPACE", ""))
must(t, b.ScratchFileWithMode("test.sh", printLivereload, 0777))
must(t, b.ScratchFile("BUILD", `
sh_binary(
name = "no_live_reload",
srcs = ["test.sh"],
)
`))
ibazel := e2e.NewIBazelTester(t, b)
ibazel.Run("//:no_live_reload")
defer ibazel.Kill()
// Expect there to not be a url since this is the negative test case.
ibazel.ExpectOutput("Live reload url: $")
}