-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
321 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/xhd2015/xgo/cmd/xgo/coverage" | ||
) | ||
|
||
func main() { | ||
// os.Arg[0] = coverage | ||
// os.Arg[1] = args | ||
args := os.Args[1:] | ||
coverage.Main(args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package coverage | ||
|
||
const help = ` | ||
Xgo tool coverage is a tool to view and merge go coverage profiles, just like an extension to go tool cover. | ||
Usage: | ||
xgo tool coverage <cmd> [arguments] | ||
The commands are: | ||
merge merge coverage profiles | ||
help show help message | ||
Options for merge: | ||
-o <file> output to file instead of stdout | ||
--exclude-prefix <pkg> exclude coverage of a specific package and sub packages | ||
Examples: | ||
xgo tool coverage merge -o cover.a cover-a.out cover-b.out merge multiple files into one | ||
See https://github.com/xhd2015/xgo for documentation. | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package coverage | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/xhd2015/xgo/support/coverage" | ||
) | ||
|
||
func Main(args []string) { | ||
if len(args) == 0 { | ||
fmt.Fprintf(os.Stderr, "requires cmd\n") | ||
os.Exit(1) | ||
} | ||
cmd := args[0] | ||
if cmd == "help" { | ||
fmt.Print(strings.TrimPrefix(help, "\n")) | ||
return | ||
} | ||
args = args[1:] | ||
|
||
var remainArgs []string | ||
var outFile string | ||
n := len(args) | ||
|
||
var flagHelp bool | ||
var excludePrefix []string | ||
for i := 0; i < n; i++ { | ||
arg := args[i] | ||
if arg == "--" { | ||
remainArgs = append(remainArgs, args[i+1:]...) | ||
break | ||
} | ||
if arg == "-o" { | ||
if i+1 >= n { | ||
fmt.Fprintf(os.Stderr, "%s requires file\n", arg) | ||
os.Exit(1) | ||
} | ||
outFile = args[i+1] | ||
i++ | ||
continue | ||
} | ||
if arg == "--exclude-prefix" { | ||
if i+1 >= n { | ||
fmt.Fprintf(os.Stderr, "%s requires argument\n", arg) | ||
os.Exit(1) | ||
} | ||
if args[i+1] == "" { | ||
fmt.Fprintf(os.Stderr, "%s requires non empty argument\n", arg) | ||
os.Exit(1) | ||
} | ||
excludePrefix = append(excludePrefix, args[i+1]) | ||
i++ | ||
continue | ||
} | ||
if arg == "--help" || arg == "-h" { | ||
flagHelp = true | ||
continue | ||
} | ||
if !strings.HasPrefix(arg, "-") { | ||
remainArgs = append(remainArgs, arg) | ||
continue | ||
} | ||
fmt.Fprintf(os.Stderr, "unrecognized flag: %s\n", arg) | ||
os.Exit(1) | ||
} | ||
if flagHelp { | ||
fmt.Print(strings.TrimPrefix(help, "\n")) | ||
return | ||
} | ||
|
||
switch cmd { | ||
case "merge": | ||
if len(remainArgs) == 0 { | ||
fmt.Fprintf(os.Stderr, "requires files\n") | ||
os.Exit(1) | ||
} | ||
err := mergeCover(remainArgs, outFile, excludePrefix) | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
default: | ||
fmt.Fprintf(os.Stderr, "unrecognized cmd: %s\n", cmd) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func mergeCover(files []string, outFile string, excludePrefix []string) error { | ||
var mode string | ||
covs := make([][]*coverage.CovLine, 0, len(files)) | ||
for _, file := range files { | ||
content, err := os.ReadFile(file) | ||
if err != nil { | ||
return err | ||
} | ||
covMode, cov := coverage.Parse(string(content)) | ||
covs = append(covs, cov) | ||
if mode == "" { | ||
mode = covMode | ||
} | ||
} | ||
res := coverage.Merge(covs...) | ||
res = coverage.Filter(res, func(line *coverage.CovLine) bool { | ||
return !hasAnyPrefix(line.Prefix, excludePrefix) | ||
}) | ||
|
||
if mode == "" { | ||
mode = "set" | ||
} | ||
mergedCov := coverage.Format(mode, res) | ||
|
||
var out io.Writer = os.Stdout | ||
if outFile != "" { | ||
file, err := os.Create(outFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
out = file | ||
} | ||
_, err := io.WriteString(out, mergedCov) | ||
return err | ||
} | ||
|
||
func hasAnyPrefix(s string, prefixes []string) bool { | ||
for _, prefix := range prefixes { | ||
if strings.HasPrefix(s, prefix) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.