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.1.0"
24
25type GinkgoConfigType struct {
26	RandomSeed        int64
27	RandomizeAllSpecs bool
28	FocusString       string
29	SkipString        string
30	SkipMeasurements  bool
31	FailOnPending     bool
32	FailFast          bool
33	EmitSpecProgress  bool
34	DryRun            bool
35
36	ParallelNode  int
37	ParallelTotal int
38	SyncHost      string
39	StreamHost    string
40}
41
42var GinkgoConfig = GinkgoConfigType{}
43
44type DefaultReporterConfigType struct {
45	NoColor           bool
46	SlowSpecThreshold float64
47	NoisyPendings     bool
48	Succinct          bool
49	Verbose           bool
50	FullTrace         bool
51}
52
53var DefaultReporterConfig = DefaultReporterConfigType{}
54
55func processPrefix(prefix string) string {
56	if prefix != "" {
57		prefix = prefix + "."
58	}
59	return prefix
60}
61
62func Flags(flagSet *flag.FlagSet, prefix string, includeParallelFlags bool) {
63	prefix = processPrefix(prefix)
64	flagSet.Int64Var(&(GinkgoConfig.RandomSeed), prefix+"seed", time.Now().Unix(), "The seed used to randomize the spec suite.")
65	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 groups.")
66	flagSet.BoolVar(&(GinkgoConfig.SkipMeasurements), prefix+"skipMeasurements", false, "If set, ginkgo will skip any measurement specs.")
67	flagSet.BoolVar(&(GinkgoConfig.FailOnPending), prefix+"failOnPending", false, "If set, ginkgo will mark the test suite as failed if any specs are pending.")
68	flagSet.BoolVar(&(GinkgoConfig.FailFast), prefix+"failFast", false, "If set, ginkgo will stop running a test suite after a failure occurs.")
69	flagSet.BoolVar(&(GinkgoConfig.DryRun), prefix+"dryRun", false, "If set, ginkgo will walk the test hierarchy without actually running anything.  Best paired with -v.")
70	flagSet.StringVar(&(GinkgoConfig.FocusString), prefix+"focus", "", "If set, ginkgo will only run specs that match this regular expression.")
71	flagSet.StringVar(&(GinkgoConfig.SkipString), prefix+"skip", "", "If set, ginkgo will only run specs that do not match this regular expression.")
72	flagSet.BoolVar(&(GinkgoConfig.EmitSpecProgress), prefix+"progress", false, "If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter.")
73
74	if includeParallelFlags {
75		flagSet.IntVar(&(GinkgoConfig.ParallelNode), prefix+"parallel.node", 1, "This worker node's (one-indexed) node number.  For running specs in parallel.")
76		flagSet.IntVar(&(GinkgoConfig.ParallelTotal), prefix+"parallel.total", 1, "The total number of worker nodes.  For running specs in parallel.")
77		flagSet.StringVar(&(GinkgoConfig.SyncHost), prefix+"parallel.synchost", "", "The address for the server that will synchronize the running nodes.")
78		flagSet.StringVar(&(GinkgoConfig.StreamHost), prefix+"parallel.streamhost", "", "The address for the server that the running nodes should stream data to.")
79	}
80
81	flagSet.BoolVar(&(DefaultReporterConfig.NoColor), prefix+"noColor", false, "If set, suppress color output in default reporter.")
82	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 (default: 5 seconds).")
83	flagSet.BoolVar(&(DefaultReporterConfig.NoisyPendings), prefix+"noisyPendings", true, "If set, default reporter will shout about pending tests.")
84	flagSet.BoolVar(&(DefaultReporterConfig.Verbose), prefix+"v", false, "If set, default reporter print out all specs as they begin.")
85	flagSet.BoolVar(&(DefaultReporterConfig.Succinct), prefix+"succinct", false, "If set, default reporter prints out a very succinct report")
86	flagSet.BoolVar(&(DefaultReporterConfig.FullTrace), prefix+"trace", false, "If set, default reporter prints out the full stack trace when a failure occurs")
87}
88
89func BuildFlagArgs(prefix string, ginkgo GinkgoConfigType, reporter DefaultReporterConfigType) []string {
90	prefix = processPrefix(prefix)
91	result := make([]string, 0)
92
93	if ginkgo.RandomSeed > 0 {
94		result = append(result, fmt.Sprintf("--%sseed=%d", prefix, ginkgo.RandomSeed))
95	}
96
97	if ginkgo.RandomizeAllSpecs {
98		result = append(result, fmt.Sprintf("--%srandomizeAllSpecs", prefix))
99	}
100
101	if ginkgo.SkipMeasurements {
102		result = append(result, fmt.Sprintf("--%sskipMeasurements", prefix))
103	}
104
105	if ginkgo.FailOnPending {
106		result = append(result, fmt.Sprintf("--%sfailOnPending", prefix))
107	}
108
109	if ginkgo.FailFast {
110		result = append(result, fmt.Sprintf("--%sfailFast", prefix))
111	}
112
113	if ginkgo.DryRun {
114		result = append(result, fmt.Sprintf("--%sdryRun", prefix))
115	}
116
117	if ginkgo.FocusString != "" {
118		result = append(result, fmt.Sprintf("--%sfocus=%s", prefix, ginkgo.FocusString))
119	}
120
121	if ginkgo.SkipString != "" {
122		result = append(result, fmt.Sprintf("--%sskip=%s", prefix, ginkgo.SkipString))
123	}
124
125	if ginkgo.EmitSpecProgress {
126		result = append(result, fmt.Sprintf("--%sprogress", prefix))
127	}
128
129	if ginkgo.ParallelNode != 0 {
130		result = append(result, fmt.Sprintf("--%sparallel.node=%d", prefix, ginkgo.ParallelNode))
131	}
132
133	if ginkgo.ParallelTotal != 0 {
134		result = append(result, fmt.Sprintf("--%sparallel.total=%d", prefix, ginkgo.ParallelTotal))
135	}
136
137	if ginkgo.StreamHost != "" {
138		result = append(result, fmt.Sprintf("--%sparallel.streamhost=%s", prefix, ginkgo.StreamHost))
139	}
140
141	if ginkgo.SyncHost != "" {
142		result = append(result, fmt.Sprintf("--%sparallel.synchost=%s", prefix, ginkgo.SyncHost))
143	}
144
145	if reporter.NoColor {
146		result = append(result, fmt.Sprintf("--%snoColor", prefix))
147	}
148
149	if reporter.SlowSpecThreshold > 0 {
150		result = append(result, fmt.Sprintf("--%sslowSpecThreshold=%.5f", prefix, reporter.SlowSpecThreshold))
151	}
152
153	if !reporter.NoisyPendings {
154		result = append(result, fmt.Sprintf("--%snoisyPendings=false", prefix))
155	}
156
157	if reporter.Verbose {
158		result = append(result, fmt.Sprintf("--%sv", prefix))
159	}
160
161	if reporter.Succinct {
162		result = append(result, fmt.Sprintf("--%ssuccinct", prefix))
163	}
164
165	if reporter.FullTrace {
166		result = append(result, fmt.Sprintf("--%strace", prefix))
167	}
168
169	return result
170}
171