Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit dd288a8

Browse files
authored
Move Convert to a sub-package (#98)
Move Convert to a sub-package to make it possible to run from other tools as an API
1 parent 12cc591 commit dd288a8

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
// IN THE SOFTWARE.
2020

21-
package main
21+
package convert
2222

2323
import (
24+
"bytes"
25+
"encoding/json"
2426
"fmt"
2527
"go/ast"
2628
"go/build"
2729
"go/parser"
2830
"go/token"
29-
"os"
31+
"io"
3032
"path/filepath"
3133
"strings"
3234

@@ -35,9 +37,13 @@ import (
3537
"golang.org/x/tools/cover"
3638
)
3739

40+
func marshalJson(w io.Writer, packages []*gocov.Package) error {
41+
return json.NewEncoder(w).Encode(struct{ Packages []*gocov.Package }{packages})
42+
}
43+
3844
type packagesCache map[string]*build.Package
3945

40-
func convertProfiles(filenames ...string) error {
46+
func ConvertProfiles(filenames ...string) ([]byte, error) {
4147
var (
4248
ps gocovutil.Packages
4349
packages = make(packagesCache)
@@ -49,22 +55,23 @@ func convertProfiles(filenames ...string) error {
4955
}
5056
profiles, err := cover.ParseProfiles(filenames[i])
5157
if err != nil {
52-
return err
58+
return nil, err
5359
}
5460
for _, p := range profiles {
5561
if err := converter.convertProfile(packages, p); err != nil {
56-
return err
62+
return nil, err
5763
}
5864
}
5965

6066
for _, pkg := range converter.packages {
6167
ps.AddPackage(pkg)
6268
}
6369
}
64-
if err := marshalJson(os.Stdout, ps); err != nil {
65-
return err
70+
buf := bytes.Buffer{}
71+
if err := marshalJson(&buf, ps); err != nil {
72+
return nil, err
6673
}
67-
return nil
74+
return buf.Bytes(), nil
6875
}
6976

7077
type converter struct {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package convert
22

33
import (
44
"go/ast"

gocov/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"os"
2929

3030
"github.com/axw/gocov"
31+
"github.com/axw/gocov/gocov/convert"
3132
)
3233

3334
func usage() {
@@ -68,10 +69,12 @@ func main() {
6869
fmt.Fprintln(os.Stderr, "missing cover profile")
6970
os.Exit(1)
7071
}
71-
if err := convertProfiles(flag.Args()[1:]...); err != nil {
72+
out, err := convert.ConvertProfiles(flag.Args()[1:]...)
73+
if err != nil {
7274
fmt.Fprintln(os.Stderr, "error:", err)
7375
os.Exit(1)
7476
}
77+
os.Stdout.Write(out)
7578
case "annotate":
7679
os.Exit(annotateSource())
7780
case "report":

gocov/test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"path/filepath"
3131
"strings"
3232

33+
"github.com/axw/gocov/gocov/convert"
3334
"github.com/axw/gocov/gocov/internal/testflag"
3435
)
3536

@@ -100,5 +101,7 @@ func runTests(args []string) error {
100101
}
101102

102103
// Merge the profiles.
103-
return convertProfiles(files...)
104+
out, err := convert.ConvertProfiles(files...)
105+
os.Stdout.Write(out)
106+
return err
104107
}

0 commit comments

Comments
 (0)