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
|
package main
import (
"flag"
"fmt"
"time"
"github.com/onsi/ginkgo/config"
"github.com/onsi/ginkgo/ginkgo/interrupthandler"
"github.com/onsi/ginkgo/ginkgo/testrunner"
"github.com/onsi/ginkgo/ginkgo/testsuite"
"github.com/onsi/ginkgo/ginkgo/watch"
)
func BuildWatchCommand() *Command {
commandFlags := NewWatchCommandFlags(flag.NewFlagSet("watch", flag.ExitOnError))
interruptHandler := interrupthandler.NewInterruptHandler()
notifier := NewNotifier(commandFlags)
watcher := &SpecWatcher{
commandFlags: commandFlags,
notifier: notifier,
interruptHandler: interruptHandler,
suiteRunner: NewSuiteRunner(notifier, interruptHandler),
}
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 *RunWatchAndBuildCommandFlags
notifier *Notifier
interruptHandler *interrupthandler.InterruptHandler
suiteRunner *SuiteRunner
}
func (w *SpecWatcher) WatchSpecs(args []string, additionalArgs []string) {
w.commandFlags.computeNodes()
w.notifier.VerifyNotificationsAreAvailable()
w.WatchSuites(args, additionalArgs)
}
func (w *SpecWatcher) runnersForSuites(suites []testsuite.TestSuite, additionalArgs []string) []*testrunner.TestRunner {
runners := []*testrunner.TestRunner{}
for _, suite := range suites {
runners = append(runners, testrunner.New(suite, w.commandFlags.NumCPU, w.commandFlags.ParallelStream, w.commandFlags.GoOpts, additionalArgs))
}
return runners
}
func (w *SpecWatcher) WatchSuites(args []string, additionalArgs []string) {
suites, _ := findSuites(args, w.commandFlags.Recurse, w.commandFlags.SkipPackage, false)
if len(suites) == 0 {
complainAndQuit("Found no test suites")
}
fmt.Printf("Identified %d test %s. Locating dependencies to a depth of %d (this may take a while)...\n", len(suites), pluralizedWord("suite", "suites", len(suites)), w.commandFlags.Depth)
deltaTracker := watch.NewDeltaTracker(w.commandFlags.Depth)
delta, errors := deltaTracker.Delta(suites)
fmt.Printf("Watching %d %s:\n", len(delta.NewSuites), pluralizedWord("suite", "suites", len(delta.NewSuites)))
for _, suite := range delta.NewSuites {
fmt.Println(" " + suite.Description())
}
for suite, err := range errors {
fmt.Printf("Failed to watch %s: %s\n", suite.PackageName, err)
}
if len(suites) == 1 {
runners := w.runnersForSuites(suites, additionalArgs)
w.suiteRunner.RunSuites(runners, w.commandFlags.NumCompilers, true, nil)
runners[0].CleanUp()
}
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
suites, _ := findSuites(args, w.commandFlags.Recurse, w.commandFlags.SkipPackage, false)
delta, _ := deltaTracker.Delta(suites)
suitesToRun := []testsuite.TestSuite{}
if len(delta.NewSuites) > 0 {
fmt.Printf(greenColor+"Detected %d new %s:\n"+defaultStyle, len(delta.NewSuites), pluralizedWord("suite", "suites", len(delta.NewSuites)))
for _, suite := range delta.NewSuites {
suitesToRun = append(suitesToRun, suite.Suite)
fmt.Println(" " + suite.Description())
}
}
modifiedSuites := delta.ModifiedSuites()
if len(modifiedSuites) > 0 {
fmt.Println(greenColor + "\nDetected changes in:" + defaultStyle)
for _, pkg := range delta.ModifiedPackages {
fmt.Println(" " + pkg)
}
fmt.Printf(greenColor+"Will run %d %s:\n"+defaultStyle, len(modifiedSuites), pluralizedWord("suite", "suites", len(modifiedSuites)))
for _, suite := range modifiedSuites {
suitesToRun = append(suitesToRun, suite.Suite)
fmt.Println(" " + suite.Description())
}
fmt.Println("")
}
if len(suitesToRun) > 0 {
w.UpdateSeed()
w.ComputeSuccinctMode(len(suitesToRun))
runners := w.runnersForSuites(suitesToRun, additionalArgs)
result, _ := w.suiteRunner.RunSuites(runners, w.commandFlags.NumCompilers, true, func(suite testsuite.TestSuite) {
deltaTracker.WillRun(suite)
})
for _, runner := range runners {
runner.CleanUp()
}
if !w.interruptHandler.WasInterrupted() {
color := redColor
if result.Passed {
color = greenColor
}
fmt.Println(color + "\nDone. Resuming watch..." + defaultStyle)
}
}
case <-w.interruptHandler.C:
return
}
}
}
func (w *SpecWatcher) ComputeSuccinctMode(numSuites int) {
if config.DefaultReporterConfig.Verbose {
config.DefaultReporterConfig.Succinct = false
return
}
if w.commandFlags.wasSet("succinct") {
return
}
if numSuites == 1 {
config.DefaultReporterConfig.Succinct = false
}
if numSuites > 1 {
config.DefaultReporterConfig.Succinct = true
}
}
func (w *SpecWatcher) UpdateSeed() {
if !w.commandFlags.wasSet("seed") {
config.GinkgoConfig.RandomSeed = time.Now().Unix()
}
}
|