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.1KB

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