maddy

git clone git://git.lin.moe/fmaddy/maddy.git

 1//usr/bin/env go run "$0" "$@"; exit $?
 2//
 3// From: https://git.lukeshu.com/go/cmd/gocovcat/
 4//
 5//go:build ignore
 6// +build ignore
 7
 8// Copyright 2017 Luke Shumaker <lukeshu@parabola.nu>
 9//
10// This program is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23// Command gocovcat combines multiple go cover runs, and prints the
24// result on stdout.
25package main
26
27import (
28	"bufio"
29	"fmt"
30	"os"
31	"sort"
32	"strconv"
33	"strings"
34)
35
36func handleErr(err error) {
37	if err != nil {
38		fmt.Fprintf(os.Stderr, "%v\n", err)
39		os.Exit(1)
40	}
41}
42
43func main() {
44	modeBool := false
45	blocks := map[string]int{}
46	for _, filename := range os.Args[1:] {
47		file, err := os.Open(filename)
48		handleErr(err)
49		buf := bufio.NewScanner(file)
50		for buf.Scan() {
51			line := buf.Text()
52
53			if strings.HasPrefix(line, "mode: ") {
54				m := strings.TrimPrefix(line, "mode: ")
55				switch m {
56				case "set":
57					modeBool = true
58				case "count", "atomic":
59					// do nothing
60				default:
61					fmt.Fprintf(os.Stderr, "Unrecognized mode: %s\n", m)
62					os.Exit(1)
63				}
64			} else {
65				sp := strings.LastIndexByte(line, ' ')
66				block := line[:sp]
67				cntStr := line[sp+1:]
68				cnt, err := strconv.Atoi(cntStr)
69				handleErr(err)
70				blocks[block] += cnt
71			}
72		}
73		handleErr(buf.Err())
74	}
75	keys := make([]string, 0, len(blocks))
76	for key := range blocks {
77		keys = append(keys, key)
78	}
79	sort.Strings(keys)
80	modeStr := "count"
81	if modeBool {
82		modeStr = "set"
83	}
84	fmt.Printf("mode: %s\n", modeStr)
85	for _, block := range keys {
86		cnt := blocks[block]
87		if modeBool && cnt > 1 {
88			cnt = 1
89		}
90		fmt.Printf("%s %d\n", block, cnt)
91	}
92}