forked from onsi/ginkgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch_command.go
More file actions
79 lines (68 loc) · 2.29 KB
/
watch_command.go
File metadata and controls
79 lines (68 loc) · 2.29 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
package main
import (
"flag"
"fmt"
"github.com/onsi/ginkgo/ginkgo/testrunner"
"github.com/onsi/ginkgo/ginkgo/testsuite"
)
func BuildWatchCommand() *Command {
commandFlags := NewWatchCommandFlags(flag.NewFlagSet("watch", flag.ExitOnError))
watcher := &SpecWatcher{
commandFlags: commandFlags,
notifier: NewNotifier(commandFlags),
interruptHandler: NewInterruptHandler(),
}
return &Command{
Name: "watch",
FlagSet: commandFlags.FlagSet,
UsageCommand: "ginkgo watch <FLAGS> <PACKAGES> -- <PASS-THROUGHS>",
Usage: []string{
"Watches the tests in the passed in <PACKAGES> and runs them when changes occur.",
"Any arguments after -- will be passed to the test.",
},
Command: watcher.WatchSpecs,
SuppressFlagDocumentation: true,
FlagDocSubstitute: []string{
"Accepts all the flags that the ginkgo command accepts except for --keepGoing and --untilItFails",
},
}
}
type SpecWatcher struct {
commandFlags *RunAndWatchCommandFlags
notifier *Notifier
interruptHandler *InterruptHandler
}
func (w *SpecWatcher) WatchSpecs(args []string, additionalArgs []string) {
w.notifier.VerifyNotificationsAreAvailable()
suites := findSuites(args, w.commandFlags.Recurse, w.commandFlags.SkipPackage)
w.WatchSuites(suites, additionalArgs)
}
func (w *SpecWatcher) WatchSuites(suites []*testsuite.TestSuite, additionalArgs []string) {
modifiedSuite := make(chan *testsuite.TestSuite)
for _, suite := range suites {
go suite.Watch(modifiedSuite)
}
if len(suites) == 1 {
w.RunSuite(suites[0], additionalArgs)
}
for {
select {
case suite := <-modifiedSuite:
w.notifier.SendNotification("Ginkgo", fmt.Sprintf(`Detected change in "%s"...`, suite.PackageName))
fmt.Printf("\n\nDetected change in %s\n\n", suite.PackageName)
w.RunSuite(suite, additionalArgs)
case <-w.interruptHandler.C:
return
}
}
}
func (w *SpecWatcher) RunSuite(suite *testsuite.TestSuite, additionalArgs []string) {
runner := testrunner.New(suite, w.commandFlags.NumCPU, w.commandFlags.ParallelStream, w.commandFlags.Race, w.commandFlags.Cover, additionalArgs)
err := runner.Compile()
if err != nil {
fmt.Print(err.Error())
}
suitePassed := (err == nil) && runner.Run()
w.notifier.SendSuiteCompletionNotification(suite, suitePassed)
runner.CleanUp()
}