forked from foxcpp/maddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocovcat.go
91 lines (85 loc) · 2.1 KB
/
gocovcat.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
83
84
85
86
87
88
89
90
91
//usr/bin/env go run "$0" "$@"; exit $?
//
// From: https://git.lukeshu.com/go/cmd/gocovcat/
//
// +build ignore
// Copyright 2017 Luke Shumaker <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Command gocovcat combines multiple go cover runs, and prints the
// result on stdout.
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
)
func handleErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func main() {
modeBool := false
blocks := map[string]int{}
for _, filename := range os.Args[1:] {
file, err := os.Open(filename)
handleErr(err)
buf := bufio.NewScanner(file)
for buf.Scan() {
line := buf.Text()
if strings.HasPrefix(line, "mode: ") {
m := strings.TrimPrefix(line, "mode: ")
switch m {
case "set":
modeBool = true
case "count", "atomic":
// do nothing
default:
fmt.Fprintf(os.Stderr, "Unrecognized mode: %s\n", m)
os.Exit(1)
}
} else {
sp := strings.LastIndexByte(line, ' ')
block := line[:sp]
cntStr := line[sp+1:]
cnt, err := strconv.Atoi(cntStr)
handleErr(err)
blocks[block] += cnt
}
}
handleErr(buf.Err())
}
keys := make([]string, 0, len(blocks))
for key := range blocks {
keys = append(keys, key)
}
sort.Strings(keys)
modeStr := "count"
if modeBool {
modeStr = "set"
}
fmt.Printf("mode: %s\n", modeStr)
for _, block := range keys {
cnt := blocks[block]
if modeBool && cnt > 1 {
cnt = 1
}
fmt.Printf("%s %d\n", block, cnt)
}
}