...
1
16
17 package cov
18
19 import (
20 "errors"
21 "fmt"
22 "golang.org/x/tools/cover"
23 "io"
24 )
25
26
27 func DumpProfile(profiles []*cover.Profile, writer io.Writer) error {
28 if len(profiles) == 0 {
29 return errors.New("can't write an empty profile")
30 }
31 if _, err := io.WriteString(writer, "mode: "+profiles[0].Mode+"\n"); err != nil {
32 return err
33 }
34 for _, profile := range profiles {
35 for _, block := range profile.Blocks {
36 if _, err := fmt.Fprintf(writer, "%s:%d.%d,%d.%d %d %d\n", profile.FileName, block.StartLine, block.StartCol, block.EndLine, block.EndCol, block.NumStmt, block.Count); err != nil {
37 return err
38 }
39 }
40 }
41 return nil
42 }
43
44 func deepCopyProfile(profile cover.Profile) cover.Profile {
45 p := profile
46 p.Blocks = make([]cover.ProfileBlock, len(profile.Blocks))
47 copy(p.Blocks, profile.Blocks)
48 return p
49 }
50
51
52
53 func blocksEqual(a cover.ProfileBlock, b cover.ProfileBlock) bool {
54 return a.StartCol == b.StartCol && a.StartLine == b.StartLine &&
55 a.EndCol == b.EndCol && a.EndLine == b.EndLine && a.NumStmt == b.NumStmt
56 }
57
58 func ensureProfilesMatch(a *cover.Profile, b *cover.Profile) error {
59 if a.FileName != b.FileName {
60 return fmt.Errorf("coverage filename mismatch (%s vs %s)", a.FileName, b.FileName)
61 }
62 if len(a.Blocks) != len(b.Blocks) {
63 return fmt.Errorf("file block count for %s mismatches (%d vs %d)", a.FileName, len(a.Blocks), len(b.Blocks))
64 }
65 if a.Mode != b.Mode {
66 return fmt.Errorf("mode for %s mismatches (%s vs %s)", a.FileName, a.Mode, b.Mode)
67 }
68 for i, ba := range a.Blocks {
69 bb := b.Blocks[i]
70 if !blocksEqual(ba, bb) {
71 return fmt.Errorf("coverage block mismatch: block #%d for %s (%+v mismatches %+v)", i, a.FileName, ba, bb)
72 }
73 }
74 return nil
75 }
76
View as plain text