blob: 9b320e387ce1c95f1691885655c4d66940e64775 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Ginkgo accepts a number of configuration options.
3
4These are documented [here](http://onsi.github.io/ginkgo/#the_ginkgo_cli)
5
6You can also learn more via
7
8 ginkgo help
9
10or (I kid you not):
11
12 go test -asdf
13*/
14package config
15
16import (
17 "flag"
18 "time"
19
20 "fmt"
21)
22
23const VERSION = "1.5.0"
24
25type GinkgoConfigType struct {
26 RandomSeed int64
27 RandomizeAllSpecs bool
28 RegexScansFilePath bool
29 FocusString string
30 SkipString string
31 SkipMeasurements bool
32 FailOnPending bool
33 FailFast bool
34 FlakeAttempts int
35 EmitSpecProgress bool
36 DryRun bool
37
38 ParallelNode int
39 ParallelTotal int
40 SyncHost string
41 StreamHost string
42}
43
44var GinkgoConfig = GinkgoConfigType{}
45
46type DefaultReporterConfigType struct {
47 NoColor bool
48 SlowSpecThreshold float64
49 NoisyPendings bool
50 NoisySkippings bool
51 Succinct bool
52 Verbose bool
53 FullTrace bool
54}
55
56var DefaultReporterConfig = DefaultReporterConfigType{}
57
58func processPrefix(prefix string) string {
59 if prefix != "" {
60 prefix = prefix + "."
61 }
62 return prefix
63}
64
65func Flags(flagSet *flag.FlagSet, prefix string, includeParallelFlags bool) {
66 prefix = processPrefix(prefix)
67 flagSet.Int64Var(&(GinkgoConfig.RandomSeed), prefix+"seed", time.Now().Unix(), "The seed used to randomize the spec suite.")
68 flagSet.BoolVar(&(GinkgoConfig.RandomizeAllSpecs), prefix+"randomizeAllSpecs", false, "If set, ginkgo will randomize all specs together. By default, ginkgo only randomizes the top level Describe, Context and When groups.")
69 flagSet.BoolVar(&(GinkgoConfig.SkipMeasurements), prefix+"skipMeasurements", false, "If set, ginkgo will skip any measurement specs.")
70 flagSet.BoolVar(&(GinkgoConfig.FailOnPending), prefix+"failOnPending", false, "If set, ginkgo will mark the test suite as failed if any specs are pending.")
71 flagSet.BoolVar(&(GinkgoConfig.FailFast), prefix+"failFast", false, "If set, ginkgo will stop running a test suite after a failure occurs.")
72
73 flagSet.BoolVar(&(GinkgoConfig.DryRun), prefix+"dryRun", false, "If set, ginkgo will walk the test hierarchy without actually running anything. Best paired with -v.")
74
75 flagSet.StringVar(&(GinkgoConfig.FocusString), prefix+"focus", "", "If set, ginkgo will only run specs that match this regular expression.")
76 flagSet.StringVar(&(GinkgoConfig.SkipString), prefix+"skip", "", "If set, ginkgo will only run specs that do not match this regular expression.")
77
78 flagSet.BoolVar(&(GinkgoConfig.RegexScansFilePath), prefix+"regexScansFilePath", false, "If set, ginkgo regex matching also will look at the file path (code location).")
79
80 flagSet.IntVar(&(GinkgoConfig.FlakeAttempts), prefix+"flakeAttempts", 1, "Make up to this many attempts to run each spec. Please note that if any of the attempts succeed, the suite will not be failed. But any failures will still be recorded.")
81
82 flagSet.BoolVar(&(GinkgoConfig.EmitSpecProgress), prefix+"progress", false, "If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter.")
83
84 if includeParallelFlags {
85 flagSet.IntVar(&(GinkgoConfig.ParallelNode), prefix+"parallel.node", 1, "This worker node's (one-indexed) node number. For running specs in parallel.")
86 flagSet.IntVar(&(GinkgoConfig.ParallelTotal), prefix+"parallel.total", 1, "The total number of worker nodes. For running specs in parallel.")
87 flagSet.StringVar(&(GinkgoConfig.SyncHost), prefix+"parallel.synchost", "", "The address for the server that will synchronize the running nodes.")
88 flagSet.StringVar(&(GinkgoConfig.StreamHost), prefix+"parallel.streamhost", "", "The address for the server that the running nodes should stream data to.")
89 }
90
91 flagSet.BoolVar(&(DefaultReporterConfig.NoColor), prefix+"noColor", false, "If set, suppress color output in default reporter.")
92 flagSet.Float64Var(&(DefaultReporterConfig.SlowSpecThreshold), prefix+"slowSpecThreshold", 5.0, "(in seconds) Specs that take longer to run than this threshold are flagged as slow by the default reporter.")
93 flagSet.BoolVar(&(DefaultReporterConfig.NoisyPendings), prefix+"noisyPendings", true, "If set, default reporter will shout about pending tests.")
94 flagSet.BoolVar(&(DefaultReporterConfig.NoisySkippings), prefix+"noisySkippings", true, "If set, default reporter will shout about skipping tests.")
95 flagSet.BoolVar(&(DefaultReporterConfig.Verbose), prefix+"v", false, "If set, default reporter print out all specs as they begin.")
96 flagSet.BoolVar(&(DefaultReporterConfig.Succinct), prefix+"succinct", false, "If set, default reporter prints out a very succinct report")
97 flagSet.BoolVar(&(DefaultReporterConfig.FullTrace), prefix+"trace", false, "If set, default reporter prints out the full stack trace when a failure occurs")
98}
99
100func BuildFlagArgs(prefix string, ginkgo GinkgoConfigType, reporter DefaultReporterConfigType) []string {
101 prefix = processPrefix(prefix)
102 result := make([]string, 0)
103
104 if ginkgo.RandomSeed > 0 {
105 result = append(result, fmt.Sprintf("--%sseed=%d", prefix, ginkgo.RandomSeed))
106 }
107
108 if ginkgo.RandomizeAllSpecs {
109 result = append(result, fmt.Sprintf("--%srandomizeAllSpecs", prefix))
110 }
111
112 if ginkgo.SkipMeasurements {
113 result = append(result, fmt.Sprintf("--%sskipMeasurements", prefix))
114 }
115
116 if ginkgo.FailOnPending {
117 result = append(result, fmt.Sprintf("--%sfailOnPending", prefix))
118 }
119
120 if ginkgo.FailFast {
121 result = append(result, fmt.Sprintf("--%sfailFast", prefix))
122 }
123
124 if ginkgo.DryRun {
125 result = append(result, fmt.Sprintf("--%sdryRun", prefix))
126 }
127
128 if ginkgo.FocusString != "" {
129 result = append(result, fmt.Sprintf("--%sfocus=%s", prefix, ginkgo.FocusString))
130 }
131
132 if ginkgo.SkipString != "" {
133 result = append(result, fmt.Sprintf("--%sskip=%s", prefix, ginkgo.SkipString))
134 }
135
136 if ginkgo.FlakeAttempts > 1 {
137 result = append(result, fmt.Sprintf("--%sflakeAttempts=%d", prefix, ginkgo.FlakeAttempts))
138 }
139
140 if ginkgo.EmitSpecProgress {
141 result = append(result, fmt.Sprintf("--%sprogress", prefix))
142 }
143
144 if ginkgo.ParallelNode != 0 {
145 result = append(result, fmt.Sprintf("--%sparallel.node=%d", prefix, ginkgo.ParallelNode))
146 }
147
148 if ginkgo.ParallelTotal != 0 {
149 result = append(result, fmt.Sprintf("--%sparallel.total=%d", prefix, ginkgo.ParallelTotal))
150 }
151
152 if ginkgo.StreamHost != "" {
153 result = append(result, fmt.Sprintf("--%sparallel.streamhost=%s", prefix, ginkgo.StreamHost))
154 }
155
156 if ginkgo.SyncHost != "" {
157 result = append(result, fmt.Sprintf("--%sparallel.synchost=%s", prefix, ginkgo.SyncHost))
158 }
159
160 if ginkgo.RegexScansFilePath {
161 result = append(result, fmt.Sprintf("--%sregexScansFilePath", prefix))
162 }
163
164 if reporter.NoColor {
165 result = append(result, fmt.Sprintf("--%snoColor", prefix))
166 }
167
168 if reporter.SlowSpecThreshold > 0 {
169 result = append(result, fmt.Sprintf("--%sslowSpecThreshold=%.5f", prefix, reporter.SlowSpecThreshold))
170 }
171
172 if !reporter.NoisyPendings {
173 result = append(result, fmt.Sprintf("--%snoisyPendings=false", prefix))
174 }
175
176 if !reporter.NoisySkippings {
177 result = append(result, fmt.Sprintf("--%snoisySkippings=false", prefix))
178 }
179
180 if reporter.Verbose {
181 result = append(result, fmt.Sprintf("--%sv", prefix))
182 }
183
184 if reporter.Succinct {
185 result = append(result, fmt.Sprintf("--%ssuccinct", prefix))
186 }
187
188 if reporter.FullTrace {
189 result = append(result, fmt.Sprintf("--%strace", prefix))
190 }
191
192 return result
193}