forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.go
224 lines (193 loc) · 4.73 KB
/
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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
type command struct {
name string
task (func() (string, error))
}
type commands []command
func andThen(a, b command) command {
var result command
result.name = a.name + " && " + b.name
result.task = (func() (string, error) {
output, err := a.task()
if err != nil {
return output, err
}
output2, err := b.task()
return output + output2, err
})
return result
}
// Output ...
type Output struct {
name string
// log string
err error
}
func runCommands(commands commands) {
size := len(commands)
output := make(chan Output, size)
var v sync.WaitGroup
for _, com := range commands {
v.Add(1)
go func(f command) {
defer v.Done()
fmt.Println("Running", f.name)
out, err := f.task()
fmt.Println(out)
if err != nil {
output <- Output{name: f.name, err: err}
}
fmt.Println("Finished", f.name)
}(com)
}
v.Wait()
close(output)
fmt.Println("All commands finished")
failed := 0
for x := range output {
failed++
fmt.Println("Failed command", x.name, x.err)
}
if failed == 0 {
fmt.Println("All commands successufl")
}
}
func makeCommand(label string, name string, args ...string) command {
cmd := exec.Command(name, args...)
return command{
name: label,
task: (func() (string, error) {
output, err := cmd.CombinedOutput()
return string(output), err
}),
}
}
func commandString(name string) command {
xs := strings.Fields(name)
if len(xs) == 0 {
panic("invalid command" + name)
}
return makeCommand(name, xs[0], xs[1:]...)
}
func checkError(err error) {
if err != nil {
log.Fatalf("Error: %s", err.Error())
}
}
func testTheme(theme string) {
fmt.Println("Removing", theme)
os.RemoveAll(theme)
cmd := exec.Command("bsb", "-theme", theme, "-init", theme)
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
checkError(err)
fmt.Println("Started to build ")
cmd2 := exec.Command("npm", "run", "build")
cmd2.Dir = theme
output2, err := cmd2.CombinedOutput()
fmt.Println(string(output2))
checkError(err)
os.RemoveAll(theme)
}
func runMoCha(wg *sync.WaitGroup) {
defer wg.Done()
fmt.Println("make -C jscomp/test/all")
make := exec.Command("make", "-C", filepath.Join("jscomp", "test"))
makeOut, err := make.CombinedOutput()
fmt.Println(string(makeOut))
checkError(err)
fmt.Println("Running Mocha tests")
cmd := exec.Command("mocha", "./jscomp/test/**/*test.js")
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
checkError(err)
fmt.Println("Running mocha finished")
}
func installGlobal(wg *sync.WaitGroup) {
defer wg.Done()
cmd := exec.Command("npm", "install", "-g", ".")
output, err := cmd.CombinedOutput()
fmt.Printf(string(output))
checkError(err)
}
var cmd = exec.Command
// Avoid rebuilding OCaml again
func init() {
vendorOCamlPath, _ := filepath.Abs(filepath.Join(".", "vendor", "ocaml", "bin"))
os.Setenv("PATH",
vendorOCamlPath+string(os.PathListSeparator)+os.Getenv("PATH"))
}
func testBsb() {
c := cmd("bsb", "-regen")
c.Dir = filepath.Join("jscomp", "build_tests", "in_source")
out, err := c.CombinedOutput()
outS := string(out)
if !strings.Contains( outS, "two module formats") {
fmt.Println(outS)
fmt.Println(err)
os.Exit(2)
} else {
fmt.Println("Output matches")
}
}
func main() {
noInstallGlobal := flag.Bool("no-install-global", false, "don't install global")
noOunitTest := flag.Bool("no-ounit", false, "don't do ounit test")
noMochaTest := flag.Bool("no-mocha", false, "don't run mocha")
noThemeTest := flag.Bool("no-theme", false, "no bsb theme test")
// disableAll := flag.Bool("disable-all", false, "disable all tets")
flag.Parse()
output, _ := cmd("which", "ocaml").CombinedOutput()
fmt.Println("OCaml:", string(output))
if !*noOunitTest {
cmd("make", "-C", "jscomp", "test")
}
if !*noMochaTest {
make := cmd("make", "-C", "jscomp", "travis-world-test")
make.Stdout = os.Stdout
make.Stderr = os.Stderr
error := make.Run()
if error != nil {
os.Exit(2)
}
}
if !*noInstallGlobal {
ginstall := cmd("npm", "i", "-g", ".")
fmt.Println("install bucklescript globally")
start := time.Now()
ginstall.Stdout = os.Stdout
ginstall.Stderr = os.Stderr
error := ginstall.Run()
if error != nil {
log.Fatalf("install failed")
} else {
fmt.Println("install finished takes", time.Since(start))
}
}
bsbDir, _ := cmd("bsb", "-where").CombinedOutput()
fmt.Println("BSBDIR:", string(bsbDir))
if !*noThemeTest {
var wg sync.WaitGroup
for _, theme := range []string{"basic", "basic-reason", "generator", "minimal", "node"} {
fmt.Println("Test theme", theme)
wg.Add(1)
go (func(theme string) {
defer wg.Done()
testTheme(theme)
})(theme)
}
wg.Wait()
}
testBsb()
}