cover: add function to parse profiles from an io.Reader
Fixes golang/go#19404
Change-Id: I893fbb999bb8d0a6c62ab8752790fce75912be0c
GitHub-Last-Rev: 101a2f5bd6e2c2e278deafc8238fa4319a697541
GitHub-Pull-Request: golang/tools#331
Reviewed-on: https://go-review.googlesource.com/c/tools/+/336329
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Bryan C. Mills <bcmills@google.com>
Trust: Michael Knyszek <mknyszek@google.com>
diff --git a/cover/profile.go b/cover/profile.go
index 5719577..47a9a54 100644
--- a/cover/profile.go
+++ b/cover/profile.go
@@ -10,6 +10,7 @@
"bufio"
"errors"
"fmt"
+ "io"
"math"
"os"
"sort"
@@ -45,14 +46,18 @@
return nil, err
}
defer pf.Close()
+ return ParseProfilesFromReader(pf)
+}
- files := make(map[string]*Profile)
- buf := bufio.NewReader(pf)
+// ParseProfilesFromReader parses profile data from the Reader and
+// returns a Profile for each source file described therein.
+func ParseProfilesFromReader(rd io.Reader) ([]*Profile, error) {
// First line is "mode: foo", where foo is "set", "count", or "atomic".
// Rest of file is in the format
// encoding/base64/base64.go:34.44,37.40 3 1
// where the fields are: name.go:line.column,line.column numberOfStatements count
- s := bufio.NewScanner(buf)
+ files := make(map[string]*Profile)
+ s := bufio.NewScanner(rd)
mode := ""
for s.Scan() {
line := s.Text()