upload.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "go-common/app/admin/main/apm/model/ut"
  10. "go-common/library/log"
  11. "github.com/smartystreets/goconvey/web/server/contract"
  12. "github.com/smartystreets/goconvey/web/server/parser"
  13. )
  14. // Upload upload file to bfs with no filename
  15. func (s *Service) Upload(c context.Context, fileType string, expire int64, body []byte) (url string, err error) {
  16. if len(body) == 0 {
  17. err = fmt.Errorf("s.Upload error(empty file)")
  18. return
  19. }
  20. if url, err = s.dao.UploadProxy(c, fileType, expire, body); err != nil {
  21. log.Error("s.Upload error(%v)", err)
  22. }
  23. return
  24. }
  25. // ParseContent parse go test output to go convey result json.
  26. // If result not contains "=== RUN", that means execute ut err.
  27. func (s *Service) ParseContent(c context.Context, body []byte) (content []byte, err error) {
  28. var (
  29. text = string(body)
  30. index = strings.Index(text, "=== RUN")
  31. )
  32. if index == -1 {
  33. err = fmt.Errorf(text)
  34. return
  35. }
  36. var res = new(contract.PackageResult)
  37. parser.ParsePackageResults(res, text[index:])
  38. if content, err = json.Marshal(res); err != nil {
  39. log.Error("service.Upload json.Marshal err (%v)", err)
  40. }
  41. return
  42. }
  43. // CalcCount calc count
  44. func (s *Service) CalcCount(c context.Context, body []byte) (pkg *ut.PkgAnls, err error) {
  45. res := new(contract.PackageResult)
  46. pkg = new(ut.PkgAnls)
  47. if err = json.Unmarshal(body, res); err != nil {
  48. log.Error("service.CalcCount json.Unmarshal err (%v)", err)
  49. return
  50. }
  51. pkg.Coverage = res.Coverage * 100
  52. for _, v := range res.TestResults {
  53. if len(v.Stories) == 0 {
  54. pkg.Assertions++
  55. if v.Error != "" {
  56. pkg.Panics++
  57. } else if !v.Passed {
  58. pkg.Failures++
  59. } else if v.Skipped {
  60. pkg.Skipped++
  61. } else {
  62. pkg.Passed++
  63. }
  64. }
  65. for _, story := range v.Stories {
  66. for _, ass := range story.Assertions {
  67. pkg.Assertions++
  68. if ass.Skipped {
  69. pkg.Skipped++
  70. } else if ass.Failure != "" {
  71. pkg.Failures++
  72. } else if ass.Error != nil {
  73. pkg.Panics++
  74. } else {
  75. pkg.Passed++
  76. }
  77. }
  78. }
  79. }
  80. return
  81. }
  82. // CalcCountFiles calculating lines and statements in files
  83. func (s *Service) CalcCountFiles(c context.Context, res *ut.UploadRes, body []byte) (utFiles []*ut.File, err error) {
  84. var (
  85. fblocks = make(map[string][]ut.Block)
  86. data = strings.Split(string(body[:]), "\n")
  87. reg = regexp.MustCompile(`^(.+):([0-9]+).([0-9]+),([0-9]+).([0-9]+) ([0-9]+) ([0-9]+)$`)
  88. )
  89. if !strings.HasPrefix(data[0], "mode:") {
  90. return nil, fmt.Errorf("Wrong cover.dat/cover.out file format")
  91. }
  92. for i := 1; i < len(data); i++ {
  93. if data[i] == "" {
  94. continue
  95. }
  96. m := reg.FindStringSubmatch(data[i])
  97. if m == nil {
  98. return nil, fmt.Errorf("line %s doesn't match expected format: %v", data[i], reg)
  99. }
  100. b := ut.Block{
  101. Start: toInt(m[2]),
  102. End: toInt(m[4]),
  103. Statements: toInt(m[6]),
  104. Count: toInt(m[7]),
  105. }
  106. fblocks[m[1]] = append(fblocks[m[1]], b)
  107. }
  108. for name, blocks := range fblocks {
  109. utFile := &ut.File{
  110. Name: name,
  111. CommitID: res.CommitID,
  112. PKG: res.PKG,
  113. }
  114. for i := 0; i < len(blocks); i++ {
  115. utFile.Statements += int64(blocks[i].Statements)
  116. if blocks[i].Count > 0 {
  117. utFile.CoveredStatements += int64(blocks[i].Statements)
  118. }
  119. }
  120. utFiles = append(utFiles, utFile)
  121. }
  122. return
  123. }
  124. // Assist functions
  125. func toInt(s string) int {
  126. i, err := strconv.Atoi(s)
  127. if err != nil {
  128. log.Error("strconv.Atoi(%s) error(%v)", s, err)
  129. }
  130. return i
  131. }