init.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package convey
  2. import (
  3. "flag"
  4. "os"
  5. "github.com/jtolds/gls"
  6. "github.com/smartystreets/assertions"
  7. "github.com/smartystreets/goconvey/convey/reporting"
  8. )
  9. func init() {
  10. assertions.GoConveyMode(true)
  11. declareFlags()
  12. ctxMgr = gls.NewContextManager()
  13. }
  14. func declareFlags() {
  15. flag.BoolVar(&json, "convey-json", false, "When true, emits results in JSON blocks. Default: 'false'")
  16. flag.BoolVar(&silent, "convey-silent", false, "When true, all output from GoConvey is suppressed.")
  17. flag.BoolVar(&story, "convey-story", false, "When true, emits story output, otherwise emits dot output. When not provided, this flag mirrors the value of the '-test.v' flag")
  18. if noStoryFlagProvided() {
  19. story = verboseEnabled
  20. }
  21. // FYI: flag.Parse() is called from the testing package.
  22. }
  23. func noStoryFlagProvided() bool {
  24. return !story && !storyDisabled
  25. }
  26. func buildReporter() reporting.Reporter {
  27. selectReporter := os.Getenv("GOCONVEY_REPORTER")
  28. switch {
  29. case testReporter != nil:
  30. return testReporter
  31. case json || selectReporter == "json":
  32. return reporting.BuildJsonReporter()
  33. case silent || selectReporter == "silent":
  34. return reporting.BuildSilentReporter()
  35. case selectReporter == "dot":
  36. // Story is turned on when verbose is set, so we need to check for dot reporter first.
  37. return reporting.BuildDotReporter()
  38. case story || selectReporter == "story":
  39. return reporting.BuildStoryReporter()
  40. default:
  41. return reporting.BuildDotReporter()
  42. }
  43. }
  44. var (
  45. ctxMgr *gls.ContextManager
  46. // only set by internal tests
  47. testReporter reporting.Reporter
  48. )
  49. var (
  50. json bool
  51. silent bool
  52. story bool
  53. verboseEnabled = flagFound("-test.v=true")
  54. storyDisabled = flagFound("-story=false")
  55. )
  56. // flagFound parses the command line args manually for flags defined in other
  57. // packages. Like the '-v' flag from the "testing" package, for instance.
  58. func flagFound(flagValue string) bool {
  59. for _, arg := range os.Args {
  60. if arg == flagValue {
  61. return true
  62. }
  63. }
  64. return false
  65. }