You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
8.5 KiB

  1. /*
  2. Ginkgo accepts a number of configuration options.
  3. These are documented [here](http://onsi.github.io/ginkgo/#the-ginkgo-cli)
  4. You can also learn more via
  5. ginkgo help
  6. or (I kid you not):
  7. go test -asdf
  8. */
  9. package config
  10. import (
  11. "flag"
  12. "time"
  13. "fmt"
  14. )
  15. const VERSION = "1.16.2"
  16. type GinkgoConfigType struct {
  17. RandomSeed int64
  18. RandomizeAllSpecs bool
  19. RegexScansFilePath bool
  20. FocusStrings []string
  21. SkipStrings []string
  22. SkipMeasurements bool
  23. FailOnPending bool
  24. FailFast bool
  25. FlakeAttempts int
  26. EmitSpecProgress bool
  27. DryRun bool
  28. DebugParallel bool
  29. ParallelNode int
  30. ParallelTotal int
  31. SyncHost string
  32. StreamHost string
  33. }
  34. var GinkgoConfig = GinkgoConfigType{}
  35. type DefaultReporterConfigType struct {
  36. NoColor bool
  37. SlowSpecThreshold float64
  38. NoisyPendings bool
  39. NoisySkippings bool
  40. Succinct bool
  41. Verbose bool
  42. FullTrace bool
  43. ReportPassed bool
  44. ReportFile string
  45. }
  46. var DefaultReporterConfig = DefaultReporterConfigType{}
  47. func processPrefix(prefix string) string {
  48. if prefix != "" {
  49. prefix += "."
  50. }
  51. return prefix
  52. }
  53. type flagFunc func(string)
  54. func (f flagFunc) String() string { return "" }
  55. func (f flagFunc) Set(s string) error { f(s); return nil }
  56. func Flags(flagSet *flag.FlagSet, prefix string, includeParallelFlags bool) {
  57. prefix = processPrefix(prefix)
  58. flagSet.Int64Var(&(GinkgoConfig.RandomSeed), prefix+"seed", time.Now().Unix(), "The seed used to randomize the spec suite.")
  59. 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.")
  60. flagSet.BoolVar(&(GinkgoConfig.SkipMeasurements), prefix+"skipMeasurements", false, "If set, ginkgo will skip any measurement specs.")
  61. flagSet.BoolVar(&(GinkgoConfig.FailOnPending), prefix+"failOnPending", false, "If set, ginkgo will mark the test suite as failed if any specs are pending.")
  62. flagSet.BoolVar(&(GinkgoConfig.FailFast), prefix+"failFast", false, "If set, ginkgo will stop running a test suite after a failure occurs.")
  63. flagSet.BoolVar(&(GinkgoConfig.DryRun), prefix+"dryRun", false, "If set, ginkgo will walk the test hierarchy without actually running anything. Best paired with -v.")
  64. flagSet.Var(flagFunc(flagFocus), prefix+"focus", "If set, ginkgo will only run specs that match this regular expression. Can be specified multiple times, values are ORed.")
  65. flagSet.Var(flagFunc(flagSkip), prefix+"skip", "If set, ginkgo will only run specs that do not match this regular expression. Can be specified multiple times, values are ORed.")
  66. flagSet.BoolVar(&(GinkgoConfig.RegexScansFilePath), prefix+"regexScansFilePath", false, "If set, ginkgo regex matching also will look at the file path (code location).")
  67. 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.")
  68. flagSet.BoolVar(&(GinkgoConfig.EmitSpecProgress), prefix+"progress", false, "If set, ginkgo will emit progress information as each spec runs to the GinkgoWriter.")
  69. flagSet.BoolVar(&(GinkgoConfig.DebugParallel), prefix+"debug", false, "If set, ginkgo will emit node output to files when running in parallel.")
  70. if includeParallelFlags {
  71. flagSet.IntVar(&(GinkgoConfig.ParallelNode), prefix+"parallel.node", 1, "This worker node's (one-indexed) node number. For running specs in parallel.")
  72. flagSet.IntVar(&(GinkgoConfig.ParallelTotal), prefix+"parallel.total", 1, "The total number of worker nodes. For running specs in parallel.")
  73. flagSet.StringVar(&(GinkgoConfig.SyncHost), prefix+"parallel.synchost", "", "The address for the server that will synchronize the running nodes.")
  74. flagSet.StringVar(&(GinkgoConfig.StreamHost), prefix+"parallel.streamhost", "", "The address for the server that the running nodes should stream data to.")
  75. }
  76. flagSet.BoolVar(&(DefaultReporterConfig.NoColor), prefix+"noColor", false, "If set, suppress color output in default reporter.")
  77. 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.")
  78. flagSet.BoolVar(&(DefaultReporterConfig.NoisyPendings), prefix+"noisyPendings", true, "If set, default reporter will shout about pending tests.")
  79. flagSet.BoolVar(&(DefaultReporterConfig.NoisySkippings), prefix+"noisySkippings", true, "If set, default reporter will shout about skipping tests.")
  80. flagSet.BoolVar(&(DefaultReporterConfig.Verbose), prefix+"v", false, "If set, default reporter print out all specs as they begin.")
  81. flagSet.BoolVar(&(DefaultReporterConfig.Succinct), prefix+"succinct", false, "If set, default reporter prints out a very succinct report")
  82. flagSet.BoolVar(&(DefaultReporterConfig.FullTrace), prefix+"trace", false, "If set, default reporter prints out the full stack trace when a failure occurs")
  83. flagSet.BoolVar(&(DefaultReporterConfig.ReportPassed), prefix+"reportPassed", false, "If set, default reporter prints out captured output of passed tests.")
  84. flagSet.StringVar(&(DefaultReporterConfig.ReportFile), prefix+"reportFile", "", "Override the default reporter output file path.")
  85. }
  86. func BuildFlagArgs(prefix string, ginkgo GinkgoConfigType, reporter DefaultReporterConfigType) []string {
  87. prefix = processPrefix(prefix)
  88. result := make([]string, 0)
  89. if ginkgo.RandomSeed > 0 {
  90. result = append(result, fmt.Sprintf("--%sseed=%d", prefix, ginkgo.RandomSeed))
  91. }
  92. if ginkgo.RandomizeAllSpecs {
  93. result = append(result, fmt.Sprintf("--%srandomizeAllSpecs", prefix))
  94. }
  95. if ginkgo.SkipMeasurements {
  96. result = append(result, fmt.Sprintf("--%sskipMeasurements", prefix))
  97. }
  98. if ginkgo.FailOnPending {
  99. result = append(result, fmt.Sprintf("--%sfailOnPending", prefix))
  100. }
  101. if ginkgo.FailFast {
  102. result = append(result, fmt.Sprintf("--%sfailFast", prefix))
  103. }
  104. if ginkgo.DryRun {
  105. result = append(result, fmt.Sprintf("--%sdryRun", prefix))
  106. }
  107. for _, s := range ginkgo.FocusStrings {
  108. result = append(result, fmt.Sprintf("--%sfocus=%s", prefix, s))
  109. }
  110. for _, s := range ginkgo.SkipStrings {
  111. result = append(result, fmt.Sprintf("--%sskip=%s", prefix, s))
  112. }
  113. if ginkgo.FlakeAttempts > 1 {
  114. result = append(result, fmt.Sprintf("--%sflakeAttempts=%d", prefix, ginkgo.FlakeAttempts))
  115. }
  116. if ginkgo.EmitSpecProgress {
  117. result = append(result, fmt.Sprintf("--%sprogress", prefix))
  118. }
  119. if ginkgo.DebugParallel {
  120. result = append(result, fmt.Sprintf("--%sdebug", prefix))
  121. }
  122. if ginkgo.ParallelNode != 0 {
  123. result = append(result, fmt.Sprintf("--%sparallel.node=%d", prefix, ginkgo.ParallelNode))
  124. }
  125. if ginkgo.ParallelTotal != 0 {
  126. result = append(result, fmt.Sprintf("--%sparallel.total=%d", prefix, ginkgo.ParallelTotal))
  127. }
  128. if ginkgo.StreamHost != "" {
  129. result = append(result, fmt.Sprintf("--%sparallel.streamhost=%s", prefix, ginkgo.StreamHost))
  130. }
  131. if ginkgo.SyncHost != "" {
  132. result = append(result, fmt.Sprintf("--%sparallel.synchost=%s", prefix, ginkgo.SyncHost))
  133. }
  134. if ginkgo.RegexScansFilePath {
  135. result = append(result, fmt.Sprintf("--%sregexScansFilePath", prefix))
  136. }
  137. if reporter.NoColor {
  138. result = append(result, fmt.Sprintf("--%snoColor", prefix))
  139. }
  140. if reporter.SlowSpecThreshold > 0 {
  141. result = append(result, fmt.Sprintf("--%sslowSpecThreshold=%.5f", prefix, reporter.SlowSpecThreshold))
  142. }
  143. if !reporter.NoisyPendings {
  144. result = append(result, fmt.Sprintf("--%snoisyPendings=false", prefix))
  145. }
  146. if !reporter.NoisySkippings {
  147. result = append(result, fmt.Sprintf("--%snoisySkippings=false", prefix))
  148. }
  149. if reporter.Verbose {
  150. result = append(result, fmt.Sprintf("--%sv", prefix))
  151. }
  152. if reporter.Succinct {
  153. result = append(result, fmt.Sprintf("--%ssuccinct", prefix))
  154. }
  155. if reporter.FullTrace {
  156. result = append(result, fmt.Sprintf("--%strace", prefix))
  157. }
  158. if reporter.ReportPassed {
  159. result = append(result, fmt.Sprintf("--%sreportPassed", prefix))
  160. }
  161. if reporter.ReportFile != "" {
  162. result = append(result, fmt.Sprintf("--%sreportFile=%s", prefix, reporter.ReportFile))
  163. }
  164. return result
  165. }
  166. // flagFocus implements the -focus flag.
  167. func flagFocus(arg string) {
  168. if arg != "" {
  169. GinkgoConfig.FocusStrings = append(GinkgoConfig.FocusStrings, arg)
  170. }
  171. }
  172. // flagSkip implements the -skip flag.
  173. func flagSkip(arg string) {
  174. if arg != "" {
  175. GinkgoConfig.SkipStrings = append(GinkgoConfig.SkipStrings, arg)
  176. }
  177. }