forked from git-lfs/git-lfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.go
More file actions
210 lines (179 loc) · 3.76 KB
/
integration.go
File metadata and controls
210 lines (179 loc) · 3.76 KB
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
package main
import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
var (
bashPath string
debugging = false
erroring = false
maxprocs = 4
testPattern = regexp.MustCompile(`test[/\\]test-([a-z\-]+)\.sh$`)
)
func mainIntegration() {
if len(os.Getenv("DEBUG")) > 0 {
debugging = true
}
setBash()
if max, _ := strconv.Atoi(os.Getenv("GIT_LFS_TEST_MAXPROCS")); max > 0 {
maxprocs = max
}
fmt.Println("Running this maxprocs", maxprocs)
files := testFiles()
if len(files) == 0 {
fmt.Println("no tests to run")
os.Exit(1)
}
var wg sync.WaitGroup
tests := make(chan string, len(files))
output := make(chan string, len(files))
for _, file := range files {
tests <- file
}
go printOutput(output)
for i := 0; i < maxprocs; i++ {
wg.Add(1)
go worker(tests, output, &wg)
}
close(tests)
wg.Wait()
close(output)
printOutput(output)
if erroring {
os.Exit(1)
}
}
func runTest(output chan string, testname string) {
buf := &bytes.Buffer{}
cmd := exec.Command(bashPath, testname)
cmd.Stdout = buf
cmd.Stderr = buf
err := cmd.Start()
if err != nil {
sendTestOutput(output, testname, buf, err)
return
}
done := make(chan error)
go func() {
if err := cmd.Wait(); err != nil {
done <- err
}
close(done)
}()
select {
case err = <-done:
sendTestOutput(output, testname, buf, err)
return
case <-time.After(3 * time.Minute):
sendTestOutput(output, testname, buf, errors.New("Timed out"))
cmd.Process.Kill()
return
}
}
func sendTestOutput(output chan string, testname string, buf *bytes.Buffer, err error) {
cli := strings.TrimSpace(buf.String())
if len(cli) == 0 {
cli = fmt.Sprintf("<no output for %s>", testname)
}
if err == nil {
output <- cli
} else {
basetestname := filepath.Base(testname)
if debugging {
fmt.Printf("Error on %s: %s\n", basetestname, err)
}
erroring = true
output <- fmt.Sprintf("error: %s => %s\n%s", basetestname, err, cli)
}
}
func printOutput(output <-chan string) {
for {
select {
case out, ok := <-output:
if !ok {
return
}
fmt.Println(out)
}
}
}
func worker(tests <-chan string, output chan string, wg *sync.WaitGroup) {
defer wg.Done()
for {
select {
case testname, ok := <-tests:
if !ok {
return
}
runTest(output, testname)
}
}
}
func testFiles() []string {
if len(os.Args) < 4 {
return allTestFiles()
}
fileMap := make(map[string]bool)
for _, file := range allTestFiles() {
fileMap[file] = true
}
files := make([]string, 0, len(os.Args)-3)
for _, arg := range os.Args {
fullname := "test/test-" + arg + ".sh"
if fileMap[fullname] {
files = append(files, fullname)
}
}
return files
}
func allTestFiles() []string {
files := make([]string, 0, 100)
filepath.Walk("test", func(path string, info os.FileInfo, err error) error {
if debugging {
fmt.Println("FOUND:", path)
}
if err != nil || info.IsDir() || !testPattern.MatchString(path) {
return nil
}
if debugging {
fmt.Println("MATCHING:", path)
}
files = append(files, path)
return nil
})
return files
}
func setBash() {
findcmd := "which"
if runtime.GOOS == "windows" {
// Can't use paths returned from which even if it's on PATH in Windows
// Because our Go binary is a separate Windows app & not MinGW, it
// can't understand paths like '/usr/bin/bash', needs Windows version
findcmd = "where"
}
out, err := exec.Command(findcmd, "bash").Output()
if err != nil {
fmt.Println("Unable to find bash:", err)
os.Exit(1)
}
bashPath = strings.TrimSpace(string(out))
if debugging {
fmt.Println("Using", bashPath)
}
// Test
_, err = exec.Command(bashPath, "--version").CombinedOutput()
if err != nil {
fmt.Println("Error calling bash:", err)
os.Exit(1)
}
}