-
Notifications
You must be signed in to change notification settings - Fork 0
/
testrunner.go
176 lines (148 loc) · 3.88 KB
/
testrunner.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
// Copyright 2019 Pronovix
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"time"
)
var (
root = flag.String("root", ".", "the directory where the tests are; pass - to skip")
command = flag.String("command", "", "command to run")
pattern = flag.String("pattern", "Test.php$", "pattern to match test files")
threads = flag.Int("threads", runtime.NumCPU(), "number of threads")
timeout = flag.Int("timeout", 9, "after timeout minutes, a new line will be printed to stdout")
verbose = flag.Bool("verbose", false, "enables verbose mode")
split = regexp.MustCompile(`\s+`)
parts []string
success = 0
fail = 0
resultMtx sync.Mutex
)
// worker reads from the channel, runs the command and puts the output on the output channel.
func worker(path, output chan string, wg *sync.WaitGroup) {
for filename := range path {
maybeLog("Starting file " + filename)
output <- run(filename, wg)
}
}
// run runs the command with the file in path as an extra parameter.
func run(path string, wg *sync.WaitGroup) string {
defer wg.Done()
localParts := make([]string, len(parts))
for i, part := range parts {
localParts[i] = part
}
start := time.Now()
cmdparts := append(localParts, path)
cmd := exec.Cmd{
Path: parts[0],
Args: cmdparts,
}
out, err := cmd.CombinedOutput()
resultMtx.Lock()
if err != nil {
fail += 1
} else {
success += 1
}
resultMtx.Unlock()
maybeLog("Finished file " + path)
return fmt.Sprintf("Running %s\n\n%s\n\nElapsed: %s\n",
strings.Join(cmdparts, " "),
string(out),
time.Since(start).String(),
)
}
// printer prints out the messages coming the channel, and marks the job done in the wait group.
func printer(str chan string) {
for {
select {
case output := <-str:
if output = strings.TrimSpace(output); output != "" {
fmt.Printf("\n%s\n\n", output)
}
case <-time.After(time.Duration(*timeout) * time.Minute):
fmt.Println("")
}
}
}
func maybeLog(msg string) {
if *verbose {
_, _ = fmt.Fprintln(os.Stderr, msg)
}
}
// main funciton.
func main() {
flag.Parse()
if *command == "" {
fmt.Println("no command is specified")
os.Exit(1)
}
parts = split.Split(*command, -1)
var wg sync.WaitGroup
input := make(chan string, 128)
output := make(chan string)
for i := 0; i < *threads; i++ {
go worker(input, output, &wg)
}
go printer(output)
matcher, err := regexp.Compile(*pattern)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Starting %d threads\n\n", *threads)
start := time.Now()
files := make(map[string]struct{})
processFile := func(path string) {
wg.Add(1)
maybeLog("Adding file " + path)
input <- path
}
if *root != "-" {
_ = filepath.Walk(*root, func(path string, info os.FileInfo, err error) error {
if matcher.MatchString(path) {
if _, found := files[path]; !found {
files[path] = struct{}{}
processFile(path)
}
}
return nil
})
}
if stat, _ := os.Stdin.Stat(); (stat.Mode() & os.ModeCharDevice) == 0 {
data, _ := ioutil.ReadAll(os.Stdin)
for _, fn := range strings.Split(string(data), "\x00") {
if fn != "" {
processFile(fn)
}
}
}
wg.Wait()
close(input)
close(output)
fmt.Printf("\n\nComplete runtime: %s | Success: %d Failure: %d\n\n", time.Since(start).String(), success, fail)
if fail > 0 {
os.Exit(1)
}
}