1/*
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.12.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	DebugParallel      bool
38
39	ParallelNode  int
40	ParallelTotal int
41	SyncHost      string
42	StreamHost    string
43}
44
45var GinkgoConfig = GinkgoConfigType{}
46
47type DefaultReporterConfigType struct {
48	NoColor           bool
49	SlowSpecThreshold float64
50	NoisyPendings     bool
51	NoisySkippings    bool
52	Succinct          bool
53	Verbose           bool
54	FullTrace         bool
55	ReportPassed      bool
56	ReportFile        string
57}
58
59var DefaultReporterConfig = DefaultReporterConfigType{}
60
61func processPrefix(prefix string) string {
62	if prefix != "" {
63		prefix += "."
64	}
65	return prefix
66}
67
68func Flags(flagSet *flag.FlagSet, prefix string, includeParallelFlags bool) {
69	prefix = processPrefix(prefix)
70	flagSet.Int64Var(&(GinkgoConfig.RandomSeed), prefix+"seed", time.Now().Unix(), "The seed used to randomize the spec suite.")
71	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.")
72	flagSet.BoolVar(&(GinkgoConfig.SkipMeasurements), prefix+"skipMeasurements", false, "If set, ginkgo will skip any measurement specs.")
73	flagSet.BoolVar(&(GinkgoConfig.FailOnPending), prefix+"failOnPending", false, "If set, ginkgo will mark the test suite as failed if any specs are pending.")
74	flagSet.BoolVar(&(GinkgoConfig.FailFast), prefix+"failFast", false, "If set, ginkgo will stop running a test suite after a failure occurs.")
75
76	flagSet.BoolVar(&(GinkgoConfig.DryRun), prefix+"dryRun", false, "If set, ginkgo will walk the test hierarchy without actually running anything.  Best paired with -v.")
77
78	flagSet.StringVar(&(GinkgoConfig.FocusString), prefix+"focus", "", "If set, ginkgo will only run specs that match this regular expression.")
79	flagSet.StringVar(&(GinkgoConfig.SkipString), prefix+"skip", "", "If set, ginkgo will only run specs that do not match this regular expression.")
80
81	flagSet.BoolVar(&(GinkgoConfig.RegexScansFilePath), prefix+"regexScansFilePath", false, "If set, ginkgo regex matching also will look at the file path (code location).")
82
83	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.")
84
85	flagSet.BoolVar(&(GinkgoConfig.EmitSpecProgress), prefix+"progress", false, "If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter.")
86
87	flagSet.BoolVar(&(GinkgoConfig.DebugParallel), prefix+"debug", false, "If set, ginkgo will emit node output to files when running in parallel.")
88
89	if includeParallelFlags {
90		flagSet.IntVar(&(GinkgoConfig.ParallelNode), prefix+"parallel.node", 1, "This worker node's (one-indexed) node number.  For running specs in parallel.")
91		flagSet.IntVar(&(GinkgoConfig.ParallelTotal), prefix+"parallel.total", 1, "The total number of worker nodes.  For running specs in parallel.")
92		flagSet.StringVar(&(GinkgoConfig.SyncHost), prefix+"parallel.synchost", "", "The address for the server that will synchronize the running nodes.")
93		flagSet.StringVar(&(GinkgoConfig.StreamHost), prefix+"parallel.streamhost", "", "The address for the server that the running nodes should stream data to.")
94	}
95
96	flagSet.BoolVar(&(DefaultReporterConfig.NoColor), prefix+"noColor", false, "If set, suppress color output in default reporter.")
97	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.")
98	flagSet.BoolVar(&(DefaultReporterConfig.NoisyPendings), prefix+"noisyPendings", true, "If set, default reporter will shout about pending tests.")
99	flagSet.BoolVar(&(DefaultReporterConfig.NoisySkippings), prefix+"noisySkippings", true, "If set, default reporter will shout about skipping tests.")
100	flagSet.BoolVar(&(DefaultReporterConfig.Verbose), prefix+"v", false, "If set, default reporter print out all specs as they begin.")
101	flagSet.BoolVar(&(DefaultReporterConfig.Succinct), prefix+"succinct", false, "If set, default reporter prints out a very succinct report")
102	flagSet.BoolVar(&(DefaultReporterConfig.FullTrace), prefix+"trace", false, "If set, default reporter prints out the full stack trace when a failure occurs")
103	flagSet.BoolVar(&(DefaultReporterConfig.ReportPassed), prefix+"reportPassed", false, "If set, default reporter prints out captured output of passed tests.")
104	flagSet.StringVar(&(DefaultReporterConfig.ReportFile), prefix+"reportFile", "", "Override the default reporter output file path.")
105
106}
107
108func BuildFlagArgs(prefix string, ginkgo GinkgoConfigType, reporter DefaultReporterConfigType) []string {
109	prefix = processPrefix(prefix)
110	result := make([]string, 0)
111
112	if ginkgo.RandomSeed > 0 {
113		result = append(result, fmt.Sprintf("--%sseed=%d", prefix, ginkgo.RandomSeed))
114	}
115
116	if ginkgo.RandomizeAllSpecs {
117		result = append(result, fmt.Sprintf("--%srandomizeAllSpecs", prefix))
118	}
119
120	if ginkgo.SkipMeasurements {
121		result = append(result, fmt.Sprintf("--%sskipMeasurements", prefix))
122	}
123
124	if ginkgo.FailOnPending {
125		result = append(result, fmt.Sprintf("--%sfailOnPending", prefix))
126	}
127
128	if ginkgo.FailFast {
129		result = append(result, fmt.Sprintf("--%sfailFast", prefix))
130	}
131
132	if ginkgo.DryRun {
133		result = append(result, fmt.Sprintf("--%sdryRun", prefix))
134	}
135
136	if ginkgo.FocusString != "" {
137		result = append(result, fmt.Sprintf("--%sfocus=%s", prefix, ginkgo.FocusString))
138	}
139
140	if ginkgo.SkipString != "" {
141		result = append(result, fmt.Sprintf("--%sskip=%s", prefix, ginkgo.SkipString))
142	}
143
144	if ginkgo.FlakeAttempts > 1 {
145		result = append(result, fmt.Sprintf("--%sflakeAttempts=%d", prefix, ginkgo.FlakeAttempts))
146	}
147
148	if ginkgo.EmitSpecProgress {
149		result = append(result, fmt.Sprintf("--%sprogress", prefix))
150	}
151
152	if ginkgo.DebugParallel {
153		result = append(result, fmt.Sprintf("--%sdebug", prefix))
154	}
155
156	if ginkgo.ParallelNode != 0 {
157		result = append(result, fmt.Sprintf("--%sparallel.node=%d", prefix, ginkgo.ParallelNode))
158	}
159
160	if ginkgo.ParallelTotal != 0 {
161		result = append(result, fmt.Sprintf("--%sparallel.total=%d", prefix, ginkgo.ParallelTotal))
162	}
163
164	if ginkgo.StreamHost != "" {
165		result = append(result, fmt.Sprintf("--%sparallel.streamhost=%s", prefix, ginkgo.StreamHost))
166	}
167
168	if ginkgo.SyncHost != "" {
169		result = append(result, fmt.Sprintf("--%sparallel.synchost=%s", prefix, ginkgo.SyncHost))
170	}
171
172	if ginkgo.RegexScansFilePath {
173		result = append(result, fmt.Sprintf("--%sregexScansFilePath", prefix))
174	}
175
176	if reporter.NoColor {
177		result = append(result, fmt.Sprintf("--%snoColor", prefix))
178	}
179
180	if reporter.SlowSpecThreshold > 0 {
181		result = append(result, fmt.Sprintf("--%sslowSpecThreshold=%.5f", prefix, reporter.SlowSpecThreshold))
182	}
183
184	if !reporter.NoisyPendings {
185		result = append(result, fmt.Sprintf("--%snoisyPendings=false", prefix))
186	}
187
188	if !reporter.NoisySkippings {
189		result = append(result, fmt.Sprintf("--%snoisySkippings=false", prefix))
190	}
191
192	if reporter.Verbose {
193		result = append(result, fmt.Sprintf("--%sv", prefix))
194	}
195
196	if reporter.Succinct {
197		result = append(result, fmt.Sprintf("--%ssuccinct", prefix))
198	}
199
200	if reporter.FullTrace {
201		result = append(result, fmt.Sprintf("--%strace", prefix))
202	}
203
204	if reporter.ReportPassed {
205		result = append(result, fmt.Sprintf("--%sreportPassed", prefix))
206	}
207
208	if reporter.ReportFile != "" {
209		result = append(result, fmt.Sprintf("--%sreportFile=%s", prefix, reporter.ReportFile))
210	}
211
212	return result
213}
214