1
2
3
4
5 package test
6
7 import (
8 "cmd/go/internal/base"
9 "fmt"
10 "io"
11 "os"
12 "path/filepath"
13 "sync"
14 )
15
16 var coverMerge struct {
17 f *os.File
18 sync.Mutex
19 }
20
21
22
23
24
25 func initCoverProfile() {
26 if testCoverProfile == "" || testC {
27 return
28 }
29 if !filepath.IsAbs(testCoverProfile) {
30 testCoverProfile = filepath.Join(testOutputDir.getAbs(), testCoverProfile)
31 }
32
33
34 f, err := os.Create(testCoverProfile)
35 if err != nil {
36 base.Fatalf("%v", err)
37 }
38 _, err = fmt.Fprintf(f, "mode: %s\n", testCoverMode)
39 if err != nil {
40 base.Fatalf("%v", err)
41 }
42 coverMerge.f = f
43 }
44
45
46
47 func mergeCoverProfile(ew io.Writer, file string) {
48 if coverMerge.f == nil {
49 return
50 }
51 coverMerge.Lock()
52 defer coverMerge.Unlock()
53
54 expect := fmt.Sprintf("mode: %s\n", testCoverMode)
55 buf := make([]byte, len(expect))
56 r, err := os.Open(file)
57 if err != nil {
58
59 return
60 }
61 defer r.Close()
62
63 n, err := io.ReadFull(r, buf)
64 if n == 0 {
65 return
66 }
67 if err != nil || string(buf) != expect {
68 fmt.Fprintf(ew, "error: test wrote malformed coverage profile.\n")
69 return
70 }
71 _, err = io.Copy(coverMerge.f, r)
72 if err != nil {
73 fmt.Fprintf(ew, "error: saving coverage profile: %v\n", err)
74 }
75 }
76
77 func closeCoverProfile() {
78 if coverMerge.f == nil {
79 return
80 }
81 if err := coverMerge.f.Close(); err != nil {
82 base.Errorf("closing coverage profile: %v", err)
83 }
84 }
85
View as plain text