How to use ModifiedSuites method of watch Package

Best Ginkgo code snippet using watch.ModifiedSuites

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

watch_command.go

Source: watch_command.go Github

copy
1package main
2
3import (
4	"flag"
5	"fmt"
6	"time"
7
8	"github.com/innotech/hydra-worker-pilot-client/vendors/github.com/onsi/ginkgo/config"
9	"github.com/innotech/hydra-worker-pilot-client/vendors/github.com/onsi/ginkgo/ginkgo/testrunner"
10	"github.com/innotech/hydra-worker-pilot-client/vendors/github.com/onsi/ginkgo/ginkgo/testsuite"
11	"github.com/innotech/hydra-worker-pilot-client/vendors/github.com/onsi/ginkgo/ginkgo/watch"
12)
13
14func BuildWatchCommand() *Command {
15	commandFlags := NewWatchCommandFlags(flag.NewFlagSet("watch", flag.ExitOnError))
16	interruptHandler := NewInterruptHandler()
17	notifier := NewNotifier(commandFlags)
18	watcher := &SpecWatcher{
19		commandFlags:		commandFlags,
20		notifier:		notifier,
21		interruptHandler:	interruptHandler,
22		suiteRunner:		NewSuiteRunner(notifier, interruptHandler),
23	}
24
25	return &Command{
26		Name:		"watch",
27		FlagSet:	commandFlags.FlagSet,
28		UsageCommand:	"ginkgo watch <FLAGS> <PACKAGES> -- <PASS-THROUGHS>",
29		Usage: []string{
30			"Watches the tests in the passed in <PACKAGES> and runs them when changes occur.",
31			"Any arguments after -- will be passed to the test.",
32		},
33		Command:			watcher.WatchSpecs,
34		SuppressFlagDocumentation:	true,
35		FlagDocSubstitute: []string{
36			"Accepts all the flags that the ginkgo command accepts except for --keepGoing and --untilItFails",
37		},
38	}
39}
40
41type SpecWatcher struct {
42	commandFlags		*RunAndWatchCommandFlags
43	notifier		*Notifier
44	interruptHandler	*InterruptHandler
45	suiteRunner		*SuiteRunner
46}
47
48func (w *SpecWatcher) WatchSpecs(args []string, additionalArgs []string) {
49	w.commandFlags.computeNodes()
50	w.notifier.VerifyNotificationsAreAvailable()
51
52	w.WatchSuites(args, additionalArgs)
53}
54
55func (w *SpecWatcher) runnersForSuites(suites []testsuite.TestSuite, additionalArgs []string) []*testrunner.TestRunner {
56	runners := []*testrunner.TestRunner{}
57
58	for _, suite := range suites {
59		runners = append(runners, testrunner.New(suite, w.commandFlags.NumCPU, w.commandFlags.ParallelStream, w.commandFlags.Race, w.commandFlags.Cover, w.commandFlags.Tags, additionalArgs))
60	}
61
62	return runners
63}
64
65func (w *SpecWatcher) WatchSuites(args []string, additionalArgs []string) {
66	suites, _ := findSuites(args, w.commandFlags.Recurse, w.commandFlags.SkipPackage)
67
68	if len(suites) == 0 {
69		complainAndQuit("Found no test suites")
70	}
71
72	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)
73	deltaTracker := watch.NewDeltaTracker(w.commandFlags.Depth)
74	delta, errors := deltaTracker.Delta(suites)
75
76	fmt.Printf("Watching %d %s:\n", len(delta.NewSuites), pluralizedWord("suite", "suites", len(delta.NewSuites)))
77	for _, suite := range delta.NewSuites {
78		fmt.Println("  " + suite.Description())
79	}
80
81	for suite, err := range errors {
82		fmt.Printf("Failed to watch %s: %s\n"+suite.PackageName, err)
83	}
84
85	if len(suites) == 1 {
86		runners := w.runnersForSuites(suites, additionalArgs)
87		w.suiteRunner.RunSuites(runners, true, nil)
88		runners[0].CleanUp()
89	}
90
91	ticker := time.NewTicker(time.Second)
92
93	for {
94		select {
95		case <-ticker.C:
96			suites, _ := findSuites(args, w.commandFlags.Recurse, w.commandFlags.SkipPackage)
97			delta, _ := deltaTracker.Delta(suites)
98
99			suitesToRun := []testsuite.TestSuite{}
100
101			if len(delta.NewSuites) > 0 {
102				fmt.Printf(greenColor+"Detected %d new %s:\n"+defaultStyle, len(delta.NewSuites), pluralizedWord("suite", "suites", len(delta.NewSuites)))
103				for _, suite := range delta.NewSuites {
104					suitesToRun = append(suitesToRun, suite.Suite)
105					fmt.Println("  " + suite.Description())
106				}
107			}
108
109			modifiedSuites := delta.ModifiedSuites()
110			if len(modifiedSuites) > 0 {
111				fmt.Println(greenColor + "\nDetected changes in:" + defaultStyle)
112				for _, pkg := range delta.ModifiedPackages {
113					fmt.Println("  " + pkg)
114				}
115				fmt.Printf(greenColor+"Will run %d %s:\n"+defaultStyle, len(modifiedSuites), pluralizedWord("suite", "suites", len(modifiedSuites)))
116				for _, suite := range modifiedSuites {
117					suitesToRun = append(suitesToRun, suite.Suite)
118					fmt.Println("  " + suite.Description())
119				}
120				fmt.Println("")
121			}
122
123			if len(suitesToRun) > 0 {
124				w.UpdateSeed()
125				w.ComputeSuccinctMode(len(suitesToRun))
126				runners := w.runnersForSuites(suitesToRun, additionalArgs)
127				result, _ := w.suiteRunner.RunSuites(runners, true, func(suite testsuite.TestSuite) {
128					deltaTracker.WillRun(suite)
129				})
130				for _, runner := range runners {
131					runner.CleanUp()
132				}
133				if !w.interruptHandler.WasInterrupted() {
134					color := redColor
135					if result.Passed {
136						color = greenColor
137					}
138					fmt.Println(color + "\nDone.  Resuming watch..." + defaultStyle)
139				}
140			}
141
142		case <-w.interruptHandler.C:
143			return
144		}
145	}
146}
147
148func (w *SpecWatcher) ComputeSuccinctMode(numSuites int) {
149	if config.DefaultReporterConfig.Verbose {
150		config.DefaultReporterConfig.Succinct = false
151		return
152	}
153
154	if w.commandFlags.wasSet("succinct") {
155		return
156	}
157
158	if numSuites == 1 {
159		config.DefaultReporterConfig.Succinct = false
160	}
161
162	if numSuites > 1 {
163		config.DefaultReporterConfig.Succinct = true
164	}
165}
166
167func (w *SpecWatcher) UpdateSeed() {
168	if !w.commandFlags.wasSet("seed") {
169		config.GinkgoConfig.RandomSeed = time.Now().Unix()
170	}
171}
172
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)