From dd288a8c01a9de4d8805bd39c59ebaf21a619ad4 Mon Sep 17 00:00:00 2001 From: Karel Bilek Date: Sun, 12 Feb 2023 03:26:23 +0100 Subject: [PATCH] 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 --- gocov/{ => convert}/convert.go | 23 +++++++++++++++-------- gocov/{ => convert}/convert_test.go | 2 +- gocov/main.go | 5 ++++- gocov/test.go | 5 ++++- 4 files changed, 24 insertions(+), 11 deletions(-) rename gocov/{ => convert}/convert.go (95%) rename gocov/{ => convert}/convert_test.go (98%) diff --git a/gocov/convert.go b/gocov/convert/convert.go similarity index 95% rename from gocov/convert.go rename to gocov/convert/convert.go index 60955e9..ec45bc0 100644 --- a/gocov/convert.go +++ b/gocov/convert/convert.go @@ -18,15 +18,17 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS // IN THE SOFTWARE. -package main +package convert import ( + "bytes" + "encoding/json" "fmt" "go/ast" "go/build" "go/parser" "go/token" - "os" + "io" "path/filepath" "strings" @@ -35,9 +37,13 @@ import ( "golang.org/x/tools/cover" ) +func marshalJson(w io.Writer, packages []*gocov.Package) error { + return json.NewEncoder(w).Encode(struct{ Packages []*gocov.Package }{packages}) +} + type packagesCache map[string]*build.Package -func convertProfiles(filenames ...string) error { +func ConvertProfiles(filenames ...string) ([]byte, error) { var ( ps gocovutil.Packages packages = make(packagesCache) @@ -49,11 +55,11 @@ func convertProfiles(filenames ...string) error { } profiles, err := cover.ParseProfiles(filenames[i]) if err != nil { - return err + return nil, err } for _, p := range profiles { if err := converter.convertProfile(packages, p); err != nil { - return err + return nil, err } } @@ -61,10 +67,11 @@ func convertProfiles(filenames ...string) error { ps.AddPackage(pkg) } } - if err := marshalJson(os.Stdout, ps); err != nil { - return err + buf := bytes.Buffer{} + if err := marshalJson(&buf, ps); err != nil { + return nil, err } - return nil + return buf.Bytes(), nil } type converter struct { diff --git a/gocov/convert_test.go b/gocov/convert/convert_test.go similarity index 98% rename from gocov/convert_test.go rename to gocov/convert/convert_test.go index 0a96a4b..bc0155c 100644 --- a/gocov/convert_test.go +++ b/gocov/convert/convert_test.go @@ -1,4 +1,4 @@ -package main +package convert import ( "go/ast" diff --git a/gocov/main.go b/gocov/main.go index c55a2c9..838e0bd 100644 --- a/gocov/main.go +++ b/gocov/main.go @@ -28,6 +28,7 @@ import ( "os" "github.com/axw/gocov" + "github.com/axw/gocov/gocov/convert" ) func usage() { @@ -68,10 +69,12 @@ func main() { fmt.Fprintln(os.Stderr, "missing cover profile") os.Exit(1) } - if err := convertProfiles(flag.Args()[1:]...); err != nil { + out, err := convert.ConvertProfiles(flag.Args()[1:]...) + if err != nil { fmt.Fprintln(os.Stderr, "error:", err) os.Exit(1) } + os.Stdout.Write(out) case "annotate": os.Exit(annotateSource()) case "report": diff --git a/gocov/test.go b/gocov/test.go index f5da51d..9f37a4f 100644 --- a/gocov/test.go +++ b/gocov/test.go @@ -30,6 +30,7 @@ import ( "path/filepath" "strings" + "github.com/axw/gocov/gocov/convert" "github.com/axw/gocov/gocov/internal/testflag" ) @@ -100,5 +101,7 @@ func runTests(args []string) error { } // Merge the profiles. - return convertProfiles(files...) + out, err := convert.ConvertProfiles(files...) + os.Stdout.Write(out) + return err }