forked from kulasama/goscript
/
goplay_test.go
348 lines (289 loc) · 8.23 KB
/
goplay_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2013 JamesClonk
package main
import (
"bytes"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
)
func init() {
// $GOPATH/bin should be part of $PATH
path := os.Getenv("PATH")
gopathBin := filepath.Join(build.Default.GOPATH, "bin")
if !strings.Contains(path, gopathBin) {
log.Fatalf("PATH does not contain GOPATH/bin\n$GOPATH/bin: [%s]\n$PATH: [%s]\n", gopathBin, path)
}
// Compile and install goplay in current $GOPATH/bin
if goplayBin, err := exec.LookPath("goplay"); err != nil {
if err.(*exec.Error).Err == exec.ErrNotFound {
install()
} else {
log.Fatal(err)
}
} else {
if GetTime("goplay.go").After(GetTime(goplayBin)) {
install()
}
}
// Change into "testdata" directory
if pwd, err := os.Getwd(); err != nil {
log.Fatal(err)
} else if filepath.Base(pwd) != "testdata" {
if err := os.Chdir("testdata"); err != nil {
log.Fatal(err)
}
}
}
func install() {
if err := exec.Command("go", "install").Run(); err != nil {
log.Fatal(err)
} else {
log.Println("Installed goplay...")
}
}
func createFile(t *testing.T, filename string) *os.File {
file, err := os.Create(filename)
if err != nil {
t.Fatal(err)
}
return file
}
func removeFile(t *testing.T, filename string) {
if err := os.Remove(filename); err != nil {
t.Fatal(err)
}
}
func expected(t *testing.T, test string, output interface{}, expected interface{}) {
if output != expected {
t.Errorf("output of %s not as expected, was [%v], but should be [%v]", test, output, expected)
}
}
func hashbangCheck(t *testing.T, filename string) bool {
file, err := os.Open(filename)
if err != nil {
t.Fatalf("Could not open file: %s", err)
}
defer file.Close()
return CheckForHashbang(file)
}
func modifyFile(t *testing.T, line string, lineNum int, filename string) {
bytes, err := ioutil.ReadFile(filename)
if err != nil {
t.Fatal(err)
}
lines := strings.Split(string(bytes), "\n")
lines[lineNum] = line
if err := ioutil.WriteFile(filename, []byte(strings.Join(lines, "\n")), 0664); err != nil {
t.Fatal(err)
}
}
func TestCheckForHashbang(t *testing.T) {
// output.go does not contain a hashbang
if hashbangCheck(t, "output.go") {
t.Error("Unexpected hashbang found")
}
// parameters.go contains a hashbang
if !hashbangCheck(t, "parameters.go") {
t.Error("Hashbang not found")
}
}
func TestCommentHashbang(t *testing.T) {
filename := "hashbang.go"
// hashbang.go should contain a hashbang
if !hashbangCheck(t, filename) {
t.Error("Hashbang not found")
}
file, err := os.OpenFile(filename, os.O_RDWR, 0)
if err != nil {
t.Fatalf("Could not open file: %s", err)
}
defer file.Close()
CommentHashbang(file, "//")
if hashbangCheck(t, filename) {
t.Error("Unexpected hashbang found")
}
CommentHashbang(file, "#!")
if !hashbangCheck(t, filename) {
t.Error("Hashbang not found")
}
}
func TestExist(t *testing.T) {
if !Exist("parameters.go") {
t.Errorf("File does not exist: [%s] ", "parameters.go")
}
if Exist("TestNotExist.test") {
t.Errorf("File should not exist: [%s] ", "TestNotExist.test")
}
}
func TestGetTime(t *testing.T) {
filenameA := "TestGetTime_A.test"
filenameB := "TestGetTime_B.test"
createFile(t, filenameA)
defer removeFile(t, filenameA)
// Sleep inbetween
time.Sleep(11 * time.Millisecond)
createFile(t, filenameB)
defer removeFile(t, filenameB)
if !GetTime(filenameA).Before(GetTime(filenameB)) {
t.Errorf("ModTime of [%s] should be older than [%s]", filenameA, filenameB)
}
}
func TestGetSubdirectories(t *testing.T) {
subdirs := GetSubdirectories("./")
if subdirs[0] != "build" && subdirs[1] != "config" && subdirs[2] != "watch" {
t.Errorf("Subdirectories should be [[build config watch]], but got [%v]", subdirs)
}
}
func TestCompileBinary(t *testing.T) {
scriptFilename := "output.go"
binaryFilename := "TestCompileBinary_output"
if Exist(binaryFilename) {
removeFile(t, binaryFilename)
}
scriptPath, err := filepath.Abs(scriptFilename)
if err != nil {
t.Fatal(err)
}
binaryPath, err := filepath.Abs(binaryFilename)
if err != nil {
t.Fatal(err)
}
CompileBinary(scriptPath, binaryPath, false)
if !Exist(binaryFilename) {
t.Fatalf("Compiled binary does not exist: [%s]", binaryFilename)
}
defer removeFile(t, binaryFilename)
out, err := exec.Command("./" + binaryFilename).Output()
if err != nil {
t.Fatal(err)
}
expected(t, binaryFilename, string(out), "The night is all magic\n")
}
func TestCompleteBuild(t *testing.T) {
scriptFilename := "build/builder.go"
binaryFilename := "build/TestCompleteBuildBinary_builder"
if Exist(binaryFilename) {
removeFile(t, binaryFilename)
}
out, err := exec.Command("./" + scriptFilename).Output()
if err != nil {
t.Fatal(err)
}
expected(t, "build/builder.go", string(out), "Build!\n")
scriptPath, err := filepath.Abs(scriptFilename)
if err != nil {
t.Fatal(err)
}
binaryPath, err := filepath.Abs(binaryFilename)
if err != nil {
t.Fatal(err)
}
CompileBinary(scriptPath, binaryPath, true)
if !Exist(binaryFilename) {
t.Fatalf("Compiled binary does not exist: [%s]", binaryFilename)
}
defer removeFile(t, binaryFilename)
out, err = exec.Command("./" + binaryFilename).Output()
if err != nil {
t.Fatal(err)
}
expected(t, "build/builder.go", string(out), "Build!\n")
}
func TestStartBinary(t *testing.T) {
filename := "TestStartBinary.test"
if Exist(filename) {
removeFile(t, filename)
}
cmd := StartBinary("goplay", []string{"write.go", filename})
if err := cmd.Wait(); err != nil {
t.Fatal(err)
}
defer removeFile(t, filename)
data, err := ioutil.ReadFile(filename)
if err != nil {
t.Error(err)
}
expected(t, "write.go", string(data), "Hello, World!")
}
func TestNoExtension(t *testing.T) {
out, err := exec.Command("./no_extension").Output()
if err != nil {
t.Fatal(err)
}
expected(t, "no_extension", string(out), "I'm extensionless")
}
func TestInput(t *testing.T) {
var buffer bytes.Buffer
cmd := exec.Command("./input.go")
cmd.Stdin = strings.NewReader("Hello, World!\n")
cmd.Stdout = &buffer
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
expected(t, "input.go", buffer.String(), "(Write and press Enter to finish)\nHello, World!\n")
}
func TestParameters(t *testing.T) {
out, err := exec.Command("./parameters.go").Output()
if err != nil {
t.Fatal(err)
}
// No parameters
expected(t, "parameters.go", string(out), "Parameters: 0\n")
// ---------------------------------------------------------------
out, err = exec.Command("./parameters.go", "-f", "One", "Two").Output()
if err != nil {
t.Fatal(err)
}
// 3 parameters
expected(t, "parameters.go", string(out), "Parameters: 3\n-f\nOne\nTwo\n")
}
func TestHotReload(t *testing.T) {
var buffer bytes.Buffer
cmd := exec.Command("goplay", "-r", "reload.go")
cmd.Stdout = &buffer
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
// Have to sleep long enough for file watches to be setup and binary to be started
// If the machine this test runs on is too slow, the sleep value needs to be increased..
time.Sleep(2222 * time.Millisecond)
// Modify reload.go while it is running in an infinite loop
modifyFile(t, "var stop = true", 6, "reload.go")
defer modifyFile(t, "var stop = false", 6, "reload.go") // Reset reload.go
if err := cmd.Wait(); err != nil {
t.Fatal(err)
}
expected(t, "reload.go", buffer.String(), "Start!\nStart!\nStop!\n")
}
func TestHotReloadRecursiveAndFileExtensions(t *testing.T) {
var buffer bytes.Buffer
cmd := exec.Command("./watch/watch.go")
cmd.Stdout = &buffer
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
// #1 - Start!
time.Sleep(2222 * time.Millisecond)
modifyFile(t, "// trigger rebuild", 0, "watch/recursive/watch_this.data")
defer modifyFile(t, "// 1234567890", 0, "watch/recursive/watch_this.data")
// #2 - Start!
time.Sleep(2222 * time.Millisecond)
modifyFile(t, "var stop = true", 8, "watch/watch.go")
defer modifyFile(t, "var stop = false", 8, "watch/watch.go")
// #3 - Start!
// #1 - Stop!
if err := cmd.Wait(); err != nil {
t.Fatal(err)
}
expected(t, "watch/watch.go", buffer.String(), "Start!\nStart!\nStart!\nStop!\n")
}