This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 101
/
packages.go
82 lines (75 loc) · 1.82 KB
/
packages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gocovutil
import (
"encoding/json"
"github.com/axw/gocov"
"io/ioutil"
"os"
"sort"
)
// Packages represents a set of gocov.Package structures.
// The "AddPackage" method may be used to merge package
// coverage results into the set.
type Packages []*gocov.Package
// AddPackage adds a package's coverage information to the
func (ps *Packages) AddPackage(p *gocov.Package) {
i := sort.Search(len(*ps), func(i int) bool {
return (*ps)[i].Name >= p.Name
})
if i < len(*ps) && (*ps)[i].Name == p.Name {
(*ps)[i].Accumulate(p)
} else {
head := (*ps)[:i]
tail := append([]*gocov.Package{p}, (*ps)[i:]...)
*ps = append(head, tail...)
}
}
// ReadPackages takes a list of filenames and parses their
// contents as a Packages object.
//
// The special filename "-" may be used to indicate standard input.
// Duplicate filenames are ignored.
func ReadPackages(filenames []string) (ps Packages, err error) {
copy_ := make([]string, len(filenames))
copy(copy_, filenames)
filenames = copy_
sort.Strings(filenames)
// Eliminate duplicates.
unique := []string{filenames[0]}
if len(filenames) > 1 {
for _, f := range filenames[1:] {
if f != unique[len(unique)-1] {
unique = append(unique, f)
}
}
}
// Open files.
var files []*os.File
for _, f := range filenames {
if f == "-" {
files = append(files, os.Stdin)
} else {
file, err := os.Open(f)
if err != nil {
return nil, err
}
defer file.Close()
files = append(files, os.Stdin)
}
}
// Parse the files, accumulate Packages.
for _, file := range files {
data, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
result := &struct{ Packages []*gocov.Package }{}
err = json.Unmarshal(data, result)
if err != nil {
return nil, err
}
for _, p := range result.Packages {
ps.AddPackage(p)
}
}
return ps, nil
}