-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgorc.go
290 lines (248 loc) · 8.54 KB
/
gorc.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
package main
import (
"fmt"
"github.com/stretchr/commander"
"github.com/stretchr/objx"
"os"
"strings"
"sync"
)
const (
// searchTest is the string for searching for test files
searchTest = "_test.go"
// searchGo is the string for searching for go files
searchGo = ".go"
)
func getwd() (string, error) {
directory, error := os.Getwd()
if error != nil {
fmt.Printf(errorCurrentDirectory, error)
}
return directory, error
}
func installTests(name string) bool {
fmt.Print("\nInstalling tests: ")
run, failed := runCommand(name, searchTest, "go", "test", "-i")
if run == 0 && failed == 0 {
fmt.Println("No tests were found in or below the current working directory.")
return false
} else {
fmt.Printf("\n\n%d installed. %d failed. [%.0f%% success]\n\n", run-failed, failed, (float32((run-failed))/float32(run))*100)
}
return failed == 0
}
func runTests(name string) {
fmt.Print("Running tests: ")
run, failed := runCommandParallel(name, searchTest, "go", "test")
if run == 0 && failed == 0 {
fmt.Println("No tests were found in or below the current working directory.")
} else {
fmt.Printf("\n\n%d run. %d succeeded. %d failed. [%.0f%% success]\n\n", run, run-failed, failed, (float32((run-failed))/float32(run))*100)
}
}
func vetPackages(name string) {
fmt.Printf("\nVetting packages: ")
run, failed := runCommandParallel(name, searchGo, "go", "vet")
if run == 0 && failed == 0 {
fmt.Println("No packages were found in or below the current working directory.")
} else {
fmt.Printf("\n\n%d vetted. %d succeeded. %d failed. [%.0f%% success]\n\n", run, run-failed, failed, (float32((run-failed))/float32(run))*100)
}
}
func raceTests(name string) {
fmt.Printf("\nRunning race tests: ")
run, failed := runCommandParallel(name, searchTest, "go", "test", "-race")
if run == 0 && failed == 0 {
fmt.Println("No tests were found in or below the current working directory.")
} else {
fmt.Printf("\n\n%d run. %d succeeded. %d failed. [%.0f%% success]\n\n", run, run-failed, failed, (float32((run-failed))/float32(run))*100)
}
}
func runCommand(target, search, command string, args ...string) (int, int) {
var outputs []string
lastPrintLen := 0
currentJob := 1
directories := []string{}
if directory, error := getwd(); error == nil {
recurseDirectories(directory, target, search,
func(currentDirectory string) bool {
if target == "all" {
return false
}
if contains, _ := sliceContainsString(currentDirectory, exclusions); target == "" && contains {
return true
}
return false
},
func(currentDirectory string) {
directories = append(directories, currentDirectory)
})
}
numCommands := len(directories)
for _, directory := range directories {
if lastPrintLen == 0 {
printString := fmt.Sprintf("[%d of %d]", currentJob, numCommands)
lastPrintLen = len(printString)
fmt.Print(printString)
} else {
printString := fmt.Sprintf("%s[%d of %d]", strings.Repeat("\b", lastPrintLen), currentJob, numCommands)
lastPrintLen = len(printString) - lastPrintLen
fmt.Print(printString)
}
currentJob++
output := runShellCommand(directory, command, args...)
if output != "" {
outputs = append(outputs, output)
}
}
if len(outputs) != 0 {
for _, output := range outputs {
fmt.Printf("\n\n%s", output)
}
return currentJob - 1, len(outputs)
}
return currentJob - 1, 0
}
func runCommandParallel(target, search, command string, args ...string) (int, int) {
var outputs []string
lastPrintLen := 0
currentJob := 1
directories := []string{}
if directory, error := getwd(); error == nil {
recurseDirectories(directory, target, search,
func(currentDirectory string) bool {
if target == "all" {
return false
}
if contains, _ := sliceContainsString(currentDirectory, exclusions); target == "" && contains {
return true
}
return false
},
func(currentDirectory string) {
directories = append(directories, currentDirectory)
})
}
numCommands := len(directories)
outputChan := make(chan string, 10)
var wg sync.WaitGroup
wg.Add(numCommands)
for _, directory := range directories {
go func(dir string) {
outputChan <- runShellCommand(dir, command, args...)
}(directory)
}
if lastPrintLen == 0 {
printString := fmt.Sprintf("[%d of %d]", currentJob, numCommands)
lastPrintLen = len(printString)
fmt.Print(printString)
} else {
printString := fmt.Sprintf("%s[%d of %d]", strings.Repeat("\b", lastPrintLen), currentJob, numCommands)
lastPrintLen = len(printString) - lastPrintLen
fmt.Print(printString)
}
go func() {
for output := range outputChan {
if lastPrintLen == 0 {
printString := fmt.Sprintf("[%d of %d]", currentJob, numCommands)
lastPrintLen = len(printString)
fmt.Print(printString)
} else {
printString := fmt.Sprintf("%s[%d of %d]", strings.Repeat("\b", lastPrintLen), currentJob, numCommands)
lastPrintLen = len(printString) - lastPrintLen
fmt.Print(printString)
}
currentJob++
if output != "" {
outputs = append(outputs, output)
}
wg.Done()
}
}()
wg.Wait()
if len(outputs) != 0 {
for _, output := range outputs {
fmt.Printf("\n\n%s", output)
}
return currentJob - 1, len(outputs)
}
return currentJob - 1, 0
}
var exclusions []string
func main() {
var config = readConfig()
exclusions = config[configKeyExclusions].([]string)
commander.Go(func() {
commander.Map(commander.DefaultCommand, "", "",
func(args objx.Map) {
name := ""
if _, ok := args["name"]; ok {
name = args["name"].(string)
}
if installTests(name) {
runTests(name)
} else {
fmt.Printf("Test dependency installation failed. Aborting test run.\n\n")
}
})
commander.Map("test [name=(string)]", "Runs tests, or named test",
"If no name argument is specified, runs all tests recursively. If a name argument is specified, runs just that test, unless the argument is \"all\", in which case it runs all tests, including those in the exclusion list.",
func(args objx.Map) {
name := ""
if _, ok := args["name"]; ok {
name = args["name"].(string)
}
if installTests(name) {
runTests(name)
} else {
fmt.Println("Test dependency installation failed. Aborting test run.")
}
})
commander.Map("install [name=(string)]", "Installs tests, or named test",
"If no name argument is specified, installs all tests recursively. If a name argument is specified, installs just that test, unless the argument is \"all\", in which case it installs all tests, including those in the exclusion list.",
func(args objx.Map) {
name := ""
if _, ok := args["name"]; ok {
name = args["name"].(string)
}
installTests(name)
})
commander.Map("vet [name=(string)]", "Vets packages, or named package",
"If no name argument is specified, vets all packages recursively. If a name argument is specified, vets just that package, unless the argument is \"all\", in which case it vets all packages, including those in the exclusion list.",
func(args objx.Map) {
name := ""
if _, ok := args["name"]; ok {
name = args["name"].(string)
}
vetPackages(name)
})
commander.Map("race [name=(string)]", "Runs race detector on tests, or named test",
"If no name argument is specified, race tests all tests recursively. If a name argument is specified, vets just that test, unless the argument is \"all\", in which case it vets all tests, including those in the exclusion list.",
func(args objx.Map) {
name := ""
if _, ok := args["name"]; ok {
name = args["name"].(string)
}
raceTests(name)
})
commander.Map("exclude name=(string)", "Excludes the named directory from recursion",
"An excluded directory will be skipped when walking the directory tree. Any subdirectories of the excluded directory will also be skipped.",
func(args objx.Map) {
exclude(args["name"].(string), config)
fmt.Printf("\nExcluded \"%s\" from being examined during recursion.\n", args["name"].(string))
config = readConfig()
exclusions = config[configKeyExclusions].([]string)
fmt.Printf("\n%s\n\n", formatExclusionsForPrint(exclusions))
})
commander.Map("include name=(string)", "Removes the named directory from the exclusion list", "",
func(args objx.Map) {
include(args["name"].(string), config)
fmt.Printf("\nRemoved \"%s\" from the exclusion list.\n", args["name"].(string))
fmt.Printf("\n%s\n\n", formatExclusionsForPrint(exclusions))
})
commander.Map("exclusions", "Prints the exclusion list", "",
func(args objx.Map) {
fmt.Printf("\n%s\n\n", formatExclusionsForPrint(exclusions))
})
})
}