forked from smartystreets/goconvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoconvey.go
318 lines (287 loc) · 9.59 KB
/
goconvey.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// This executable provides an HTTP server that watches for file system changes
// to .go files within the working directory (and all nested go packages).
// Navigating to the configured host and port in a web browser will display the
// latest results of running `go test` in each go package.
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/smartystreets/goconvey/web/server/api"
"github.com/smartystreets/goconvey/web/server/contract"
"github.com/smartystreets/goconvey/web/server/executor"
"github.com/smartystreets/goconvey/web/server/messaging"
"github.com/smartystreets/goconvey/web/server/parser"
"github.com/smartystreets/goconvey/web/server/system"
"github.com/smartystreets/goconvey/web/server/watch"
)
func init() {
flags()
folders()
}
func flags() {
flag.IntVar(&port, "port", 8080, "The port at which to serve http.")
flag.StringVar(&host, "host", "127.0.0.1", "The host at which to serve http.")
flag.DurationVar(&nap, "poll", quarterSecond, "The interval to wait between polling the file system for changes.")
flag.IntVar(¶llelPackages, "packages", 10, "The number of packages to test in parallel. Higher == faster but more costly in terms of computing.")
flag.StringVar(&gobin, "gobin", "go", "The path to the 'go' binary (default: search on the PATH).")
flag.BoolVar(&cover, "cover", true, "Enable package-level coverage statistics. Requires Go 1.2+ and the go cover tool.")
flag.IntVar(&depth, "depth", -1, "The directory scanning depth. If -1, scan infinitely deep directory structures. 0: scan working directory. 1+: Scan into nested directories, limited to value.")
flag.StringVar(&timeout, "timeout", "0", "The test execution timeout if none is specified in the *.goconvey file (default is '0', which is the same as not providing this option).")
flag.StringVar(&watchedSuffixes, "watchedSuffixes", ".go", "A comma separated list of file suffixes to watch for modifications.")
flag.StringVar(&excludedDirs, "excludedDirs", "vendor,node_modules", "A comma separated list of directories that will be excluded from being watched.")
flag.StringVar(&workDir, "workDir", "", "set goconvey working directory (default current directory).")
flag.BoolVar(&autoLaunchBrowser, "launchBrowser", true, "toggle auto launching of browser.")
log.SetOutput(os.Stdout)
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
func folders() {
_, file, _, _ := runtime.Caller(0)
here := filepath.Dir(file)
static = filepath.Join(here, "/web/client")
reports = filepath.Join(static, "reports")
}
func main() {
flag.Parse()
log.Printf(initialConfiguration, host, port, nap, cover)
working := getWorkDir()
cover = coverageEnabled(cover, reports)
shell := system.NewShell(gobin, reports, cover, timeout)
watcherInput := make(chan messaging.WatcherCommand)
watcherOutput := make(chan messaging.Folders)
excludedDirItems := strings.Split(excludedDirs, `,`)
watcher := watch.NewWatcher(working, depth, nap, watcherInput, watcherOutput, watchedSuffixes, excludedDirItems)
parser := parser.NewParser(parser.ParsePackageResults)
tester := executor.NewConcurrentTester(shell)
tester.SetBatchSize(parallelPackages)
longpollChan := make(chan chan string)
executor := executor.NewExecutor(tester, parser, longpollChan)
server := api.NewHTTPServer(working, watcherInput, executor, longpollChan)
listener := createListener()
go runTestOnUpdates(watcherOutput, executor, server)
go watcher.Listen()
if autoLaunchBrowser {
go launchBrowser(listener.Addr().String())
}
serveHTTP(server, listener)
}
func browserCmd() (string, bool) {
browser := map[string]string{
"darwin": "open",
"linux": "xdg-open",
"windows": "start",
}
cmd, ok := browser[runtime.GOOS]
return cmd, ok
}
func launchBrowser(addr string) {
browser, ok := browserCmd()
if !ok {
log.Printf("Skipped launching browser for this OS: %s", runtime.GOOS)
return
}
log.Printf("Launching browser on %s", addr)
url := fmt.Sprintf("http://%s", addr)
cmd := exec.Command(browser, url)
output, err := cmd.CombinedOutput()
if err != nil {
log.Println(err)
}
log.Println(string(output))
}
func runTestOnUpdates(queue chan messaging.Folders, executor contract.Executor, server contract.Server) {
for update := range queue {
log.Println("Received request from watcher to execute tests...")
packages := extractPackages(update)
output := executor.ExecuteTests(packages)
root := extractRoot(update, packages)
server.ReceiveUpdate(root, output)
}
}
func extractPackages(folderList messaging.Folders) []*contract.Package {
packageList := []*contract.Package{}
for _, folder := range folderList {
if isInsideTestdata(folder) {
continue
}
hasImportCycle := testFilesImportTheirOwnPackage(folder.Path)
packageName := resolvePackageName(folder.Path)
packageList = append(
packageList,
contract.NewPackage(folder, packageName, hasImportCycle),
)
}
return packageList
}
// For packages that operate on Go source code files, such as Go tooling, it is
// important to have a location that will not be considered part of package
// source to store those files. The official Go tooling selected the testdata
// folder for this purpose, so we need to ignore folders inside testdata.
func isInsideTestdata(folder *messaging.Folder) bool {
relativePath, err := filepath.Rel(folder.Root, folder.Path)
if err != nil {
// There should never be a folder that's not inside the root, but if
// there is, we can presumably count it as outside a testdata folder as
// well
return false
}
for _, directory := range strings.Split(filepath.ToSlash(relativePath), "/") {
if directory == "testdata" {
return true
}
}
return false
}
func extractRoot(folderList messaging.Folders, packageList []*contract.Package) string {
path := packageList[0].Path
folder := folderList[path]
return folder.Root
}
func createListener() net.Listener {
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil {
log.Println(err)
}
if l == nil {
os.Exit(1)
}
return l
}
func serveHTTP(server contract.Server, listener net.Listener) {
serveStaticResources()
serveAjaxMethods(server)
activateServer(listener)
}
func serveStaticResources() {
http.Handle("/", http.FileServer(http.Dir(static)))
}
func serveAjaxMethods(server contract.Server) {
http.HandleFunc("/watch", server.Watch)
http.HandleFunc("/ignore", server.Ignore)
http.HandleFunc("/reinstate", server.Reinstate)
http.HandleFunc("/latest", server.Results)
http.HandleFunc("/execute", server.Execute)
http.HandleFunc("/status", server.Status)
http.HandleFunc("/status/poll", server.LongPollStatus)
http.HandleFunc("/pause", server.TogglePause)
}
func activateServer(listener net.Listener) {
log.Printf("Serving HTTP at: http://%s\n", listener.Addr())
err := http.Serve(listener, nil)
if err != nil {
log.Println(err)
}
}
func coverageEnabled(cover bool, reports string) bool {
return (cover &&
goMinVersion(1, 2) &&
coverToolInstalled() &&
ensureReportDirectoryExists(reports))
}
func goMinVersion(wanted ...int) bool {
version := runtime.Version() // 'go1.2....'
s := regexp.MustCompile(`go([\d]+)\.([\d]+)\.?([\d]+)?`).FindAllStringSubmatch(version, 1)
if len(s) == 0 {
log.Printf("Cannot determine if newer than go1.2, disabling coverage.")
return false
}
for idx, str := range s[0][1:] {
if len(wanted) == idx {
break
}
if v, _ := strconv.Atoi(str); v < wanted[idx] {
log.Printf(pleaseUpgradeGoVersion, version)
return false
}
}
return true
}
func coverToolInstalled() bool {
working := getWorkDir()
command := system.NewCommand(working, "go", "tool", "cover").Execute()
installed := strings.Contains(command.Output, "Usage of 'go tool cover':")
if !installed {
log.Print(coverToolMissing)
return false
}
return true
}
func ensureReportDirectoryExists(reports string) bool {
result, err := exists(reports)
if err != nil {
log.Fatal(err)
}
if result {
return true
}
if err := os.Mkdir(reports, 0755); err == nil {
return true
}
log.Printf(reportDirectoryUnavailable, reports)
return false
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
func getWorkDir() string {
working := ""
var err error
if workDir != "" {
working = workDir
} else {
working, err = os.Getwd()
if err != nil {
log.Fatal(err)
}
}
result, err := exists(working)
if err != nil {
log.Fatal(err)
}
if !result {
log.Fatalf("Path:%s does not exists", working)
}
return working
}
var (
port int
host string
gobin string
nap time.Duration
parallelPackages int
cover bool
depth int
timeout string
watchedSuffixes string
excludedDirs string
autoLaunchBrowser bool
static string
reports string
quarterSecond = time.Millisecond * 250
workDir string
)
const (
initialConfiguration = "Initial configuration: [host: %s] [port: %d] [poll: %v] [cover: %v]\n"
pleaseUpgradeGoVersion = "Go version is less that 1.2 (%s), please upgrade to the latest stable version to enable coverage reporting.\n"
coverToolMissing = "Go cover tool is not installed or not accessible: for Go < 1.5 run`go get golang.org/x/tools/cmd/cover`\n For >= Go 1.5 run `go install $GOROOT/src/cmd/cover`\n"
reportDirectoryUnavailable = "Could not find or create the coverage report directory (at: '%s'). You probably won't see any coverage statistics...\n"
separator = string(filepath.Separator)
endGoPath = separator + "src" + separator
)