result.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package contract
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "go/build"
  6. "github.com/smartystreets/goconvey/convey/reporting"
  7. "github.com/smartystreets/goconvey/web/server/messaging"
  8. )
  9. type Package struct {
  10. Path string
  11. Name string
  12. Ignored bool
  13. Disabled bool
  14. BuildTags []string
  15. TestArguments []string
  16. Error error
  17. Output string
  18. Result *PackageResult
  19. HasImportCycle bool
  20. }
  21. func NewPackage(folder *messaging.Folder, hasImportCycle bool) *Package {
  22. self := new(Package)
  23. self.Path = folder.Path
  24. self.Name = resolvePackageName(self.Path)
  25. self.Result = NewPackageResult(self.Name)
  26. self.Ignored = folder.Ignored
  27. self.Disabled = folder.Disabled
  28. self.BuildTags = folder.BuildTags
  29. self.TestArguments = folder.TestArguments
  30. self.HasImportCycle = hasImportCycle
  31. return self
  32. }
  33. func (self *Package) Active() bool {
  34. return !self.Disabled && !self.Ignored
  35. }
  36. func (self *Package) HasUsableResult() bool {
  37. return self.Active() && (self.Error == nil || (self.Output != ""))
  38. }
  39. type CompleteOutput struct {
  40. Packages []*PackageResult
  41. Revision string
  42. Paused bool
  43. }
  44. var ( // PackageResult.Outcome values:
  45. Ignored = "ignored"
  46. Disabled = "disabled"
  47. Passed = "passed"
  48. Failed = "failed"
  49. Panicked = "panicked"
  50. BuildFailure = "build failure"
  51. NoTestFiles = "no test files"
  52. NoTestFunctions = "no test functions"
  53. NoGoFiles = "no go code"
  54. TestRunAbortedUnexpectedly = "test run aborted unexpectedly"
  55. )
  56. type PackageResult struct {
  57. PackageName string
  58. Elapsed float64
  59. Coverage float64
  60. Outcome string
  61. BuildOutput string
  62. TestResults []TestResult
  63. }
  64. func NewPackageResult(packageName string) *PackageResult {
  65. self := new(PackageResult)
  66. self.PackageName = packageName
  67. self.TestResults = []TestResult{}
  68. self.Coverage = -1
  69. return self
  70. }
  71. type TestResult struct {
  72. TestName string
  73. Elapsed float64
  74. Passed bool
  75. Skipped bool
  76. File string
  77. Line int
  78. Message string
  79. Error string
  80. Stories []reporting.ScopeResult
  81. RawLines []string `json:",omitempty"`
  82. }
  83. func NewTestResult(testName string) *TestResult {
  84. self := new(TestResult)
  85. self.Stories = []reporting.ScopeResult{}
  86. self.RawLines = []string{}
  87. self.TestName = testName
  88. return self
  89. }
  90. func resolvePackageName(path string) string {
  91. pkg, err := build.ImportDir(path, build.FindOnly)
  92. if err == nil {
  93. return pkg.ImportPath
  94. }
  95. nameArr := strings.Split(path, endGoPath)
  96. return nameArr[len(nameArr)-1]
  97. }
  98. const (
  99. separator = string(filepath.Separator)
  100. endGoPath = separator + "src" + separator
  101. )