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.

gocovmerge.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Copyright (c) 2015, Wade Simmons
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // gocovmerge takes the results from multiple `go test -coverprofile` runs and
  6. // merges them into one profile
  7. //go:build ignore
  8. package main
  9. import (
  10. "flag"
  11. "fmt"
  12. "io"
  13. "log"
  14. "os"
  15. "sort"
  16. "golang.org/x/tools/cover"
  17. )
  18. func mergeProfiles(p, merge *cover.Profile) {
  19. if p.Mode != merge.Mode {
  20. log.Fatalf("cannot merge profiles with different modes")
  21. }
  22. // Since the blocks are sorted, we can keep track of where the last block
  23. // was inserted and only look at the blocks after that as targets for merge
  24. startIndex := 0
  25. for _, b := range merge.Blocks {
  26. startIndex = mergeProfileBlock(p, b, startIndex)
  27. }
  28. }
  29. func mergeProfileBlock(p *cover.Profile, pb cover.ProfileBlock, startIndex int) int {
  30. sortFunc := func(i int) bool {
  31. pi := p.Blocks[i+startIndex]
  32. return pi.StartLine >= pb.StartLine && (pi.StartLine != pb.StartLine || pi.StartCol >= pb.StartCol)
  33. }
  34. i := 0
  35. if sortFunc(i) != true {
  36. i = sort.Search(len(p.Blocks)-startIndex, sortFunc)
  37. }
  38. i += startIndex
  39. if i < len(p.Blocks) && p.Blocks[i].StartLine == pb.StartLine && p.Blocks[i].StartCol == pb.StartCol {
  40. if p.Blocks[i].EndLine != pb.EndLine || p.Blocks[i].EndCol != pb.EndCol {
  41. log.Fatalf("OVERLAP MERGE: %v %v %v", p.FileName, p.Blocks[i], pb)
  42. }
  43. switch p.Mode {
  44. case "set":
  45. p.Blocks[i].Count |= pb.Count
  46. case "count", "atomic":
  47. p.Blocks[i].Count += pb.Count
  48. default:
  49. log.Fatalf("unsupported covermode: '%s'", p.Mode)
  50. }
  51. } else {
  52. if i > 0 {
  53. pa := p.Blocks[i-1]
  54. if pa.EndLine >= pb.EndLine && (pa.EndLine != pb.EndLine || pa.EndCol > pb.EndCol) {
  55. log.Fatalf("OVERLAP BEFORE: %v %v %v", p.FileName, pa, pb)
  56. }
  57. }
  58. if i < len(p.Blocks)-1 {
  59. pa := p.Blocks[i+1]
  60. if pa.StartLine <= pb.StartLine && (pa.StartLine != pb.StartLine || pa.StartCol < pb.StartCol) {
  61. log.Fatalf("OVERLAP AFTER: %v %v %v", p.FileName, pa, pb)
  62. }
  63. }
  64. p.Blocks = append(p.Blocks, cover.ProfileBlock{})
  65. copy(p.Blocks[i+1:], p.Blocks[i:])
  66. p.Blocks[i] = pb
  67. }
  68. return i + 1
  69. }
  70. func addProfile(profiles []*cover.Profile, p *cover.Profile) []*cover.Profile {
  71. i := sort.Search(len(profiles), func(i int) bool { return profiles[i].FileName >= p.FileName })
  72. if i < len(profiles) && profiles[i].FileName == p.FileName {
  73. mergeProfiles(profiles[i], p)
  74. } else {
  75. profiles = append(profiles, nil)
  76. copy(profiles[i+1:], profiles[i:])
  77. profiles[i] = p
  78. }
  79. return profiles
  80. }
  81. func dumpProfiles(profiles []*cover.Profile, out io.Writer) {
  82. if len(profiles) == 0 {
  83. return
  84. }
  85. fmt.Fprintf(out, "mode: %s\n", profiles[0].Mode)
  86. for _, p := range profiles {
  87. for _, b := range p.Blocks {
  88. fmt.Fprintf(out, "%s:%d.%d,%d.%d %d %d\n", p.FileName, b.StartLine, b.StartCol, b.EndLine, b.EndCol, b.NumStmt, b.Count)
  89. }
  90. }
  91. }
  92. func main() {
  93. flag.Parse()
  94. var merged []*cover.Profile
  95. for _, file := range flag.Args() {
  96. profiles, err := cover.ParseProfiles(file)
  97. if err != nil {
  98. log.Fatalf("failed to parse profile '%s': %v", file, err)
  99. }
  100. for _, p := range profiles {
  101. merged = addProfile(merged, p)
  102. }
  103. }
  104. dumpProfiles(merged, os.Stdout)
  105. }