-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_runner.go
106 lines (92 loc) · 1.94 KB
/
test_runner.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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
"github.com/pkg/errors"
)
const TestTimeout = 15
type TestOutcome struct {
name, message string
ok bool
}
func worker(id int, jobs <-chan string, results chan<- *TestOutcome, wg *sync.WaitGroup) {
defer wg.Done()
for job := range jobs {
outcome := runTest(job)
if outcome.ok {
fmt.Printf(".")
} else {
fmt.Printf("x")
}
results <- outcome
}
}
func runTest(path string) *TestOutcome {
ctx, cancel := context.WithTimeout(context.Background(), TestTimeout*time.Second)
cmd := exec.CommandContext(ctx, "./goboy", path)
outcome := &TestOutcome{name: path}
defer cancel()
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatal(errors.Wrap(err, "couldn't open stdout"))
}
if err := cmd.Start(); err != nil {
log.Fatal(errors.Wrap(err, "couldn't start cmd"))
}
var output string
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
text := scanner.Text()
output += "."
output += text
if strings.Contains(output, "Passed") {
outcome.ok = true
return outcome
} else if strings.Contains(output, "Failed") {
outcome.message = output
return outcome
}
}
if err := cmd.Wait(); err != nil {
outcome.message = "timeout"
return outcome
}
outcome.message = "error"
return outcome
}
func main() {
runtime.GOMAXPROCS(4)
var wg sync.WaitGroup
paths := make(chan string, 100)
results := make(chan *TestOutcome, 100)
for w := 1; w <= 4; w++ {
wg.Add(1)
go worker(w, paths, results, &wg)
}
_ = filepath.Walk("specs", func(path string, _ os.FileInfo, _ error) error {
if filepath.Ext(path) == ".gb" {
paths <- path
}
return nil
})
close(paths)
wg.Wait()
close(results)
fmt.Printf("\n")
fmt.Printf("=============\n")
fmt.Printf("\n")
for test := range results {
if !test.ok {
fmt.Printf("Test %s failed: %s\n", test.name, test.message)
}
}
}