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

Commit

Permalink
Move Convert to a sub-package (#98)
Browse files Browse the repository at this point in the history
Move Convert to a sub-package to make it possible
to run from other tools as an API
  • Loading branch information
karelbilek authored Feb 12, 2023
1 parent 12cc591 commit dd288a8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
23 changes: 15 additions & 8 deletions gocov/convert.go → gocov/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand All @@ -49,22 +55,23 @@ 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
}
}

for _, pkg := range converter.packages {
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 {
Expand Down
2 changes: 1 addition & 1 deletion gocov/convert_test.go → gocov/convert/convert_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package convert

import (
"go/ast"
Expand Down
5 changes: 4 additions & 1 deletion gocov/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"os"

"github.com/axw/gocov"
"github.com/axw/gocov/gocov/convert"
)

func usage() {
Expand Down Expand Up @@ -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":
Expand Down
5 changes: 4 additions & 1 deletion gocov/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"path/filepath"
"strings"

"github.com/axw/gocov/gocov/convert"
"github.com/axw/gocov/gocov/internal/testflag"
)

Expand Down Expand Up @@ -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
}

0 comments on commit dd288a8

Please sign in to comment.