File: coverage.go

package info (click to toggle)
hub 2.14.2~ds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,376 kB
  • sloc: sh: 1,049; ruby: 857; makefile: 89
file content (53 lines) | stat: -rw-r--r-- 1,086 bytes parent folder | download | duplicates (3)
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
package coverage

import (
	"fmt"
	"io"
	"os"
	"reflect"
	"runtime"
)

var out io.Writer
var seen map[string]bool

func init() {
	var err error
	out, err = os.OpenFile(os.Getenv("HUB_COVERAGE"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		panic(err)
	}
	seen = make(map[string]bool)
}

func Record(data interface{}, i int) {
	_, filename, _, _ := runtime.Caller(1)
	if !seen[filename] {
		seen[filename] = true
		d := reflect.ValueOf(data)
		count := reflect.ValueOf(d.FieldByName("Count").Interface())
		total := count.Len()
		for j := 0; j < total; j++ {
			write(data, j, 0, filename)
		}
	}
	write(data, i, 1, filename)
}

func write(data interface{}, i, count int, filename string) {
	d := reflect.ValueOf(data)
	pos := reflect.ValueOf(d.FieldByName("Pos").Interface())
	numStmt := reflect.ValueOf(d.FieldByName("NumStmt").Interface())

	fmt.Fprintf(
		out,
		"%s:%d.%d,%d.%d %d %d\n",
		filename,
		pos.Index(3*i).Uint(),
		pos.Index(3*i+2).Uint()&0xFFFF,
		pos.Index(3*i+1).Uint(),
		pos.Index(3*i+2).Uint()>>16&0xFFFF,
		numStmt.Index(i).Uint(),
		count,
	)
}