-
Notifications
You must be signed in to change notification settings - Fork 10
/
runner.go
226 lines (184 loc) · 5.1 KB
/
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
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
package gfmrun
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/sirupsen/logrus"
)
// Runner is the top level of execution for running examples in sources
type Runner struct {
Sources []string
Count int
Frobs map[string]Frob
Languages *Languages
noExec bool
extractDir string
log *logrus.Logger
}
// NewRunner makes a *Runner from a slice of sources, optional expected example
// count, optional languages.yml location, and a log
func NewRunner(sources []string, count int, languagesYml string, autoPull bool, log *logrus.Logger) (*Runner, error) {
var langs *Languages
if languagesYml == "" {
languagesYml = DefaultLanguagesYml
}
if _, err := os.Stat(languagesYml); err != nil && autoPull {
log.WithFields(logrus.Fields{
"url": DefaultLanguagesYmlURL,
"dest": languagesYml,
}).Info("downloading")
err = PullLanguagesYml(DefaultLanguagesYmlURL, languagesYml)
if err != nil {
return nil, err
}
}
if _, err := os.Stat(languagesYml); err == nil {
log.WithFields(logrus.Fields{
"languages": languagesYml,
}).Info("loading")
langs, err = LoadLanguages(languagesYml)
if err != nil {
return nil, err
}
}
return &Runner{
Sources: sources,
Count: count,
Frobs: DefaultFrobs,
Languages: langs,
log: log,
}, nil
}
// Run scans all sources for runnable examples, runs them, and returns a slice
// of errors encountered
func (r *Runner) Run() []error {
if len(r.Sources) < 1 {
r.log.Warn("no sources given")
return nil
}
res := []*runResult{}
sourcesStart := time.Now()
for i, sourceFile := range r.Sources {
sourceBytes, err := ioutil.ReadFile(sourceFile)
if err != nil {
res = append(res, &runResult{Retcode: -1, Error: err})
continue
}
res = append(res, r.checkSource(i, sourceFile, string(sourceBytes))...)
}
if !r.noExec && r.Count > 0 && len(res) != r.Count {
r.log.WithFields(logrus.Fields{
"expected": r.Count,
"actual": len(res),
}).Error("mismatched example count")
return []error{fmt.Errorf("example count %d != expected %d", len(res), r.Count)}
}
if len(res) == 0 {
return []error{}
}
errs := []error{}
for _, result := range res {
if result == nil {
continue
}
if result.Stdout != "" || result.Stderr != "" {
r.log.WithFields(logrus.Fields{
"source": result.Runnable.SourceFile,
"stdout": result.Stdout,
"stderr": result.Stderr,
}).Debug("captured output")
}
if result.Error != nil {
if v, ok := result.Error.(*skipErr); ok {
r.log.WithFields(logrus.Fields{
"source": result.Runnable.SourceFile,
"line": result.Runnable.LineOffset,
"reason": v.Reason,
}).Debug("skipped example")
} else {
errs = append(errs, result.Error)
}
}
}
r.log.WithFields(logrus.Fields{
"source_count": len(r.Sources),
"example_count": len(res),
"error_count": len(errs),
"time": time.Since(sourcesStart),
}).Info("done")
return errs
}
func (r *Runner) checkSource(i int, sourceName, source string) []*runResult {
res := []*runResult{}
sourceStart := time.Now()
runnables := r.findRunnables(i, sourceName, source)
for j, runnable := range runnables {
if r.noExec {
res = append(res, runnable.Extract(j, r.extractDir))
continue
}
r.log.WithFields(logrus.Fields{
"i": fmt.Sprintf("%d/%d", j+1, len(runnables)),
"source": sourceName,
"line": runnable.LineOffset,
"lang": runnable.Lang,
}).Info("start")
start := time.Now()
res = append(res, runnable.Run(j))
end := time.Since(start)
r.log.WithFields(logrus.Fields{
"i": fmt.Sprintf("%d/%d", j+1, len(runnables)),
"source": sourceName,
"line": runnable.LineOffset,
"lang": runnable.Lang,
"time": end,
}).Info("finish")
}
r.log.WithFields(logrus.Fields{
"source": sourceName,
"time": time.Since(sourceStart),
}).Info("checked")
return res
}
func (r *Runner) findRunnables(i int, sourceName, source string) []*Runnable {
finder := newRunnableFinder(sourceName, source, r.log)
runnables := finder.Find()
filteredRunnables := []*Runnable{}
for _, runnable := range runnables {
exe, ok := r.Frobs[runnable.Lang]
if !ok && r.Languages != nil {
lang := r.Languages.Lookup(runnable.Lang)
if lang == nil {
r.log.WithFields(logrus.Fields{
"source": runnable.SourceFile,
"lineno": runnable.LineOffset,
"lang": runnable.Lang,
}).Debug("unknown language, skipping")
continue
}
runnable.Lang = lang.Name
exe, ok = r.Frobs[runnable.Lang]
}
if !ok {
r.log.WithFields(logrus.Fields{
"source": runnable.SourceFile,
"lineno": runnable.LineOffset,
"lang": runnable.Lang,
}).Debug("no executor available for lang")
continue
}
runnable.Frob = exe
if err := exe.CanExecute(runnable); err != nil {
r.log.WithFields(logrus.Fields{
"source": runnable.SourceFile,
"lineno": runnable.LineOffset,
"reason": err,
}).Debug("skipping runnable due to filter func")
continue
}
filteredRunnables = append(filteredRunnables, runnable)
}
r.log.WithField("runnable_count", len(filteredRunnables)).Debug("returning runnables")
return filteredRunnables
}