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